Nginx High Availability – How to Set Up Reverse Proxy for Failover

failoverhigh-availabilitynginx

The scenario is like this, I have a nginx reverse proxy server in front of two app servers which are backups of each other, I want nginx to forward all user requests to the first app server at first, and if the first app server is down, I want nginx to forward all user requests to the second app server from that time on, until if the second app server is down, and nginx should revert back to forward all user requests to the first app server, so on and so forth. Of course, if both app servers are down then nginx will return an error.

I looked into settings like upstream load balancing and proxy_next_upstream settings but it seems not really what I'm looking for here. Any idea how to configure nginx for my scenario? Thanks.

Best Answer

You'll want to be using the nginx upstream configuration in order to achieve this..

upstream mybackend {
    # first app server
    server 192.168.0.1;

    # second app server
    server 192.168.0.2 backup;
}

server {
    ...

    location / {
        proxy_pass http://mybackend;
    }
}

The key here is the backup parameter, which tells nginx to only utilize that node in the event that primary nodes (i.e; nodes not tagged backup) are unavailable.

Full documentation here.

Related Topic