谁能告诉我为什么这个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.html
处index.html
文件的 URI 是/admin/index.html
。
在同一location
块中使用alias
和try_files
可能会有问题。
您可能希望使用更可靠的解决方案:
location ^~ /admin {
alias /path/to/admin/build;
if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}
location
和alias
值都应以/
结尾,或者都不以/
结尾。^~
运算符将阻止其他正则表达式location
块匹配任何以/admin
开头的 URI。请参阅此注意事项 使用if
.