nginx - Nginx 与位置不匹配
问题描述
谁能告诉我为什么这个 ngnix 配置与所有以 /admin 开头的 URL 不匹配:
location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
它总是回退到 location / 的默认内容。但是,我在 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。index.html
文件的 URI/path/to/admin/build/index.html
是/admin/index.html
.
在同一个块中使用alias
and可能会有问题。try_files
location
您可能希望使用更可靠的解决方案:
location ^~ /admin {
alias /path/to/admin/build;
if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}
location
andalias
值应该都以 结尾,或者/
都不以 结尾/
。该^~
运算符将阻止其他正则表达式location
块匹配任何以/admin
. 请参阅此使用注意事项if
。
推荐阅读
- java - 活动未列入白名单
- python - ImportError:无法导入名称“pkg_resources”
- c - 将条件字符添加到 C 中的打印语句
- .net - 通用动作的表达式树
- python - python - 如何从python中sklearn中的cross_val_predict获取排序的概率和名称
- tensorflow - 如何编写自定义 keras 层来重新采样 3D 体积?
- node.js - 如何修复应该显示数据的授权的 [object Object] 响应?
- python - xarry 绘图 - 具有最大值的像素不会被设置的级别淹没
- python - re.match() 在python中具有固定模式
- google-maps - 为什么谷歌地图在加载时会切断这个网址的最后两个地址?