Nginx as Reverse Proxy – Disable Upstream Node if 502

nginxreverse-proxy

There is a very simple LB, say, LB1

upstream api_servers {
    least_conn;
        server 10.0.0.193;
        server 10.0.0.11;
    }
server {
    location / {
        proxy_http_version 1.1;
        proxy_pass http://api_servers;
    }
}

Each of the upstream servers is also an nginx LB running locally and reverse proxying a process on 9000 port. The problem is that when any of the processes on port 9000 go down, then LB1 happily returns 502 if request hits one of the machines where the process is down.

LB1:nginx:80 -> 10.0.0.193:nginx:80 -> localhost:9000

How do I tell nginx LB1 to not send requests to a node, which responds with 502?

Best Answer

Would be nice to know the formal answer,as to how make nginx ingore an upstream server if it fails with 502, but in this particular case it is just easier let LB1 contact servers on 9000 and remove intermediate nginx,which is there for historical reasons.