Nginx keepalive_timeout Explained: Configuration, Performance, and Best Practices
Introduction
The keepalive_timeout directive controls how long Nginx keeps an idle HTTP keep-alive connection open before closing it.
Persistent connections allow clients to reuse an existing TCP connection for multiple HTTP requests, reducing connection overhead and improving page load performance. However, keeping connections open for too long also consumes server resources.
Choosing an appropriate timeout value helps balance performance, scalability, and efficient resource utilization.
This guide explains how keepalive_timeout works, recommended values, configuration examples, and common mistakes.
Test Environment
What Is keepalive_timeout?
By default, after Nginx finishes processing an HTTP request, it does not necessarily close the TCP connection immediately.
Instead, it may keep the connection open so the client can send additional requests using the same connection.
The keepalive_timeout directive determines how long an idle connection remains open.
Example:
In this example, Nginx waits up to 65 seconds for another request before closing the connection.
Why Keep-Alive Matters
Without Keep-Alive:
Every request requires establishing a new TCP connection.
With Keep-Alive enabled:
One connection serves multiple requests.
This reduces:
TCP handshake overhead
Network latency
CPU usage
Connection establishment time
How It Works
A typical request flow looks like this:
Check the Current Configuration
The directive is usually configured in:
/etc/nginx/nginx.conf
Search for it:
grep keepalive_timeout /etc/nginx/nginx.conf
Example:
keepalive_timeout 65;
Change keepalive_timeout
Open the configuration file:
sudo nano /etc/nginx/nginx.conf
Example:
Save the file.
Test the Configuration
Always verify the configuration before reloading Nginx.
sudo nginx -t
Expected output:
nginx: the configuration file syntax is ok
nginx: configuration file test is successful
Reload Nginx
Apply the changes.
sudo systemctl reload nginx
Verify:
sudo systemctl status nginx
Expected output:
Active: active (running)
Recommended Values
For most production websites:
keepFor most production websites:
keepalive_timeout 65;
is a sensible default.alive_timeout 65;
is a sensible default.
Relationship with worker_connections
Every idle keep-alive connection still occupies one connection slot.
Example:
Suppose:
4096 Available Connections
If:
3500 Connections
Idle (Keep-Alive)
Only a limited number of connection slots remain available for new clients.
Long keep-alive timeouts can therefore reduce the server's ability to handle bursts of new traffic.
Common Issues
Timeout Too Short
Example:
keepalive_timeout 5;
Possible effects:
Clients reconnect frequently.
More TCP handshakes.
Higher CPU usage.
Increased network overhead.
Timeout Too Long
Example:
keepalive_timeout 300;
Possible effects:
Thousands of idle connections remain open.
More memory usage.
Fewer available connection slots.
Lower scalability during traffic spikes.
Confusing Keep-Alive with Session Duration
keepalive_timeout does not control:
User login sessions.
PHP sessions.
Cookies.
Authentication timeouts.
It only determines how long an idle TCP connection remains open.
Relationship with Other Directives
keepalive_timeout works together with several Nginx settings.
Optimizing these directives together helps improve overall server performance.
Best Practices
Use a moderate timeout for most production environments.
Avoid excessively large timeout values unless required.
Monitor idle connections under real workloads.
Tune keepalive_timeout together with worker_connections.
Validate the configuration using nginx -t before reloading.
Conclusion
The keepalive_timeout directive controls how long Nginx keeps idle HTTP connections open for reuse. Properly configured keep-alive connections reduce connection overhead, improve response times, and enhance the browsing experience.
For most production servers, a value between 30 and 65 seconds provides a good balance between performance and resource efficiency. When tuning Nginx, consider keepalive_timeout alongside worker_processes and worker_connections to achieve optimal scalability and concurrency.
Explore More
› Nginx Explained: Essential Configuration Directives and Best Practices
› Nginx worker_processes Explained: Configuration, Performance, and Best Practices
› Nginx worker_connections Explained: Configuration, Limits, and Best Practices
› Nginx client_max_body_size Explained: Configuration, Upload Limits, and Best Practices
› Best Areas to Live in Bangkok in 2026: A Complete Guide for Expats and Digital Nomads
› Best Coworking Spaces in Bangkok 2026: A Guide for Digital Nomads and Remote Workers
› Why Do People in Southeast Asia Love Iced Drinks? The Climate, Culture, and Science Explained
› Best eSIM for Southeast Asia in 2026: Compare the Top Travel eSIMs