linux - 为什么 try_files 不强制立即响应 404?

标签 linux nginx

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/

相关文章:

c++ - 在 Ubuntu 上为 vim 安装 clang_complete

linux - 如何在 Linux 中从 url(不是直接 url)下载文件

linux - 如何在Linux控制台中删除.jpg文件

nginx - 使用更新的 openssl 获取 Nginx

linux - 不使用 -S 选项对 ls -alh 的输出值进行排序

c++ - 使用CPPCHECK的Red Hat Enterprise Linux(RHEL)上的C++宏问题

php - MySQL 和 PHP 很好,但 Web 服务很慢

node.js - 包含对 Ubuntu 上 Node 服务器内部 "process.env"变量的访问权限的环境文件在哪里?

wordpress - 如何使用Nginx前端通过两个Docker容器运行的两个wordpress网站。

ruby-on-rails - 保存在主机上时,文件不会在 vagrant 中更新?