Prev Next

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.

What does the number in the keepalive directive actually represent?
Why must proxy_set_header Connection ""; be included when using upstream keepalive?

More Related questions...

What is NGINX? What is the purpose of NGINX as a reverse proxy? What are the main features of NGINX? What are the types of context blocks in NGINX configuration? Define upstream in NGINX? What is the master-worker process model in NGINX? List common NGINX configuration directives? How do you install NGINX on Ubuntu? How do you start, stop, and reload NGINX? What is the nginx.conf file? What are server blocks in NGINX? What is a location block in NGINX? How do you serve static files with NGINX? What is load balancing in NGINX? What are the types of load balancing methods in NGINX? Define a virtual host in NGINX? What is SSL termination in NGINX? How do you enable Gzip compression in NGINX? What is caching in NGINX? What is the purpose of the events block in NGINX? What is the difference between NGINX and Apache HTTP Server? What is the difference between proxy_pass and rewrite in NGINX? What is the difference between a forward proxy and a reverse proxy? How does NGINX handle concurrent client connections? How does NGINX's event-driven architecture work? Why is NGINX considered more performant than process-per-connection servers? Why do we use upstream blocks with multiple servers? When should you use NGINX purely as a reverse proxy versus also as a load balancer? When would you choose round robin over least_conn load balancing? What happens when an upstream server fails a health check? How is SSL/TLS termination configured in NGINX? How can you optimize NGINX for high-traffic websites? How do you troubleshoot a 502 Bad Gateway error in NGINX? How do you troubleshoot high memory usage in NGINX worker processes? Explain the lifecycle of an HTTP request in NGINX? Explain the execution flow of NGINX's request processing phases? Explain the internal working of NGINX worker processes and the event loop? Why doesn't NGINX use a thread-per-request model like Apache's prefork MPM? Why should location block matching order matter between regex and prefix matches? What is the difference between try_files and rewrite directives? How does NGINX handle SSL session caching for performance? How can you optimize NGINX buffer settings for large file uploads? What is the difference between active and passive health checks in NGINX? How do you troubleshoot NGINX configuration errors before reloading? Why is the worker_connections directive important for concurrency limits? When should you use NGINX caching instead of a dedicated CDN? What is the difference between limit_req and limit_conn for rate limiting? How does NGINX handle WebSocket proxying? Explain the internal working of NGINX's keepalive connection pooling to upstream servers? Why do we use try_files with a fallback to PHP-FPM in WordPress-style setups?
Show more question and Answers...


Comments & Discussions