路由和资产在本地服务器上正确加载。但是,在我使用 nginx 在生产服务器上部署后,只有根 url 有效,即 http://calculator.example.com。但是所有资产都返回 403。此外,当我尝试访问任何路由时,例如:http://calculator.example.com/page-1/ 它也返回 403。
Nginx 配置:
server {
listen 80;
server_name calculator.example.com;
root /var/www/html/calculator/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;
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}
答案 0 :(得分:0)
首先,您需要检查请求是否已传递给应用程序。 如果在 nginx 中返回 403,我认为问题是用户运行 nginx 没有访问或执行代码的权限。 如果php应用返回403,请正常调试
答案 1 :(得分:0)
在我删除其他位置的全部拒绝后,此问题得到解决:
location ~ /.(?!well-known).* {
deny all;
}
答案 2 :(得分:0)
如果您关闭了目录索引,并且遇到了这个问题,那可能是因为您使用的 try_files 有一个目录选项:
location / {
try_files $uri $uri/ /index.php?$query_string;
} ^ that is the issue
删除它,它应该可以工作:
location / {
try_files $uri /index.php?$query_string;
}
TL;DR:这是因为 nginx 会尝试索引目录,并被自己阻止。抛出OP提到的错误。
try_files $uri $uri/
表示从根目录尝试 uri
指向的文件,如果该文件不存在,则尝试一个目录(因此是 /
)。当 nginx 访问一个目录时,它会尝试对其进行索引并将其中的文件列表返回给浏览器/客户端,但是默认情况下目录索引被禁用,因此它返回错误“Nginx 403 错误:[文件夹] 的目录索引”被禁止”。
目录索引由 autoindex
选项控制:https://nginx.org/en/docs/http/ngx_http_autoindex_module.html