如果无法确定特定服务器,则从池反向代理到指定服务器

时间:2018-12-10 17:23:50

标签: nginx nginx-location nginx-reverse-proxy nginx-config

在我的nginx配置中(取自here),nginx充当基于x_server_select标头值的反向代理:

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;
}

那很好,但是如果没有x_server_select头,我希望nginx将请求从上游列表代理到随机服务器。我怎样才能做到这一点?

0 个答案:

没有答案