Nginx Upstream Keepalive: Reuse Backend Connections
Two independent connections
A reverse proxy terminates the client connection and opens a separate connection to an application server. Reusing the backend connection avoids repeating TCP setup, and potentially TLS setup for encrypted upstreams. Reuse does not mean caching the application response or pinning a user session to one process.
Client A -> Nginx -> backend connection #7: GET /catalogClient A disconnects; #7 becomes idleClient B -> Nginx -> backend connection #7: GET /catalogThe second request can reuse the connection only when the server keeps it open, the response is fully consumed and the connection is otherwise eligible. Client keepalive has its own policy.
Configure reuse explicitly
In the HTTP upstream group, reserve a small idle cache. Explicit HTTP/1.1 and removal of the hop-by-hop Connection header make the configuration clear on older builds. These are HTTP-context fragments, not standalone files.
upstream catalog_reuse { server 127.0.0.1:18181; keepalive 8;}server { listen 127.0.0.1:18180; location / { proxy_pass http://catalog_reuse; proxy_http_version 1.1; proxy_set_header Connection ""; }}The documented defaults changed in 1.29.7: upstream idle caching is enabled with 32 local entries and HTTP/1.1 is the proxy default. The local argument was added then; do not put it in a 1.26.x configuration. Explicit keepalive 8 on newer releases permits cross-location reuse of matching upstream connections, unlike the new default local. Older releases require enabling reuse explicitly. Place alternative balancing methods before keepalive for compatibility.
Bound the idle pool and recycle connections
Each worker maintains its own idle cache. With four workers and eight entries in one group, up to 32 idle entries are possible for that group, not counting active connections. Multiple groups and overlapping workers during reload can increase the socket footprint. The limit is not a concurrency cap.
keepalive 8;keepalive_requests 100;keepalive_time 10m;keepalive_timeout 20s;Request count and connection age encourage periodic recycling; the age limit does not forcibly interrupt an in-flight response. Idle expiry closes unused sockets. Align these settings with the backend idle policy and connection budget. proxy_socket_keepalive is a TCP-level probing feature, not the HTTP idle connection cache.
Observe rather than guess
Use the staged fixture in lab-fixtures/chapter-17/. It assigns a connection number per accepted backend socket. Fresh client connections can still receive the same number when the upstream socket is reused. Compare a route that sends Connection: close to a route that clears it.
reuse: connection=1, connection=1close: connection=2, connection=3The result is a controlled observation, not a universal guarantee. Under load or backend closes, reuse may decline. Measure backend connection creation, resource usage and request latency before tuning.
Practice
A pool has three workers and keepalive 12. Estimate the maximum idle entries in that group in steady state; explain why this is not a total connection limit and how to observe reuse.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx Upstream Keepalive: Reuse Backend Connections
Distinguish the two connection lifetimes and configure a small upstream idle cache.
What does keepalive 12 bound?
Why clear Connection on a basic HTTP/1.1 proxy route?
Four workers with 8 idle slots can hold how many idle entries in one group?
What changed in 1.29.7?
Does keepalive_time interrupt a running response at the age limit?