运行PHP时Nginx 403 Forbidden

Posted

技术标签:

【中文标题】运行PHP时Nginx 403 Forbidden【英文标题】:Nginx 403 Forbidden when running PHP 【发布时间】:2021-06-06 00:06:46 【问题描述】:

注意:我没有在任何地方得到我的答案这就是我制作这个的原因

我的 nginx.conf 文件:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events 
    worker_connections  1024;



http 
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server 
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / 
            root   html;
            index index.html index.htm index.php;
        

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html 
            root   html;
        

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ 
        #    proxy_pass   http://127.0.0.1;
        #

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ 
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  C:/nginx/html/$fastcgi_script_name;
           include        fastcgi_params;
        

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht 
        #    deny  all;
        #
    


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server 
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / 
    #        root   html;
    #        index  index.html index.htm;
    #    
    #


    # HTTPS server
    #
    #server 
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / 
    #        root   html;
    #        index  index.html index.htm;
    #    
    #


我也跑了

php-cgi.exe -b 127.0.0.1:9000

在我的 C:\PHP 中的命令提示符中

我得到的错误是

403 禁止 nginx/1.19.7

在本地主机中。

我没有启用代理,我真的不知道错误是什么 我注意到有一个错误文件 所以这是该文件的内容:

2021/03/08 07:13:46 [emerg] 17544#7752: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
2021/03/08 07:14:04 [emerg] 1160#19796: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
2021/03/08 07:14:15 [emerg] 15840#20192: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
2021/03/08 07:17:55 [error] 17248#19796: *9 CreateFile() "C:\nginx/html/robots.txt" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /robots.txt?448659 HTTP/1.1", host: "localhost"
2021/03/08 07:25:13 [error] 17248#19796: *41 CreateFile() "C:\nginx/html/phpmyadmin" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /phpmyadmin HTTP/1.1", host: "localhost"
2021/03/08 07:26:02 [error] 17248#19796: *41 directory index of "C:\nginx/html/Facebook/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET /Facebook/ HTTP/1.1", host: "localhost"
2021/03/08 07:26:07 [error] 17248#19796: *48 CreateFile() "C:\nginx/html/robots.txt" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /robots.txt?448659 HTTP/1.1", host: "localhost"
2021/03/08 07:26:48 [error] 17248#19796: *49 CreateFile() "C:\nginx/html/favicon.ico" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
2021/03/08 07:26:53 [error] 17248#19796: *49 directory index of "C:\nginx/html/Facebook/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET /Facebook/ HTTP/1.1", host: "127.0.0.1"
2021/03/08 07:31:00 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:31:00 [error] 17248#19796: *70 CreateFile() "C:\nginx/html/favicon.ico" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost", referrer: "http://localhost/"
2021/03/08 07:31:42 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:31:43 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:31:43 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:32:24 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:32:27 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:32:28 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:32:28 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:32:29 [error] 17248#19796: *70 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:34:07 [error] 17248#19796: *89 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:35:52 [error] 17248#19796: *94 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:35:52 [error] 17248#19796: *94 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:35:53 [error] 17248#19796: *94 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:35:53 [error] 17248#19796: *94 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:35:53 [error] 17248#19796: *94 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:35:54 [error] 17248#19796: *94 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:37:29 [error] 17248#19796: *111 directory index of "C:\nginx/html/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2021/03/08 07:37:29 [error] 17248#19796: *111 CreateFile() "C:\nginx/html/favicon.ico" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost", referrer: "http://localhost/"

【问题讨论】:

C:\nginx/html/ 目录中有哪些文件? @RichardSmith 我有一个可以使用的 index.html 文件和一个名为 Script 的文件夹,其中包含我的 PHP 代码 你试过打开http://localhost/index.php吗?你的 index.php 脚​​本在 c:\nginx\html 内吗? 是的,我什至试过127.0.0.1:900/index.php,第二个问题是的 首先,您似乎无法在端口 80 上绑定。您可以在浏览器中的localhost 上看到您的 index.html 文件吗?你的 403 的原因就像在这篇文章中一样:***.com/questions/66941065/…。您的根位置或 index.html 有问题。请分享`C:\nginx\html`的截图 【参考方案1】:

netsh http add iplisten ipaddress=:: 面临类似的问题。在命令提示符下运行上述命令。 这应该会释放 80 端口,并且您可以运行 nginx。

说明: netsh http 命令用于查询和配置 HTTP.sys 的设置和参数。

add iplisten :将新的 IP 地址添加到 IP 侦听列表中,不包括端口号。 "::" 表示任何 IPv6 地址。

有关更多 netsh http 命令,请参阅 netsh http 命令文档。

设置后重启NGiNx

并在不同的浏览器中刷新页面

service nginx restart

service nginx stop

service nginx start

检查 NGINX 的状态

service nginx status

如果问题仍然存在,请对项目进行完整备份。

卸载 NGINX 服务器

  apt remove nginx

   apt autoremove

   apt install nginx

删除服务器时,请确保仅卸载服务器。 这样您就不会遇到其他服务器附加组件的问题。

学习

https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

【讨论】:

遗憾的是这没有帮助!问题出在我的 nginx.conf 文件上!

以上是关于运行PHP时Nginx 403 Forbidden的主要内容,如果未能解决你的问题,请参考以下文章

plupload 运行时在尝试访问 upload.php 时返回 403 FORBIDDEN 错误

nginx:403 forbidden 二种原因

nginx 上传文件时返回 403 禁止错误

Nginx 403 forbidden多种原因及故障模拟重现

Rails,Passenger,Nginx,我得到“403 Forbidden”,但为啥呢?

无法找到 403 Forbidden 错误的原因:Nginx Daphne Django