通过proxy_pass设置反向代理,隐藏端口号
nginx配置修改,通过 proxy_pass 设置反向代理,监听域名(IP)转发到指定端口。
nginx proxy_pass的配置
Nginx的官网将proxy_pass分为两种类型:
proxy_pass http://localhost:8080/proxy_pass http://localhost:8080/proxy_pass http://localhost:8080/abc
URL末尾存在 uri
处理逻辑:
代理请求时,会先将请求的uri中和location匹配的部分替换成 proxy_pass 指定的uri,再将最终的uri拼接到代理地址,才是最终访问的url
如:
发送如下请求:http://localhost:8088/proxy/index.html
详细解析:
- 请求的uri:/proxy/index.html
location匹配的部分:/proxy
- proxy_pass 指定的uri:/svr1
- 最终的uri:/svr1/index.html (将请求的uri中和location匹配的部分替换成 proxy_pass 指定的uri)
- 代理地址:http://127.0.0.1:8099
- 最终访问的url:http://127.0.0.1:8099/svr1/index.html
- 即访问 http://localhost:8088/proxy/index.html,
- 实际请求路径为 http://127.0.0.1:8099/svr1/index.html
URL末尾不存在 uri
处理逻辑:
代理请求时,直接将请求的uri拼接到代理地址,就是最终访问的url
如:
发送如下请求:http://localhost:8088/proxy2/index.html
详细解析:
- 请求的uri:/proxy2/index.html
- 代理地址:http://127.0.0.1:8099
- 最终访问的url:http://127.0.0.1:8099/proxy2/index.html
- 即访问 http://localhost:8088/proxy2/index.html,
- 实际请求路径为 http://127.0.0.1:8099/proxy2/index.html
下面的几个例子加深理解
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。