nginx位置由变量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
为什么不行?
感谢。
答案 0 :(得分:0)
要强制执行立即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
的位置,您没有在问题中包含该位置,因此我无法真正告诉您该位置的影响。
尽管您确实应该阅读https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/,但您编写的配置也可以实现应有的功能,甚至是if
。