Nginx与位置不匹配



谁能告诉我为什么这个ngnix配置与所有以/admin开头的URL不匹配:

location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}

它始终回退到位置/的默认内容。但是,我在 Nginx 配置中对所有可能的 URL 进行了硬编码,它有效并且仅与硬编码的 URL 匹配,例如:

location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
location /admin/news/ {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
location /admin/another-url/ {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}

感谢您的帮助。

try_files语句的最后一个术语是 URI。/path/to/admin/build/index.htmlindex.html文件的 URI 是/admin/index.html

在同一location块中使用aliastry_files可能会有问题。

您可能希望使用更可靠的解决方案:

location ^~ /admin {
alias /path/to/admin/build;
if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}

locationalias值都应以/结尾,或者都不以/结尾。^~运算符将阻止其他正则表达式location块匹配任何以/admin开头的 URI。请参阅此注意事项 使用if.

最新更新