nginx upstream based on http header

upstream apache {
     server 127.0.0.1:8080;
}

upstream staging {
     server 127.0.0.1:8081;
}

upstream development {
     server 127.0.0.1:8082;
}

# map to different upstream backends based on header
map $http_x_server_select $pool {
     default "apache";
     staging "staging";
     dev "development";
}

server {
     listen 80;
     server_name example.com;
     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;
     }
}


This entry was posted on 2019 年 02 月 18 日 and is filed under IT DOC. Written by: . You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.