Nginx与位置不匹配 [英] Nginx not matching the location

查看:306
本文介绍了Nginx与位置不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么此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块中使用aliastry_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; }
}

locationalias值都应以/结尾或都不以/结尾. ^~运算符将阻止其他正则表达式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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆