php - Laravel + NGINX 给出 403 禁止

标签 php laravel nginx http-status-code-403 laravel-5.6

第一次在网络 (EC2) 上加载 Laravel 应用程序时,我真的卡住了。

我有一个运行最新 laravel 5.6 的 Ubuntu 18.04 实例。我被困了几个小时试图解决 403 问题。我已关注:

These steps (创建新组,将ubuntu和www-data加入其中,设置group和owner可读、可写、可执行)

These steps设置文件夹权限

...许多其他尝试/服务器重建...

所以刚才我把root下的所有文件和文件夹都设置成777来测试

$ find /home/ubuntu/projectname -type f -exec chmod 777 {} \;
$ find /home/ubuntu/projectname -type d -exec chmod 777 {} \;

根据下面的建议,我也这样做了:

$ namei -l /home/ubuntu/projectname/public
f: /home/ubuntu/projectname/public
drwxr-xr-x root     root     /
drwxr-xr-x root     root     home
drwxr-xr-x ubuntu   ubuntu   ubuntu
drwxrwxrwx www-data www-data projectname
drwxrwxrwx www-data www-data public

$ sudo chmod -R 777 /home/ubuntu/projectname

$ ls -l
drwxrwxrwx   8 www-data www-data   4096 Aug 16 10:25 app
-rwxrwxrwx   1 www-data www-data   1686 Aug 16 10:25 artisan
drwxrwxrwx   3 www-data www-data   4096 Aug 16 10:25 bootstrap
-rwxrwxrwx   1 www-data www-data   1652 Aug 16 10:25 composer.json
-rwxrwxrwx   1 www-data www-data 166078 Aug 16 10:25 composer.lock
drwxrwxrwx   2 www-data www-data   4096 Aug 16 10:25 config
drwxrwxrwx   5 www-data www-data   4096 Aug 16 10:25 database
drwxrwxrwx 999 www-data www-data  36864 Aug 16 10:46 node_modules
-rwxrwxrwx   1 www-data www-data   1442 Aug 16 10:25 package.json
-rwxrwxrwx   1 www-data www-data 604905 Aug 16 10:45 package-lock.json
-rwxrwxrwx   1 www-data www-data   1134 Aug 16 10:25 phpunit.xml
drwxrwxrwx   6 www-data www-data   4096 Aug 16 10:46 public
-rwxrwxrwx   1 www-data www-data   3675 Aug 16 10:25 readme.md
drwxrwxrwx   6 www-data www-data   4096 Aug 16 10:25 resources
drwxrwxrwx   2 www-data www-data   4096 Aug 16 10:25 routes
-rwxrwxrwx   1 www-data www-data    563 Aug 16 10:25 server.php
drwxrwxrwx   6 www-data www-data   4096 Aug 16 10:25 storage
drwxrwxrwx   4 www-data www-data   4096 Aug 16 10:25 tests
drwxrwxrwx  45 www-data www-data   4096 Aug 16 10:45 vendor
-rwxrwxrwx   1 www-data www-data   1738 Aug 16 10:25 webpack.mix.js

最后一个让我难住了,因为结果与我的 nginx 项目错误日志 (tail -f) 中的结果相同:

2018/08/16 13:44:25 [error] 27246#27246: *1 directory index of "/home/ubuntu/projectname/public/" is forbidden, client: 29.99.0.232, server: projectname.com.au, request: "GET / HTTP/1.1", host: "projectname.com.au"

我不知道接下来要尝试什么。 任何帮助都会让你进入我的圣诞贺卡列表!

我的项目被克隆到:

/home/ubuntu/projectname

我的 nginx 配置(取自 Laravel's Deploy page )是:

server {
    listen 80;

    server_name projectname.com.au www.projectname.com.au;
    root /home/ubuntu/projectname/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    access_log  /var/log/nginx/projectname.access.log;
    error_log  /var/log/nginx/projectname.error.log error;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/projectname.com.au/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/projectname.com.au/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
    if ($host = www.projectname.com.au) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = projectname.com.au) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        server_name projectname.com.au www.projectname.com.au;
    return 404; # managed by Certbot
}

最佳答案

尝试更改 Nginx 配置中的这一行:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

为此:

location / {
   try_files $uri /index.php?$query_string;
}

From what I can see, this is caused because nginx will try to index the directory, and be blocked by itself.

来源答案:Nginx 403 error: directory index of [folder] is forbidden

关于php - Laravel + NGINX 给出 403 禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51878924/

相关文章:

php - 动态输入表单php

php - 如何在新行上编写laravel sql查询子句

laravel - nginx 无法使用 Laravel Forge 颁发 letencrypt ssl 证书

php - Bootstrap.min.css 文件未在 tcpdf 中调用

php - 如何将自相似子对象(深层对象保存)保存到数据库中?

php - Laravel 5 Elixir : copy all files except some

javascript - Laravel - 选择2 |表单重置时重置选择值

php - Docker 撰写 : Nginx and PHP-FPM not working

javascript - 简单更新后 sails 无法升起

design-patterns - 在 PHP 项目中,存在哪些模式来存储、访问和组织辅助对象?