Nginx与位置不匹配 [英] Nginx not matching the location
问题描述
谁能告诉我为什么此ngnix配置与所有以/admin开头的URL不匹配:
Can anyone tell me why is this ngnix config doesn't match all URL that starts with /admin :
location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
它总是退回到位置/的默认内容.但是,我在Nginx配置中对所有可能的URL进行了硬编码,它可以工作并且仅与硬编码的URL相匹配,例如:
It always fall back to the default content of location / . However, I hardcoded all the possible URL in the Nginx config, it works and only matches the hard coded URL, something like :
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
.
The final term of the try_files
statement is a URI. The URI of the index.html
file at /path/to/admin/build/index.html
is /admin/index.html
.
在同一location
块中使用alias
和try_files
可能会出现问题.
您可能想使用更可靠的解决方案:
You may want to use a more reliable solution:
location ^~ /admin {
alias /path/to/admin/build;
if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}
location
和alias
值都应以/
结尾或都不以/
结尾. ^~
运算符将阻止其他正则表达式location
块与以/admin
开头的任何URI匹配.有关使用if
的信息,请参见此警告.
The location
and alias
values should both end with /
or neither end with /
. The ^~
operator will prevent other regular expression location
blocks from matching any URI that begins with /admin
. See this caution on the use of if
.
这篇关于Nginx与位置不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!