Nginx Load Balancing – Automatic Failover Configuration

clusterfailoverload balancingnginx

I'm using nginx and NginxHttpUpstreamModule for loadbalancing. My config is very simple:

upstream lb {
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;
}

server {
    listen  89;
    server_name localhost;

    location / {
            proxy_pass      http://lb;
            proxy_redirect  off;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

But with this config, when one of 2 backend server is down, nginx still routes request to it and it results in timeout half of the time 🙁

Is there any solution to make nginx to automatically route the request to another server when it detects a downed server.

Thank you.

Best Answer

I think that it's because nginx is not detecting that the upstream is down because it's on the same machine.

The options that you're looking for are: proxy_next_upstream and proxy_connect_timeout.

Try this:

location / {
        proxy_pass              http://lb;
        proxy_redirect          off;
        proxy_next_upstream     error timeout invalid_header http_500;
        proxy_connect_timeout   2;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
}