【问题标题】:How to fix 403 Forbidden nginx/1.16.1如何修复 403 Forbidden nginx/1.16.1
【发布时间】:2020-08-16 23:02:29
【问题描述】:

我正在尝试在这个 Nginx 服务器上运行一个 Larevel 项目。当我通过网络浏览器访问项目/公共文件时,我得到了

403 Forbidden
nginx/1.16.1

在我的公共文件中,文件看起来像这样

我该如何解决这个问题?

PS:出于好奇,我将 index.php 文件重命名为 index.html,并通过网络浏览器访问了 project/public。然后它向我显示代码而不是给出 403 错误。

这里是 Nginx 配置文件。

user  daemon daemon;
worker_processes  auto;

error_log  "/opt/bitnami/nginx/logs/error.log";

pid        "/opt/bitnami/nginx/logs/nginx.pid";

events {
    use                 epoll;
    worker_connections  1024;
    multi_accept        on;
}


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

    client_body_temp_path  "/opt/bitnami/nginx/tmp/client_body" 1 2;
    proxy_temp_path "/opt/bitnami/nginx/tmp/proxy" 1 2;
    fastcgi_temp_path "/opt/bitnami/nginx/tmp/fastcgi" 1 2;
    scgi_temp_path "/opt/bitnami/nginx/tmp/scgi" 1 2;
    uwsgi_temp_path "/opt/bitnami/nginx/tmp/uwsgi" 1 2;

    access_log  "/opt/bitnami/nginx/logs/access.log";

    sendfile        on;

    keepalive_timeout  65;
    client_max_body_size 80M;

    gzip on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_vary on;
    gzip_types text/plain
               text/xml
               text/css
               text/javascript
               application/json
               application/javascript
               application/x-javascript
               application/ecmascript
               application/xml
               application/rss+xml
               application/atom+xml
               application/rdf+xml
               application/xml+rss
               application/xhtml+xml
               application/x-font-ttf
               application/x-font-opentype
               application/vnd.ms-fontobject
               image/svg+xml
               image/x-icon
               application/atom_xml;

    gzip_buffers 16 8k;

    add_header X-Frame-Options SAMEORIGIN;

    ssl_prefer_server_ciphers  on;
gzip on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_vary on;
    gzip_types text/plain
               text/xml
               text/css
               text/javascript
               application/json
               application/javascript
               application/x-javascript
               application/ecmascript
               application/xml
               application/rss+xml
               application/atom+xml
               application/rdf+xml
               application/xml+rss
               application/xhtml+xml
               application/x-font-ttf
               application/x-font-opentype
               application/vnd.ms-fontobject
               image/svg+xml
               image/x-icon
               application/atom_xml;

    gzip_buffers 16 8k;

    add_header X-Frame-Options SAMEORIGIN;

    ssl_prefer_server_ciphers  on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS;

    include "/opt/bitnami/nginx/conf/bitnami/bitnami.conf";

}

这是 nginx.conf.default 文件

#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;
        }

        #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  /scripts$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;
    #    }
    #}

}

【问题讨论】:

  • 请分享你的nginx配置文件
  • @ChristopheHubert 这是位于 etc/nginx 吗?但是 /etc 中并没有这样一个名为 Nginx 的文件夹。这是位于 /opt/bitnami/nginx 中的 Nginx 配置文件。我会在问题中添加它。
  • @ChristopheHubert 检查,我添加了 nginx.conf 文件。

标签: laravel nginx debian http-status-code-403


【解决方案1】:

您声明将 .php 文件传递​​给 Apache,然后您声明在 127.0.0.1:9000 上使用 fastcgi 服务:

        # 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  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

首先,您可能想要删除 Apache 引用。然后你需要检查你的 fastcgi 后端配置,因为你的 nginx 正在“传递”所有的 php 请求给它。

您还声明了fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;,这意味着您的 php 脚本在 /scripts 文件夹中被搜索。这是正确的吗?如果您想将 PHP 文件保存在 html 文件夹中,您需要使用 fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; 更改此行。还要检查 fastcgi_params 文件中的内容,因为该文件已包含在内,因此您正在从中加载配置。

我想你正在使用 php-fpm,我也可以建议更改配置以使用套接字文件而不是 TCP 连接:

    fastcgi_pass   unix:/var/run/php5-fpm.sock;

并在您的 php-fpm 配置中将 listen = 127.0.0.1:9000 更改为 listen = /var/run/php5-fpm.sock

【讨论】:

  • 这是我第一次上传 laravel 项目,我正在使用 aws 并得到同样的错误你能帮我吗?我已经使用 beanstalk 上传了应用程序,它创建了一个新实例,现在我面临这个错误
猜你喜欢
  • 2020-05-24
  • 1970-01-01
  • 2021-06-06
  • 2016-01-31
  • 1970-01-01
  • 2019-09-22
  • 2020-06-27
  • 2019-10-02
  • 2019-11-03
相关资源
最近更新 更多