Nginx不能使用第二个位置



我试图提供两个静态平面html文件,但我的位置设置只能解析为一个。当我访问myurl.com/a/b时,它总是提供.html.

我尝试的第一件事是根据这个答案嵌套/a/b位置,

location /a/ {
location /a/b {
try_files /b.html
}
try_files /a.html
}

但只得到过.html。

然后,我根据这个位置优先级问题添加了^~,认为/a/b会首先匹配,但仍然不是。

location ^~ /a/b {
try_files /b.html;
}
location /a/ {
try_files /a.html;
}

在评论中有一个链接https://nginx.viraptor.info/,如果我将^~尝试复制到那里,结果是:

Final Match:
Location: /a/b
Match type: priority prefix

我还尝试了使用和不使用$uri$uri/,例如

try_files $uri $uri/ XX.html

我的Nginx日志文件中的一些行,我认为是相关的:

[debug] 1222#1222: *10775 http uri: "/a/b/"
...
[debug] 1222#1222: *10775 test location: "/"
[debug] 1222#1222: *10775 test location: "a/"
[debug] 1222#1222: *10775 test location: ~ "/.ht"
[debug] 1222#1222: *10775 using configuration "/a/"
...
[debug] 1222#1222: *10775 trying to use file: "/a.html" "/var/www/html/a.html"
[debug] 1222#1222: *10775 try file uri: "/a.html"

然后我根据这个问题尝试使用完全匹配,但我的结果仍然是.html

location = /a/b/ {
try_files /b.html =404;
}
location /a/ {
try_files /a.html =404;
}

我的配置出了什么问题?

如果静态文件a.html和b.html都位于一个位置。您只需要一个位置即可提供请求/a/和/a/b:

location / {
root ... ;
try_files $uri =404;
rewrite /([^/]+)/$ /$1.html last;
}

最新更新