栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx proxy

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx proxy

综述

nginx官方网站讲解proxy_pass时,只给了规则的说明,并没有给出具体的示例辅助理解。对于英语不太好的人,理解起来真的很头痛,只能通过测试来验证对英文意思的猜测。
nginx对proxy_pass的官方说明见http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
其将proxy_pass的转发规则分为3类:

  1. url中带path路径
  2. url中不带path路径
  3. nginx无法确认url的替换规则
url中带path路径 判断条件

如果域名后面带了“/”,则认为是url中带了path路径。比如:

  1. proxy_pass http://127.0.0.1/
  2. proxy_pass http://127.0.0.1/aaa
  3. 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这一段

url中不带path路径 判断条件

与第一点相反,proxy_pass指定url中只有域名,比如ttp://127.0.0.1

转发规则

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无法确认url的替换规则

这种场景nginx又细分成3类

location指定的是正则表达式

nginx官方要求配置proxy_pass时,不能带path路径。

When location is specified using a regular expression, and also inside named locations.
In these cases, proxy_pass should be specified without a URI.

path路径在location中使用rewrite重写了

比如

location /name/ {
rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1;
}

proxy_pass配置的url中带变量

比如

location /name/ {
proxy_pass http://127.0.0.1$request_uri;
}

待续
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854389.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号