Nginx failover without load balancing

configurationfailoverload balancingnginxreverse-proxy

I'm having trouble configuring nginx.

I'm using nignx as a reverse proxy. I want to send my all requests to my first server. If the first server is down, I want to send requests to second server.

In short, how can I have a failover solution without load balancing?

Best Answer

What you want is an active+passive setup. Here's an example nginx conf snippet to get you going:

upstream backend {
    server 1.2.3.4:80 fail_timeout=5s max_fails=3;
    server 4.5.6.7:80 backup;
}

server {
    listen 80;
    server_name whatevs.com;

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

So, 'normally', all requests will go to host 1.2.3.4. If we get three failures to that box, then 4.5.6.7 will take over.