nginx_upstream_multiplexing_keepalive
Overview
Http Keep alive(multiplexing) means after a TCP connection serves a request, it’s not closed by server, so that subsequent request can reuse the same connection to avoid create new. hence can improve http performance.
enable it from client side(make sure sever does not disable it)
- Http 1.0: send request(explicit enable)
Connection: keep-alive
- Http 1.1 default is keep-alive(explicit disable
Connection: close
)
Note: even connection is keep-alive, it's not kept for ever if it's idle(most server has a timeout for idle connection)
.
Upstream keep-alive
Here we say how to enable upstream keep-alive, as by default nginx uses http1.0 for upstream, keep-alive is disabled, to enable upstream keep-alive and make it work, it needs
- nginx(now as client) to cache the idle connection(keep it)
- nginx(now as client) not use http1.0 as by default http1.0 upstream server will close the connection after served.
- server(upstream) must not disable keep-alive, out of nginx scope.
1 | http { |
As mentioned, for idle connection, it’s not kept for ever, it may be closed by client(nginx) Or server due to config
- client(nginx) closes it due to timed out(on client side) or serve a number of request
- server(upstream) closes it due to timed out on server side
keep-alive cache
Here is how nginx cache the idle connection to make sure we only cache a number of idle connection.
Data Structure
1 | /* each upstream has one */ |
API
1 | /* check idle cache, if found, use existing connection, otherwise create a new by event framework */ |