目的:Nginx 代理过滤header
环境:
系统:Centos 7.5
环境:x64,nginx 1.4
需求:
- Nginx作负载均衡,在业务场景更改以后,为避免重复编码。将路由规则放入Http请求headers中,从Nginx端进行过滤路由。
方法步骤:
1. 定义一个location规则
upstream server_test1{
server 192.168.1.18:8086 weight=1;
server 192.168.1.17:8086 weight=1;
}
upstream server_test2{
server 192.168.1.19:8087;
}
location = /api/hello/test{
proxy_pass http://server_test1;
}
2. 修改location,从Http header中获取参数,然后决定proxy
location = /api/hello/test{
#应特别注意,if和(中间要有一个空格,否则会报错
if ($http_sdkVersion = "v1.0") {
proxy_pass http://server_test1;
break; #执行完毕跳出,则不会往下执行
}
pro