Web / NGINX Interview questions
Explain the internal working of NGINX's keepalive connection pooling to upstream servers?
By default, NGINX opens a new TCP connection to an upstream server for every proxied request and closes it afterward - fine at low traffic, but wasteful at scale, since TCP handshakes and (if applicable) TLS negotiation carry real latency and CPU cost.
The keepalive directive inside an upstream block changes this by maintaining a pool of already-open, idle connections per worker process that can be reused across multiple requests.
upstream backend_app { server 10.0.0.11:8080; keepalive 32; } server { location / { proxy_pass http://backend_app; proxy_http_version 1.1; proxy_set_header Connection ""; } }
The number after keepalive sets how many idle connections per worker are kept open, not a hard connection limit - if more concurrent requests are in flight than that, NGINX opens additional connections as needed and simply doesn't keep all of them idle afterward. proxy_http_version 1.1 and clearing the Connection header are required, since HTTP/1.0 and a default Connection: close header would otherwise force the upstream connection to close after each request regardless of the keepalive pool.
The practical effect is fewer TCP handshakes under sustained traffic, lower latency per proxied request, and reduced load on upstream servers from constantly accepting and tearing down connections.
More Related questions...