nginx的location和proxy_pass

概述

nginx代理访问很好用,但是好多人不清楚 location和proxy_pass组合在一起使用时访问的url被代理的url真实地址是什么,这篇文章我总结一下这个问题。

测试环境安装

使用两台主机A和B,A作为反向代理段,B作为业务端,两台都安装nginx程序,打开访问日志,这样可以在B机器上获取真实的访问地址。

详解

我根据实际使用的情况,总结出所有的组合,带“/”和不带“/”的

场景1

server_name abc.baidu.com;
location  /abc {
  proxy_pass http://192.168.0.10/;
}

访问地址:http://abc.baidu.com/abc/a.html

代理地址:http://192.168.0.10//a.html

场景2

server_name abc.baidu.com;
location  /abc/ {
  proxy_pass http://192.168.0.10;
}

访问地址:http://abc.baidu.com/abc/a.html

代理地址:http://192.168.0.10/abc/a.html

场景3

server_name abc.baidu.com;
##按顺序匹配,此项在最前面
location  /a {
  proxy_pass http://192.168.0.10;
}

访问地址:http://abc.baidu.com/abc/a.html

代理地址:http://192.168.0.10/abc/a.html

场景4

1.

server_name abc.baidu.com;
location  /abc/ {
  proxy_pass http://192.168.0.10/;
}

访问地址:http://abc.baidu.com/abc/a.html

代理地址:http://192.168.0.10/a.html

访问地址:http://abc.baidu.com/abc/efg/ui

代理地址:http://192.168.0.10/efg/ui

2.

server_name abc.baidu.com;
location  /abc/efg/ {
  proxy_pass http://192.168.0.10/;
}

访问地址:http://abc.baidu.com/abc/efg/ui

代理地址:http://192.168.0.10/ui

场景5 

伪指令

server_name abc.baidu.com;
location  /abc/ {
  proxy_pass http://192.168.0.10/xyz/;
}

访问地址:http://abc.baidu.com/abc/a.html

代理地址:http://192.168.0.10/xyz/a.html

场景6

server_name abc.baidu.com;
location  /abc {
  proxy_pass http://192.168.0.10/xyz;
}

访问地址:http://abc.baidu.com/abc/a.html

代理地址:http://192.168.0.10/xyz/a.html

server_name abc.baidu.com;
location  /abc/ {
  proxy_pass http://192.168.0.10/xyz;
}

访问地址:http://abc.baidu.com/abc/a.html

代理地址:http://192.168.0.10/xyza.html

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值