nginx proxy_pass 用法总结

用法

语法:proxy_pass URL;
默认值:无
上下文:一般是location内

设置某个location内上游服务器的转发协议、地址、url。
协议支持:http、 https
地址支持:域名和ip,支持设置端口号
样例:

proxy_pass http://localhost:8000/uri/;

如果一个域名能够解析出多个ip地址,则按照 round-robin轮训算法进行负载均衡。如果一个地址是域名,优先查询该域名是否是已经定义好的upstream server group,如果是则直接使用该group的地址和回源策略。

不同场景

location /name/ {
    proxy_pass http://127.0.0.1/remote/;
}
请求url:http://www.baidu.com/name/index.html
转发到后端的url:http://www.baidu.com/remote/index.html
  • proxy_pass 指定了uri的情况下,转发到后端的请求,将请求的uri匹配location的部分替换成proxy_pass指定的uri。
    疑问:这里的标准uri是改写之后的uri,假设改写之后匹配不上location会怎么办?
location /name/ {
    proxy_pass http://127.0.0.1;
}
请求url:http://www.baidu.com/name/index.html
转发到后端的url:http://www.baidu.com/name/index.html
  • proxy_pass没有指定uri的情况下,转发到后端的请求,将直接使用原始的请求往后转发,在经过改写的情况下,将使用改写后的标准url进行转发。
location ~ /name/ {
    proxy_pass http://127.0.0.1;
}
请求url:http://www.baidu.com/name/index.html
转发到后端的url:http://www.baidu.com/name/index.html
  • 这种情况下,location后面跟了一个正则表达式,则proxy_pass 后不能携带uri,直接按照原始url转发,在经过改写的情况下,将使用改写后的标准url进行转发。
location /name/ {
    proxy_pass http://127.0.0.1$request_uri;
}
请求url:http://www.baidu.com/name/index.html
转发到后端的url:http://www.baidu.com/name/index.html
  • 如果proxy_pass的uri部分使用变量,则完全使用该变量的值当作转发的uri,并且不再进行urlencode编码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值