Nginx根据url中的path动态转发到upstream的实现
在Nginx中,有一些高级场景,需要根据url中的path参数,动态转发到不通的upstream场景1
/svr1/xxxx?yyy 转发到 svr1:8080/xxxx?yyy
/svr2/xxxx?yyy 转发到 svr2:8080/xxxx?yyy
配置如下:
location ~* /(srv+)/(.*)$ {
allow all;
proxy_pass http://$1/$2$is_args$args;
proxy_set_header Host $host;
proxy_set_header x-forwarded-for $forwarded_addr;
}
upstream srv1 {
server srv1-ip:8080;
}
upstream srv2 {
server srv2-ip:8080;
}
场景2
svc1下有3个对等服务srv1,2,3,/svc1/xxxx?yyy 转发到,srv1/2/3:8080/xxxx?yyy
svc2下有3个对等服务srv4,5,6,/svc2/xxxx?yyy 转发到,svr4/5/6:8080/xxxx?yyy
location ~* /(svc+)/(.*)$ {
allow all;
proxy_pass http://$1/$1/$2$is_args$args;
proxy_set_header Host $host;
proxy_set_header x-forwarded-for $forwarded_addr;
}
upstream svc1 {
server srv1:8080;
server srv2:8080;
server srv3:8080;
}
upstream svc2 {
server srv3:8080;
server srv4:8080;
server srv5:8080;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.coder4.com/archives/6914
文档来源:服务器之家http://www.zzvips.com/article/52394.html
页:
[1]