keepalive_time
The keepalive_time directive sets the time during which an existing connection in the upstream can remain idle before it is closed.
Description
The keepalive_time directive specifies the maximum duration that an idle keep-alive connection to an upstream server should be maintained before being closed. This helps in optimizing resource usage by preventing unused connections from lingering indefinitely, thereby allowing for a better management of the maximum connections available to the server. When the time specified by keepalive_time elapses, the connection will be closed, which can help to free up resources for new requests. This directive is particularly useful in scenarios dealing with heavy traffic and many short-lived connections, ensuring that connections are reused efficiently without overwhelming the server’s resources.
By using the keepalive_time directive, administrators can balance load and maintain connection state in a way that aligns with their application’s needs. It's important to consider the performance implications when determining the value to set for this directive—too short a timeout may increase connection overhead, while too long a timeout may lead to under-utilization of resources. The directive accepts a single argument, which represents the duration in seconds for which the connection will be kept alive during idle times.
Config Example
upstream backend {
keepalive_time 60;
}Setting a very low keepalive_time may lead to increased latency due to frequent opening and closing of connections.
If not set properly, it may lead to either resource exhaustion or under-utilization based on the workload.
Ensure that the timeout set aligns with client expectations for connection stability.