Some scenarios we need to select different backend pools based on some attributes in the request.
Nginx has that capability to selecting different backend pools based on the request header value.
To accomplish this in Nginx you can use the following code in your configuration.
upstream gold { server 127.0.0.1:8080; server 127.0.0.1:8081; server 127.0.0.1:8082; } upstream platinum { server 127.0.0.1:8083; server 127.0.0.1:8084; server 127.0.0.1:8085; } upstream silver { server 127.0.0.1:8086; server 127.0.0.1:8087; } # map to different upstream backends based on header map $customer_type $pool { default "gold"; platinum "platinum"; silver "silver"; } server { listen 80; server_name localhost; location / { proxy_pass http://$pool; #standard proxy settings proxy_set_header X-Real-IP $remote_addr; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-NginX-Proxy true; proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 600; send_timeout 600; }
You can test this setup by sending request with custom http header customer_type.
Ex:- customer_type = gold
This will load balance only among the 8080, 8081 and 8082.
Comments