【问题标题】:About priority of nginx location / {} and location = {}关于 nginx location / {} 和 location = {} 的优先级
【发布时间】:2018-05-09 02:08:52
【问题描述】:

在研究nginx位置配置的时候,有一些疑问,下面是我的例子。

文件结构如下: test1/index.html test2/index.html

nginx.conf 的位置部分如下:

    location = / {
            root test1;
            index index.html;
    #       deny all;
    }
    location  / {
        root test2;
        index  index.html;
    }

问题是,当我发出 curl -v http://host/ 时,我得到了 test2/index.html 的页面,但是当我去掉 location = / {} 部分中的 # 时, 结果将是 403 禁止。谁能解释为什么?当 location = same_uri {A} 和 location same_uri {B} 都在配置文件中时,哪个配置将匹配[A 或 B]?非常感谢。

http://nginx.org/en/docs/http/ngx_http_core_module.html#location

【问题讨论】:

    标签: nginx


    【解决方案1】:

    当您请求 URI / 时,nginx 将处理两个请求。

    第一个请求(针对 URI /)由 location = / 块处理,因为它具有最高优先级。该块的功能是将请求更改为/index.html 并重新开始搜索匹配的location 块。

    第二个请求(针对 URI /index.html)由 location / 块处理,因为它匹配与更具体的 location 不匹配的任何 URI。

    所以最终响应来自第二个location 块,但两个块都参与评估访问。

    请参阅 this document 了解 location 语法和 this document index 指令。

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 2016-06-26
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多