简体   繁体   English

Laravel 资产和路由在使用 nginx 部署到生产服务器后返回 403

[英]Laravel assets and routes returning 403 after deploying on production server using nginx

The routes and assets are loading properly on local server.路线和资产正在本地服务器上正确加载。 However, after I deployed on production server using nginx, only the root url is working which is http://calculator.example.com . However, after I deployed on production server using nginx, only the root url is working which is http://calculator.example.com . But all the assets are returning 403. Also when I try to access any routes for eg: http://calculator.example.com/page-1/ it is also returning 403.但是所有资产都返回 403。此外,当我尝试访问任何路线时,例如: http://calculator.example.com/page-1/它也返回 403。

Nginx config: 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;
    }
}

First, you need check if requests have been passed to app.首先,您需要检查请求是否已传递给应用程序。 if 403 be returned in nginx, i think issue is user run nginx not have permission to access or execute code.如果在 nginx 中返回 403,我认为问题是用户运行 nginx 无权访问或执行代码。 if 403 be returned in php app, please debug normally.如果php app返回403,请正常调试。

This issue got resolved after I removed the deny all for other location:删除其他位置的全部拒绝后,此问题得到解决:

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

If you have directory indexing off, and is having this problem, it's probably because the try_files you are using has a directory option:如果您关闭了目录索引,并且遇到了这个问题,可能是因为您使用的 try_files 有一个目录选项:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}                 ^ that is the issue

Remove it and it should work:删除它,它应该可以工作:

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

Why this happens为什么会发生这种情况

TL;DR: This is caused because nginx will try to index the directory, and be blocked by itself. TL;DR:这是因为 nginx 会尝试索引目录,并被自己阻止。 Throwing the error mentioned by OP.抛出OP提到的错误。

try_files $uri $uri/ means, from the root directory, try the file pointed by the uri , if that does not exists, try a directory instead (hence the / ). try_files $uri $uri/表示从根目录尝试uri指向的文件,如果不存在,则尝试使用目录(因此/ )。 When nginx access a directory, it tries to index it and return the list of files inside it to the browser/client, however by default directory indexing is disabled, and so it returns the error "Nginx 403 error: directory index of [folder] is forbidden".当 nginx 访问一个目录时,它会尝试对其进行索引并将其中的文件列表返回给浏览器/客户端,但是默认情况下目录索引是禁用的,因此它返回错误“Nginx 403 error: directory index of [folder]禁止”。

Directory indexing is controlled by the autoindex option: https://nginx.org/en/docs/http/ngx_http_autoindex_module.html目录索引由自动索引选项控制: autoindex ://nginx.org/en/docs/http/ngx_http_autoindex_module.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM