nginx条件代理通过不同的上下文

时间:2015-05-19 08:04:38

标签: nginx

我的问题是我有几个主服务器,如:

192.168.0.100/service
192.168.0.101/service

和一个备份服务器,如:

192.168.0.102/serviceInternal

Nginx正在倾听:

192.168.0.200:8000

配置文件如下:

upstream sth {
         server 192.168.0.100:8001; #primary server
         server 192.168.0.101:8001; #primary server
         server 192.168.0.102:8001 backup; #backup server
}

server {
         listen   192.168.0.200:8000;

         location /service {
                 if ( primary_servers = up ) {
                        proxy_pass sth/service;
                 }
                 if ( primary_servers = down ) {
                        proxy_pass sth/serviceInternal;
                 }
         }
}

我希望我能以非常明确的方式提出我的问题:)

1 个答案:

答案 0 :(得分:2)

我会使用"假的"备份服务器

upstream sth {
         server 192.168.0.100:8001; #primary server
         server 192.168.0.101:8001; #primary server
         server 127.0.0.1:8001 backup; #backup server
}

upstream sth_backup {
         server 192.168.0.102:8001;
}

# fake server for URI replace
server {
         listen 127.0.0.1:8001;
         location /service {
                 proxy_pass http://sth_backup/serviceInternal;
         }

server {
         listen   192.168.0.200:8000;

         location /service {
                 proxy_pass http://sth/service;
         }
}