nginx通过header的值分流后端 - meetbill/butterfly-nginx GitHub Wiki

配置文件

server {  
        listen 80;  
        charset utf-8;  
        underscores_in_headers on; 
        access_log  /var/log/nginx/host.access.log  main; 
        location / {  
            root    html;  
            index index.html index.htm;   
            if ($http_region = "bj_test") {  
                proxy_pass http://127.0.0.1:8080; 
            } 
            if ($http_region = "bj") {  
                proxy_pass http://192.168.53.10:8080; 
            }  
        }  
    } 

启动

docker run --name nginx1 -p 8777:80   -v /home/work/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -d nginx

测试

curl -H "region:bj_test" "http://127.0.0.1:8777"