nginx location通过一个变量if设置,但是try_files
没有执行最后一个条件,直接返回404。
我的 nginx 配置如下:
location ~ /linux/(.*).html {
if ($uri ~* ^(/linux/("shell-"|about))) {
set $xheader "";
}
try_files /linux/$xheader$1.html /linux/$xheader/$1.html @upst;
}
但是这里的 nignx 日志输出不是我所期望的:
2017/09/24 12:31:52 [debug] 14701#14701: *4 http script var: "/linux/about.html"
2017/09/24 12:31:52 [debug] 14701#14701: *4 http script regex: "^(/linux/("shell-"|about))"
2017/09/24 12:31:52 [notice] 14701#14701: *4 "^(/linux/("shell-"|about))" matches "/linux/about.html", client: 127.0.0.1, server: upst.com, request: "GET /linux/about.html HTTP/1.1", host: "upst.com"
2017/09/24 12:31:52 [debug] 14701#14701: *4 http script if
2017/09/24 12:31:52 [debug] 14701#14701: *4 http script value: ""
2017/09/24 12:31:52 [debug] 14701#14701: *4 http script set $xheader
2017/09/24 12:31:52 [debug] 14701#14701: *4 http filename: "/var/www/cache_html/upst/linux/about.html"
2017/09/24 12:31:52 [debug] 14701#14701: *4 add cleanup: 000055BD85012D38
2017/09/24 12:31:52 [debug] 14701#14701: *4 cached open file: /var/www/cache_html/upst/linux/about.html, fd:-1, c:0, e:2, u:8
2017/09/24 12:31:52 [error] 14701#14701: *4 open() "/var/www/cache_html/upst/linux/about.html" failed (2: No such file or directory), client: 127.0.0.1, server: upst.com, request: "GET /linux/about.html HTTP/1.1", host: "upst.com"
2017/09/24 12:31:52 [debug] 14701#14701: *4 http finalize request: 404, "/linux/about.html?" a:1, c:1
2017/09/24 12:31:52 [debug] 14701#14701: *4 http special response: 404, "/linux/about.html?"
2017/09/24 12:31:52 [debug] 14701#14701: *4 internal redirect: "/404.html?"
我的问题是@upst
为什么不工作?
谢谢。
最佳答案
要强制立即返回 404,您可以这样写:
try_files /linux/$xheader$1.html /linux/$xheader/$1.html =404;
如文档中所述:https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files .
您正在做的是告诉 NGiNX 回退到名为 @upst
的位置,您没有将其包含在您的问题中,所以我无法真正告诉您任何关于它的影响位置。
你写的配置完全按照它应该做的,甚至 if
也做了,尽管你真的应该阅读 https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ .
关于linux - 为什么 try_files 不强制立即响应 404?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46386706/