keepalive_requests
The `keepalive_requests` directive limits the number of requests that can be sent over a single keepalive connection.
Description
The keepalive_requests directive is configured within the context of an upstream block, allowing you to set a limit on the number of requests that can be sent through a single persistent connection (keepalive) to a backend server. This feature helps prevent a single connection from monopolizing resources and allows for more efficient connection management, particularly in high-load scenarios where many simultaneous requests are typical.
By setting the value of keepalive_requests, you can define how many requests are acceptable on a keepalive connection. For instance, if you set it to 10, once the 10th request is completed, NGINX will close that particular connection and potentially open a new connection for subsequent requests. This not only aids in balancing resource utilization but also ensures that backend servers are not overwhelmed by lingering connections that may hinder performance.
The parameter must be a positive integer, and you should carefully consider the specific needs of your application when choosing its value. A smaller number may lead to more frequent connections but less resource retention, while a larger number can improve throughput but should be monitored for potential resource exhaustion or server strain.
Config Example
upstream backend {
keepalive 16;
keepalive_requests 100;
server backend1.example.com;
server backend2.example.com;
}Setting a very high value may lead to resource exhaustion on backend servers due to too many open connections.
Not using keepalive connections properly with keepalive_requests can cause performance degradation under load.