location = /bar 应该在 Nginx 中匹配什么请求路径?
What request path is location = /bar supposed to match in Nginx?
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 提到:
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.
但正如我的示例中所解释的那样,它似乎不起作用?那么什么样的请求会匹配location = /bar
指令呢?
URI /bar/
依赖 index
指令 在内部 将 URI 重写为 /bar/index.html
。有关详细信息,请参阅 this document。
完全匹配location
语法将仅匹配原始URI,不重写的URI。
nginx
将使用默认的 location
(在您的配置中是 server
上下文)处理重写的 URI。因此,将在 /tmp/bar/index.html
处搜索 URI /bar/index.html
,但未找到。
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 提到:
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.
但正如我的示例中所解释的那样,它似乎不起作用?那么什么样的请求会匹配location = /bar
指令呢?
URI /bar/
依赖 index
指令 在内部 将 URI 重写为 /bar/index.html
。有关详细信息,请参阅 this document。
完全匹配location
语法将仅匹配原始URI,不重写的URI。
nginx
将使用默认的 location
(在您的配置中是 server
上下文)处理重写的 URI。因此,将在 /tmp/bar/index.html
处搜索 URI /bar/index.html
,但未找到。