Laravel、Docker和Nginx:404用于/public文件夹中的所有文件



我使用docker compose 3.3和Nginx 1.17在Laravel 7中创建了一个新项目。

在Laravel上创建的enpoints运行良好,当我试图访问"/public"文件夹中的任何静态资产时,问题就来了。我尝试了使用.css.js文件以及使用robots.txt,但Laravel返回了404未找到的错误。

这是我的nginx.conf(取自https://laravel.com/docs/7.x/deployment#nginx)

events {
}
http {
server {
server {
listen 80;
server_name localhost;
root /var/www/app/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ .php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}
}

我尝试了论坛和Stackoverflow上的不同修复程序,比如重新编译文件、清除缓存。。。但什么都不管用。

你能发现问题吗?

您可以尝试以下配置吗:

server {
listen 80;
index index.php index.html;
server_name localhost;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

最新更新