Nginx keepalive_timeout Explained: Configuration, Performance, and Best Practices

Published: 2026-07-14

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

Operating System : AlmaLinux 10
PHP Version      : PHP 8.3
Web Server       : Nginx
PHP-FPM          : Enabled
Architecture     : x86_64
Last Tested      : July 2026

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:

http {
    keepalive_timeout 65;
}

In this example, Nginx waits up to 65 seconds for another request before closing the connection.

Why Keep-Alive Matters

Without Keep-Alive:

Browser
Request 1
    │
Connect
    │
Response
    │
Disconnect
Request 2
    │
Connect
    │
Response
    │
Disconnect
Request 3
    │
Connect
    │
Response
    │
Disconnect

Every request requires establishing a new TCP connection.

With Keep-Alive enabled:

Browser
Connect
   │
   ├── Request 1
   ├── Request 2
   ├── Request 3
   └── Request 4
   │
Disconnect

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:

Client
   │
   ▼
Open TCP Connection
   │
   ▼
HTTP Request
   │
   ▼
Nginx Response
   │
   ▼
Wait for Next Request
   │
   ├── New Request Arrives
   │         │
   │         ▼
   │    Reuse Connection
   │
   └── Timeout Reached
             │
             ▼
      Close Connection

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:

http {
    keepalive_timeout 30;
}

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

| Server Type      | Suggested Value |
| ---------------- | --------------- |
| Personal website |   30–60 seconds |
| Business website |   30–65 seconds |
| WordPress        |   30–65 seconds |
| API server       |   15–30 seconds |
| Reverse proxy    |   30–75 seconds |

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:

worker_processes auto;
events {
    worker_connections 4096;
}
http {
    keepalive_timeout 65;
}

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.

Nginx
   │
   ├── worker_processes
   ├── worker_connections
   ├── keepalive_timeout
   ├── sendfile
   ├── gzip
   └── client_max_body_size

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

Technology Guides →

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

Southeast Asia Insights →

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