Nginx 位置块导致请求返回 404 状态
Nginx location block results in requests returning a 404 status
我正在尝试将 header 自动添加到对存储在 /storage/
文件夹中的正在提取的文件的任何响应中。
每当我使用这个 location-block:
location /storage/ {
add_header 'Access-Control-Allow-Origin' '*';
}
我收到 404 响应:
HTTP/1.1 404 Not Found
Server: nginx/1.19.4
Date: Tue, 17 Nov 2020 20:44:52 GMT
Content-Type: image/jpeg
Content-Length: 28300
Connection: keep-alive
ETag: "5fb37909-6e8c"
一旦我注释掉 location-block 条件:
#location /storage/ {
add_header 'Access-Control-Allow-Origin' '*';
#}
它正确地添加了 header
HTTP/1.1 200 OK
Server: nginx/1.19.4
Date: Tue, 17 Nov 2020 20:46:12 GMT
Content-Type: image/jpeg
Content-Length: 28300
Last-Modified: Tue, 17 Nov 2020 07:17:29 GMT
Connection: keep-alive
ETag: "5fb37909-6e8c"
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
为了排除任何权限问题,我已将我尝试获取的文件(图片)的权限设置为 777。
如果有人想浏览完整的 nginx.conf,就是这样:
server {
listen 127.0.0.1:80 default_server;
root /;
charset utf-8;
client_max_body_size 128M;
location /storage/ {
add_header Access-Control-Allow-Origin *;
}
location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
internal;
alias /;
try_files $uri $uri/;
}
location / {
rewrite ^ "/Users/chrisbreuer/.composer/vendor/laravel/valet/server.php" last;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log "/Users/chrisbreuer/.config/valet/Log/nginx-error.log";
error_page 404 "/Users/chrisbreuer/.composer/vendor/laravel/valet/server.php";
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass "unix:/Users/chrisbreuer/.config/valet/valet.sock";
fastcgi_index "/Users/chrisbreuer/.composer/vendor/laravel/valet/server.php";
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "/Users/chrisbreuer/.composer/vendor/laravel/valet/server.php";
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
如果能在浏览器和其他 headers(Content-Type
、Content-Length
) 正在正确添加,表明图像已“找到”。
非常感谢!
编辑 1
我不确定这是否有所不同,但“存储”文件夹是一个符号链接。
编辑 2
nginx错误:
2020/11/17 12:45:50 [error] 64990#0: *1 open() "/storage/media/1/1/turtle.jpg" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "HEAD /storage/media/1/1/turtle.jpg HTTP/1.1", host: "my-api.test"
当我尝试在浏览器中访问 URL 时 my-api.test/storage/media/1/1/turtle.jpg
它是 returns 图像而不是 404。
您的服务器根目录设置为 /
。
因此,当您指定 /storage/ 位置时,它会在 /storage/media/1/1/turtle.jpg
中查找文件 - 该文件打印在您的日志中,但由于文件不在其中而失败。
当您删除此位置块时,它使用具有重写命令的location /
。
因此,如果您修改存储位置并在其中包含适当的重写指令,这应该会起作用。请注意,这需要进行一些调整才能做到这一点 - 请参阅此处的文档:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
最后,静态内容的最佳做法是根本不使用重写,而是为其在磁盘上确定适当的位置并修改 root
指令以匹配该位置并摆脱重写。这应该会提高性能和可读性。
我正在尝试将 header 自动添加到对存储在 /storage/
文件夹中的正在提取的文件的任何响应中。
每当我使用这个 location-block:
location /storage/ {
add_header 'Access-Control-Allow-Origin' '*';
}
我收到 404 响应:
HTTP/1.1 404 Not Found
Server: nginx/1.19.4
Date: Tue, 17 Nov 2020 20:44:52 GMT
Content-Type: image/jpeg
Content-Length: 28300
Connection: keep-alive
ETag: "5fb37909-6e8c"
一旦我注释掉 location-block 条件:
#location /storage/ {
add_header 'Access-Control-Allow-Origin' '*';
#}
它正确地添加了 header
HTTP/1.1 200 OK
Server: nginx/1.19.4
Date: Tue, 17 Nov 2020 20:46:12 GMT
Content-Type: image/jpeg
Content-Length: 28300
Last-Modified: Tue, 17 Nov 2020 07:17:29 GMT
Connection: keep-alive
ETag: "5fb37909-6e8c"
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
为了排除任何权限问题,我已将我尝试获取的文件(图片)的权限设置为 777。
如果有人想浏览完整的 nginx.conf,就是这样:
server {
listen 127.0.0.1:80 default_server;
root /;
charset utf-8;
client_max_body_size 128M;
location /storage/ {
add_header Access-Control-Allow-Origin *;
}
location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
internal;
alias /;
try_files $uri $uri/;
}
location / {
rewrite ^ "/Users/chrisbreuer/.composer/vendor/laravel/valet/server.php" last;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log "/Users/chrisbreuer/.config/valet/Log/nginx-error.log";
error_page 404 "/Users/chrisbreuer/.composer/vendor/laravel/valet/server.php";
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass "unix:/Users/chrisbreuer/.config/valet/valet.sock";
fastcgi_index "/Users/chrisbreuer/.composer/vendor/laravel/valet/server.php";
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "/Users/chrisbreuer/.composer/vendor/laravel/valet/server.php";
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
如果能在浏览器和其他 headers(Content-Type
、Content-Length
) 正在正确添加,表明图像已“找到”。
非常感谢!
编辑 1
我不确定这是否有所不同,但“存储”文件夹是一个符号链接。
编辑 2
nginx错误:
2020/11/17 12:45:50 [error] 64990#0: *1 open() "/storage/media/1/1/turtle.jpg" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "HEAD /storage/media/1/1/turtle.jpg HTTP/1.1", host: "my-api.test"
当我尝试在浏览器中访问 URL 时 my-api.test/storage/media/1/1/turtle.jpg
它是 returns 图像而不是 404。
您的服务器根目录设置为 /
。
因此,当您指定 /storage/ 位置时,它会在 /storage/media/1/1/turtle.jpg
中查找文件 - 该文件打印在您的日志中,但由于文件不在其中而失败。
当您删除此位置块时,它使用具有重写命令的location /
。
因此,如果您修改存储位置并在其中包含适当的重写指令,这应该会起作用。请注意,这需要进行一些调整才能做到这一点 - 请参阅此处的文档:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
最后,静态内容的最佳做法是根本不使用重写,而是为其在磁盘上确定适当的位置并修改 root
指令以匹配该位置并摆脱重写。这应该会提高性能和可读性。