How to control keepalive connections for upstream before the version of 1.15.3

Maxim Dounin mdounin at
Mon Feb 25 14:43:14 UTC 2019


On Mon, Feb 25, 2019 at 09:46:56PM +0800, yf chu wrote:

> But if a connection to upstream is dead or there are some other 
> network problems in this connection, how could Nginx handle it, 
> will the HTTP requests on this connection be affected? For 
> example, is it possible that the HTTP requests on this 
> connection is hanging until timeout?  

If a connection is silently dead, nginx will have to wait till a 
relevant timeout expires.  If a network error occurs, nginx will 
be able to detect this and will act accordingly.  If a network 
error or timeout occurs when re-using a keepalive connection, 
nginx will retry a request as per proxy_next_upstream (and will 
allow an additional retry attempt to make sure even requests to a 
single upstream server a retryed).

Note this doesn't really depend on using keepalive connections, as 
well as keepalive_requests and keepalive_timeout directives.  
Though some network problems may become more obvious when using 
keepalive connections.  In particular, a statefull firewall 
between nginx and a backend can be a problem if states are dropped 
after some inactivity timeout, and using keepalive_timeout may 
help to mitigate such problems (though using proxy_socket_keepalive 
or removing the firewall might be a better way to go).

The main goal of the keepalive_timeout directive is to avoid the 
race between closing of a connection by the upstream server and 
using this connection for another request, most importantly in 
case of non-idempotent requests which cannot be retried.

The main goal of the keepalive_requests directive is to make sure 
connections will be closed periodically and connection-specific 
memory allocations will be freed.

Maxim Dounin

More information about the nginx mailing list