【问题标题】:What request path is location = /bar supposed to match in Nginx?location = /bar 应该在 Nginx 中匹配什么请求路径?
【发布时间】:2018-11-04 13:41:29
【问题描述】:

location = /bar 在 Nginx 中应该匹配什么请求路径?

什么工作正常:location /bar

这是我的 nginx 配置、主机文件配置和我的 HTML 文件。

# cat /etc/nginx/sites-enabled/foo
server {
    listen 80;
    listen [::]:80;
    server_name foo;

    root /tmp/;

    location /bar/ {
        alias /var/www/foo/;
    }
}

# cat /etc/hosts
127.0.0.1       localhost foo
127.0.1.1       debian


# cat /tmp/index.html
Hi! I am Tmp!

# cat /var/www/foo/index.html
<p>Hi! I am Index!</p>

# cat /var/www/foo/max.html
<p>Hi! I am Max!</p>

对根、/bar/ 和 /bar/max.html 的 HTTP 请求会产生预期的 输出:

# systemctl restart nginx && curl http://foo/
Hi! I am Tmp!

# systemctl restart nginx && curl http://foo/bar/
<p>Hi! I am Index!</p>

# systemctl restart nginx && curl http://foo/bar/max.html
<p>Hi! I am Max!</p>

什么不能正常工作:location = /bar

现在我编辑配置以将location /bar 替换为location = /bar

# cat /etc/nginx/sites-enabled/foo
server {
    listen 80;
    listen [::]:80;
    server_name foo;

    root /tmp/;

    location = /bar/ {
        alias /var/www/foo/;
    }
}

# systemctl restart nginx && curl http://foo/
Hi! I am Tmp!

这些 HTTP 请求不再有效:

# systemctl restart nginx && curl http://foo/bar/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:07:50 [error] 29157#29157: *1 open() "/tmp/bar/index.html" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar/ HTTP/1.1", host: "foo"

# systemctl restart nginx && curl http://foo/bar
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:08:49 [error] 29203#29203: *1 open() "/tmp/bar" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar HTTP/1.1", host: "foo"

# systemctl restart nginx && curl http://foo/bar/max.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:10:10 [error] 29265#29265: *1 open() "/tmp/bar/max.html" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar/max.html HTTP/1.1", host: "foo"

似乎/bar/bar/ 的GET 请求与location = /bar/ 指令匹配?我认为这些请求应该与该指令一起使用,因为http://nginx.org/en/docs/http/ngx_http_core_module.html#location 提到:

此外,使用“=”修饰符可以定义 URI 和位置的精确匹配。如果找到完全匹配,则搜索终止。

但正如我在示例中所解释的那样,它似乎不起作用?那么什么样的请求会匹配location = /bar 指令呢?

【问题讨论】:

    标签: nginx configuration location httprequest


    【解决方案1】:

    URI /bar/ 依赖于index 指令来在内部将 URI 重写为 /bar/index.html。详情请见this document

    精确匹配 location 语法将匹配原始 URI,匹配重写后的 URI。

    nginx 将使用默认的location(在您的配置中为server 上下文)处理重写的URI。因此,URI /bar/index.html 将在 /tmp/bar/index.html 处搜索,但未找到。

    【讨论】:

    • 感谢您的回答!我用location = /bar/index.html 替换了location = /bar/,但现在curl http://foo/bar/ 导致HTTP 404curl http://foo/bar/index.html 导致HTTP 301 重定向到`foo/bar/index.html`。你能帮我理解这里发生了什么吗?
    • 我怀疑您需要将 alias 更改为 alias /var/www/foo/index.html; 否则您已将 /bar/index.html 别名为目录 - 因此是 301 重定向。
    • 你是对的。正如你所说,我更改了alias,现在curl http://foo/bar/index.html 确实返回HTTP 200
    猜你喜欢
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2016-06-26
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    相关资源
    最近更新 更多