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

在Nginx中location=/bar应该匹配什么请求路径?,nginx,configuration,location,httprequest,Nginx,Configuration,Location,Httprequest,在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

在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>
这些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/指令匹配?我认为这些请求应该与本指令配合使用,因为其中提到:

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

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

URI/bar/依赖index指令在内部将URI重写为/bar/index.html。有关详细信息,请参阅

精确的匹配位置语法将只匹配原始URI,而不匹配重写的URI


nginx将使用默认位置(在您的配置中是服务器上下文)处理重写的URI。因此,URI/bar/index.html将在/tmp/bar/index.html中搜索,但找不到。

谢谢您的回答!我将location=/bar/替换为location=/bar/index.html,但现在是curlhttp://foo/bar/ 导致HTTP 404和curlhttp://foo/bar/index.html 导致HTTP 301重定向到``。你能帮我理解这里发生了什么吗?我怀疑你需要将别名更改为alias/var/www/foo/index.html;否则,您将/bar/index.html化名为一个目录,从而实现301重定向。您是对的。我按你说的改了化名现在http://foo/bar/index.html 返回HTTP 200。
# 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!
# 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"