使用 nginx 作为代理服务器的路径问题
但是很多时候,我们的服务器地址是这样的:
-
a: 192.168.1.14 -
b: 192.168.1.15 -
c: 192.168.1.16 -
...
我们需要将
-
127.0.0.1/a 代理到 a 服务器 -
127.0.0.1/b 代理到 b 服务器 -
127.0.0.1/c 代理到 c 服务器 -
...
那么应该怎么设置呢?
-
location /a { -
proxy_pass http://192.168.1.14; -
access_log off; -
}
通过试验可以发现,这样代理的实际地址为
并不是我们想要的,为什么会这样呢?
答案在与 proxy_pass 中路径是否有加上/:
当路径加上了/,相当于是绝对根路径,则 nginx 不会把 location 中匹配的路径部分代理。
如果没有/,则会把匹配的路径部分也给代理。
通过以上结论,我们知道应该如何设置了
代理到同一台服务器的多个项目下:
-
location /a/ { -
proxy_pass http://192.168.1.14; -
access_log off; -
} -
location /b/ { -
proxy_pass http://192.168.1.14; -
access_log off; -
} -
location /c/ { -
proxy_pass http://192.168.1.14; -
access_log off; -
}
分别代理为:
-
http://127.0.0.1/a -> http://192.168.1.14/a -
http://127.0.0.1/b -> http://192.168.1.14/b -
http://127.0.0.1/c -> http://192.168.1.14/c
代理到不同服务器上:
-
location /a/ { -
proxy_pass http://192.168.1.14/; -
access_log off; -
} -
location /b/ { -
proxy_pass http://192.168.1.15/; -
access_log off; -
} -
location /c/ { -
proxy_pass http://192.168.1.14/; -
access_log off; -
}
分别代理为:
-
http://127.0.0.1/a -> http://192.168.1.14 -
http://127.0.0.1/b -> http://192.168.1.15 -
http://127.0.0.1/c -> http://192.168.1.16
一个记忆的技巧
proxy_pass 后面带不带 "/"
带 “/” 相当于剪刀会剪掉location的路径

本文详细解析了使用Nginx作为代理服务器时,如何正确配置路径以实现对不同服务器及项目的精准代理,包括理解proxy_pass参数中路径的作用,以及如何区分绝对与相对路径代理。
2096

被折叠的 条评论
为什么被折叠?



