nginx官方网站讲解proxy_pass时,只给了规则的说明,并没有给出具体的示例辅助理解。对于英语不太好的人,理解起来真的很头痛,只能通过测试来验证对英文意思的猜测。
nginx对proxy_pass的官方说明见http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
其将proxy_pass的转发规则分为3类:
- url中带path路径
- url中不带path路径
- nginx无法确认url的替换规则
如果域名后面带了“/”,则认为是url中带了path路径。比如:
- proxy_pass http://127.0.0.1/
- proxy_pass http://127.0.0.1/aaa
- proxy_pass http://127.0.0.1/aaa/
nginx官网原文如下:
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
个人理解如下:
使用proxy_pass配置的url去替换location指定的部分。如下图就是使用http://127.0.0.1/remote/去替换path中/name这一段
与第一点相反,proxy_pass指定url中只有域名,比如ttp://127.0.0.1
转发规则nginx无法确认url的替换规则If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:
location /some/path/ {
proxy_pass http://127.0.0.1;
}
个人理解如下:
转发url=proxy_pass配置的url+原始url中path部分
这种场景nginx又细分成3类
location指定的是正则表达式nginx官方要求配置proxy_pass时,不能带path路径。
path路径在location中使用rewrite重写了When location is specified using a regular expression, and also inside named locations.
In these cases, proxy_pass should be specified without a URI.
比如
proxy_pass配置的url中带变量location /name/ {
rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1;
}
比如
待续location /name/ {
proxy_pass http://127.0.0.1$request_uri;
}



