解读nginx反向代理location和proxy_pass的映射关系

2024-03-01 0 826
目录
  • 1. proxy_pass 只有主机地址时
  • 2. proxy_pass 带路径时
  • 总结

配置nginx反向代理时,总是需要尝试多次才能配置成功,通过本文做个记录,方便以后查看。

1. proxy_pass 只有主机地址时

只有主机地址的意思是,proxy_pass 值为 http://host:port 这种形式,url部分没有 / 或 /xxx 等.

对应到本文示例就是 http://backend。

这种情况下,相当于下面的公式:

backend url = proxy_pass + path

这种情况下,请求的 path 部分会直接追加到 proxy_pass 地址后,相当于把nginx地址一对一的映射到了后端地址,这种配置方式理解最简单。

locationproxy_passpathnginx urlbackend url/http://backend/http://nginx/http://backend//http://backend/hellohttp://nginx/hellohttp://backend/hello/http://backend/hello/worldhttp://nginx/hello/worldhttp://backend/hello/world
locationproxy_passpathnginx urlbackend url/ahttp://backend/ahttp://nginx/ahttp://backend/a/ahttp://backend/abhttp://nginx/abhttp://backend/ab/ahttp://backend/a/http://nginx/a/http://backend/a//ahttp://backend/a/bhttp://nginx/a/bhttp://backend/a/b/ahttp://backend/a/b/http://nginx/a/b/http://backend/a/b/
locationproxy_passpathnginx urlbackend url/b/http://backend/b/http://nginx/b/http://backend/b//b/http://backend/b/ahttp://nginx/b/ahttp://backend/b/a/b/http://backend/b/a/http://nginx/b/a/http://backend/b/a/

2. proxy_pass 带路径时

这种情况下,相当于下面的公式:

backend url = proxy_pass + (path – location)

path 部分减去匹配的 location 部分后,剩余内容追加到 proxy_pass 上去请求。

如果不是为了 /u 去匹配 /uv 这种路径,使用 /v/ 去匹配 /v/w 这种更准确,不会出现 // 这种情况。

locationproxy_passpathnginx urlbackend url/uhttp://backend//uhttp://nginx/uhttp://backend//uhttp://backend//uvhttp://nginx/uvhttp://backend/v/uhttp://backend//u/http://nginx/u/http://backend///uhttp://backend//u/vhttp://nginx/u/vhttp://backend//v/uhttp://backend//u/v/http://nginx/u/v/http://backend//v/
locationproxy_passpathnginx urlbackend url/v/http://backend//v/http://nginx/v/http://backend//v/http://backend//v/whttp://nginx/v/whttp://backend/w/v/http://backend//v/w/http://nginx/v/w/http://backend/w/
locationproxy_passpathnginx urlbackend url/chttp://backend/c/chttp://nginx/chttp://backend/c/chttp://backend/c/cdhttp://nginx/cdhttp://backend/cd/chttp://backend/c/c/http://nginx/c/http://backend/c//chttp://backend/c/c/dhttp://nginx/c/dhttp://backend/c/d/chttp://backend/c/c/d/http://nginx/c/d/http://backend/c/d/
locationproxy_passpathnginx urlbackend url/d/http://backend/d/d/http://nginx/d/http://backend/d/d/http://backend/d/d/ehttp://nginx/d/ehttp://backend/de/d/http://backend/d/d/e/http://nginx/d/e/http://backend/de/
locationproxy_passpathnginx urlbackend url/ehttp://backend/e//ehttp://nginx/ehttp://backend/e//ehttp://backend/e//efhttp://nginx/efhttp://backend/e/f/ehttp://backend/e//e/http://nginx/e/http://backend/e///ehttp://backend/e//e/fhttp://nginx/e/fhttp://backend/e//f/ehttp://backend/e//e/f/http://nginx/e/f/http://backend/e//f/
locationproxy_passpathnginx urlbackend url/f/http://backend/f//f/http://nginx/f/http://backend/f//f/http://backend/f//f/ghttp://nginx/f/ghttp://backend/f/g/f/http://backend/f//f/g/http://nginx/f/g/http://backend/f/g/
locationproxy_passpathnginx urlbackend url/ghttp://backend/m/ghttp://nginx/ghttp://backend/m/ghttp://backend/m/ghhttp://nginx/ghhttp://backend/mh/ghttp://backend/m/g/http://nginx/g/http://backend/m//ghttp://backend/m/g/hhttp://nginx/g/hhttp://backend/m/h/ghttp://backend/m/g/h/http://nginx/g/h/http://backend/m/h/
locationproxy_passpathnginx urlbackend url/h/http://backend/n/h/http://nginx/h/http://backend/n/h/http://backend/n/h/ihttp://nginx/h/ihttp://backend/ni/h/http://backend/n/h/i/http://nginx/h/i/http://backend/ni/
locationproxy_passpathnginx urlbackend url/ihttp://backend/x//ihttp://nginx/ihttp://backend/x//ihttp://backend/x//ijhttp://nginx/ijhttp://backend/x/j/ihttp://backend/x//i/http://nginx/i/http://backend/x///ihttp://backend/x//i/jhttp://nginx/i/jhttp://backend/x//j/ihttp://backend/x//i/j/http://nginx/i/j/http://backend/x//j/
locationproxy_passpathnginx urlbackend url/j/http://backend/y//j/http://nginx/j/http://backend/y//j/http://backend/y//j/khttp://nginx/j/khttp://backend/y/k/j/http://backend/y//j/k/http://nginx/j/k/http://backend/y/k/
locationproxy_passpathnginx urlbackend url/ws/http://backend/spring/ws//ws/qrloginhttp://nginx/ws/qrloginhttp://backend/spring/ws/qrlogin/ws/http://backend/spring/ws//ws/stomphttp://nginx/ws/stomphttp://backend/spring/ws/stomp

参考文档:

英文 – ngx_http_proxy_module.html#proxy_pass

中文 – ngx_http_proxy_module#proxy_pass

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持悠久资源。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Nginx服务器 解读nginx反向代理location和proxy_pass的映射关系 https://www.u-9.cn/server/nginx/174468.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务