2

My problem is that i have several primary servers like:

192.168.0.100/service
192.168.0.101/service

and one backup server like:

192.168.0.102/serviceInternal

Nginx is listening on:

192.168.0.200:8000

Config file looks like:

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

I hope i presented my problem in quite clear way :)

7
  • Could you formulate a question? What is not working and what do you like to achieve? Commented May 19, 2015 at 8:06
  • the main problem is in conditions about primary servers. If primary servers are online i would like to forward traffic to /service endpoint if they are not online i would like to forward traffic to /serviceInternal endpoint on backup server. Commented May 19, 2015 at 8:12
  • Did you have a look at other questions on SA? This might help maybe? Commented May 19, 2015 at 8:14
  • Yes, I have searched SA and I think it is not a solution for my problem because request_body will be always the same. What I need is check primary servers if the are online or not. Commented May 19, 2015 at 8:33
  • If they all would be /service (not /serviceInternal) then simple proxy_pass http://sth/service; would do what you want. Commented May 19, 2015 at 8:36

1 Answer 1

3

I would use "fake" server for backup

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;
         }
}
Sign up to request clarification or add additional context in comments.

1 Comment

wow, I'm impressed :) Really nice workaround :) Thanks a lot @AlexeyTen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.