ran*_*ran 5 nginx docker-compose
我有以下 nginx.conf 文件:
server {
listen 8080;
server_name dummy_server;
root /usr/share/nginx/html;
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
default_type 'application/json';
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Content-Type' 'application/json';
if ($request_uri ~* "([^/]*$)" ) {
set $last_path_component $1;
}
try_files $last_path_component.json $last_path_component.json =404;
}
}
Run Code Online (Sandbox Code Playgroud)
我想healthcheck.json
在localhost:8080/v1/healthcheck
被要求时提供服务。但是,如果我没有明确点击 ,nginx 拒绝提供任何 json 文件localhost:8080/v1/healthcheck.json
,否则它会记录未找到文件,并且响应是 nginx 的 404 页面。如果我在 url 的末尾添加 .json,就会神奇地找到该文件,并且会发生巨大的变化。我尝试强制application/json
类型,以及更改try files
行以显式返回我想要返回的文件(所以我已经设置try_files healthcheck.json =404;
)都返回相同的问题。
我很迷茫..有人有什么想法吗?
小智 5
这实际上是此配置的预期行为。因此,您正在做的是剥离 URL,然后将其传递给try_files
.
但是没有任何东西try_files
可以处理没有扩展名的文件。为此,请像这样修改它
try_files $last_path_component $last_path_component.json $last_path_component.json =404;
Run Code Online (Sandbox Code Playgroud)
这将允许您提供.json
没有扩展名的文件。但是,这不会提供.json
带或不带扩展名的相同文件。该try_files
指令检查文件的存在并按此顺序提供它们。因此,如果您healthcheck.json
尝试通过localhost:8080/v1/healthcheck
它访问它仍然会失败。因此,您需要使healthcheck
端点实际上在服务器上没有扩展才能访问。
如果您想同时处理带有或不带有扩展名的两种变体,您可能需要.json
像这样先剥离零件
if ($request_uri ~ ^/([^?]*)\.json(\?.*)?$) {
return 302 /$1$2;
}
try_files $uri $uri.json =404;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4811 次 |
最近记录: |