keepalive_timeout

The keepalive_timeout directive sets the timeout for keep-alive connections to upstream servers.

Syntaxkeepalive_timeout timeout;
Defaultnone
Contextupstream
Arguments1

Description

The keepalive_timeout directive in NGINX is essential for managing persistent connections between the NGINX server and upstream servers (like application servers). By configuring this directive, administrators can specify how long an idle connection should remain open before it is closed. This helps optimize server resource consumption and can enhance application performance by reducing the overhead of establishing new TCP connections for each request.

The directive accepts one argument which indicates the duration of the keep-alive timeout in seconds. If the connection is idle for longer than this timeout, it will be closed, allowing the server to reclaim resources. It is particularly useful in high-traffic environments where managing keep-alive connections efficiently can lead to significant performance improvements.

In practice, setting this timeout too low can result in increased connection overhead, as clients may need to frequently re-establish connections. Conversely, setting it too high may lead to resource exhaustion if too many idle connections are kept alive. It is recommended to find a balanced value based on your specific application and traffic patterns.

Config Example

upstream backend {
    keepalive_timeout 65;
}

Ensure the timeout value is appropriate for your application's performance needs; too low may increase latency, too high may exhaust resources.

Not applicable for all upstream configurations; check compatibility if using specific modules.

← Back to all directives