Sub topic 9
Reusing Upstream Connections with Keepalive
Assume you understand HTTP requests and responses, TCP ports, Nginx server and location blocks, and the basic upstream directive. This chapter focuses on connections between Nginx and application servers.
Why upstream keepalive matters
A reverse proxy creates a connection to an upstream server, sends the request, reads the response, and normally closes the connection. Opening a TCP connection for every request adds work:
- Nginx performs a TCP handshake.
- The operating system allocates connection resources.
- The application server accepts the connection.
- The connection closes after the response.
Upstream keepalive lets Nginx reuse idle connections. Nginx keeps a cache of connections for each worker process. A later request can use an existing connection instead of opening a new one.
This reduces connection setup time and lowers CPU and socket usage. It does not remove the need to size the application server or database. It also does not mean that every request uses one permanent connection.
Configure the upstream pool
Place keepalive inside the upstream block. Its value is the number of idle connections that each Nginx worker may keep for that upstream group.
upstream app_backend {
server 10.0.0.11:8080;
server 10.0.0.12:8080;
keepalive 32;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}The two proxy settings are important:
proxy_http_version 1.1enables HTTP/1.1 to the upstream.proxy_set_header Connection ""removes the hop-by-hopConnectionheader. This allows the upstream connection to remain persistent.
The keepalive value is a cache size, not a maximum number of active requests. Nginx can create additional temporary connections when all cached connections are busy. The cache is also per worker. For example, four workers with keepalive 32 can retain up to about 128 idle connections across the group.
| Setting | Meaning |
|---|---|
| `keepalive 32` | Keep up to 32 idle upstream connections per worker |
| `proxy_http_version 1.1` | Use HTTP/1.1 for proxy-to-upstream requests |
| `proxy_set_header Connection ""` | Permit persistent upstream connections |
Tune the pool safely
A small pool may provide little reuse during busy periods. A large pool may leave many idle sockets open on Nginx and the application servers. Start with a modest value, measure connection reuse, and increase it only when connection creation is a bottleneck.
Recent Nginx versions also support limits such as keepalive_requests, which limits requests per reused connection, and keepalive_time, which limits how long a connection may be reused. These can help avoid very long-lived connections, but confirm that your installed Nginx version supports the directives before adding them.
The upstream application must support persistent HTTP connections. Some servers close connections after every response, and Nginx cannot reuse those connections.
Validate and observe
Test the configuration before reloading Nginx:
sudo nginx -t && sudo systemctl reload nginxWatch application-server connection counts and Nginx upstream timings. If connections are frequently closed by the upstream, check application keepalive settings, idle timeouts, firewalls, and load balancers between Nginx and the application.
Exercise
Four Nginx workers use an upstream block with keepalive 16. What does that value describe, and what two proxy settings are normally needed for reuse?
Show answer
It describes up to 16 cached idle connections per worker for that upstream group, or about 64 across four workers. Use proxy_http_version 1.1 and clear the hop-by-hop header with proxy_set_header Connection "".