公共资源无法访问



我正在访问我的symfony项目的bundles目录(位于:/usr/src/app/public/bundles)。 但是,无法使用我的导航器访问公共目录中的所有文件。例如:

Request URL: http://localhost:8080/bundles/easyadmin/app.css
Request Method: GET
Status Code: 404 Not Found

文件存在...

这是我的nginx配置:

server {
server_name ~.*;
location / {
root /usr/src/app;
try_files $uri /index.php$is_args$args;
}
client_max_body_size 100m;
location ~ ^/index.php(/|$) {
if ($request_method ~* "(GET|POST|PATCH|DELETE)") {
add_header "Access-Control-Allow-Origin" '*' always;
}
# Preflighted requests
if ($request_method = OPTIONS ) {
add_header "Access-Control-Allow-Origin" '*' always;
add_header "Access-Control-Allow-Methods" "GET, POST, PATCH, DELETE, OPTIONS, HEAD";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
return 200;
}
fastcgi_pass php:9000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/src/app/public/index.php;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ .php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}

我不知道错过了什么...

没有看到任何错误日志,我只能猜测问题,但让我们检查您正在使用的目录。

首先尽量避免在地点root

将根放在位置块内将起作用,并且它是完全有效的。问题是当您开始添加位置块时。如果向每个位置块添加根,则不匹配的地址块将没有根。因此,在位置块之前出现根指令非常重要,如果需要,根指令可以覆盖此指令。

配置应如下所示:

root /usr/src/app/public/;
location / {
try_files $uri /index.php$is_args$args;
}

您的location缺少public目录。那么为什么会看到404:

对于此请求http://localhost:8080/bundles/easyadmin/app.css,使用您的配置NGINX将查找/bundles/easyadmin/app.css. And it will not be able to find it. But/usr/src/app/public/bundles/easyadmin/app.css'的/usr/src/app/,这将是一个有效的路径,应该导致200 OK。

最新更新