【问题标题】:Why is simple NGINX exact location match failing?为什么简单的 NGINX 精确位置匹配失败?
【发布时间】:2021-01-26 15:20:28
【问题描述】:

我正在尝试使用 NGINX 将没有路径的请求(例如 http://example.com/http://example.com)路由到使用此配置的特定 html 文件(这只是服务器配置中的相关部分):

root $root;

location = / {
    index index.html;
    alias $root/apps/www/html/main/;
}

如果我仅使用根 url (http://example.com) 导航到该站点,我希望 NGINX 返回 <root>/apps/www/html/main/index.html 但它会忽略确切位置并直接转到根:所有请求都返回错误找不到<root>/index.html

为了尝试调试它,我尝试使用不同的确切位置路径(例如location = /foo),但这些也被忽略了。我也试过用root代替alias,但也没有成功。

如何重新路由到 NGINX 中的特定位置?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    索引模块将在内部将 URI 重写为 /index.html,然后重新开始搜索匹配的 location

    所以你要么需要提供一个location = /index.html 块来匹配新的 URI。

    或者您可以绕过索引模块并改用try_files,在单个location 块中处理整个请求。

    例如:

    location = / {
        try_files /apps/www/html/main/index.html =404;
    }
    

    详情请见this document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-27
      • 2016-05-20
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      相关资源
      最近更新 更多