403 禁止访问在 nginx/1.4.6 (Ubuntu) - Laravel 上的表现

24

我一直收到 403 Forbidden 的错误提示。

enter image description here


我的设置:

/etc/nginx/sites-available/default

default

server {
        listen   80;


        root home/laravel-app/;

        index index.php index.html index.htm;

        server_name example.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }

}

更新

我按照这里的说明进行操作。


如果您有任何提示/建议,将会是极大的帮助!


1
你安装了php5-fpm吗? - Furkan Başaran
你能否查看nginx的错误日志,并查看当您收到状态500时它显示什么错误?我有一种预感,您的sock文件可能具有错误的权限,这导致了状态500。要么是这个问题,要么是您的PHP配置中的某些问题。 - tftd
你尝试过以下命令吗:chown -R www-data:www-data /home/benu/home/forge/ssc-portal 或者 chmod +x /home/benu/home/forge/ssc-portal/index.php - Nguyen Sy Thanh Son
我已经准备好了。我的网站现在正在加载。这个答案对我有用。 - code-8
请检查 error.log 文件。 - plaes
显示剩余2条评论
3个回答

28

您需要为root指令指定一个绝对路径。Nginx使用在编译时使用--prefix开关设置的目录。默认情况下,这是/usr/local/nginx

这意味着您的root,当前设置为home/laravel-app/,会导致nginx在/usr/local/nginx/home/laravel-app/查找文件,这可能不是您文件所在的位置。

如果将您的root指令设置为绝对路径,例如/var/www/laravel-app/public/,nginx将会找到文件。

类似地,您会注意到我在上面的路径中添加了/public/。这是因为Laravel将其index.php文件存储在那里。如果您只是指向/laravel-app/,它将没有索引文件,并且会给您一个403错误。


3
而且,我犯过很多次的一个错误是忘记从我的laravel项目根目录运行 php artisan key:generate。如果你没有设置 APP_KEY 环境变量,即使你的nginx配置正确,你也可能会得到403错误。 - morphatic
编辑完配置文件后,不要忘记重新加载nginx配置文件,可以在终端中使用以下命令:nginx -s reload。 - amiron

1

您需要在默认文件中为php文件编写规则。

# pass the PHP scripts to FastCGI server listening on (...)
#
location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}

1
基本上,你需要一个 PHP 的位置规则。 - ali haider
我将你的代码添加到了我的default文件中。我运行了sudo nginx restart命令。但是我仍然得到相同的结果。 - code-8

0

你在更新中添加的错误表明nginx正在尝试从你的ssc-portal文件夹进行目录索引。由于你似乎正在使用基本的nginx安装,这里目录索引失败的唯一原因是nginx无法找到列出的索引选项。

在你的服务器块中,你告诉nginx在请求目录列表(以斜杠结尾的URI)时按顺序尝试以下位置:index.php、index.html,然后是index.htm。

如果没有找到这些文件,则目录索引请求将失败。

我最好的猜测是你把index.php文件放错了地方。你把它从ssc-portal文件夹中移动了吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接