Imagine that we have a web application that is running in three different servers. And we have a NginX server that has a load balancer and redirects requests to these three servers.
So what happens to requests when the NginX server is no longer running? Are they redirect to one of the servers? If not how could we redirect them to one of the servers?
If one of the load balancing instances is down, requests will still get routed to that server, because nginx has no way of knowing upstream instance is failing. You'll get 502 Bad Gateway
for one out of three requests.
To avoid down servers getting requests, you can use nginx's health checks.
NGINX and NGINX Plus can continually test your upstream servers, avoid the servers that have failed, and gracefully add the recovered servers into the load‑balanced group.
In your app, you can have a path /health_check
that responds with 200 Status Code if the instance is OK and use this configuration.
http {
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
location / {
proxy_pass http://backend;
health_check uri=/health_check;
}
}
}