Nginx – Force nginx to close connection

amazon-elbnginx

We have a setup that looks like this:

Client -> AWS ELB (tcp load balancing)-> NGINX (ssl termination) -> application server.

We want to gracefully take application servers out of the load balancer. We do this by having a health check that responds with 503 when the node is about to shut down, while allowing any pending requests to complete.

However, since nginx is configured to use keep-alive, clients that have already established a connection may continue to send new requests, even after the node has been taken out of the load balancer.

Setting the Connection: close header in all responses from the application server does not help, as nginx (rightfully, according to the RFC) does not pass that on to the client.

Is there any other way, but to disable keep-alive altogether?

Best Answer

We solved this by updating the nginx config to disable keep-alive (by setting both keepalive_requests and keepalive_timeout to 0) and then reload nginx as part of the application shutdown procedure.

It would probably be enough to simply reload the configuration, given that nginx fires up a new worker process when reloading, and only keeps the old worker(s) around until they have finished their ongoing requests.