keepalive
The `keepalive` directive enables HTTP keepalive connections to be maintained in an upstream context.
Description
The keepalive directive in NGINX is utilized within the context of upstream blocks to control the maximum number of idle keepalive connections to an upstream server. When keepalive connections are enabled, NGINX can re-use existing connections to upstream servers for multiple requests, instead of opening new connections each time, which can improve performance and reduce latency. This functionality is particularly beneficial when dealing with high traffic loads, as it reduces the overhead associated with frequent connection establishment and teardown.
The directive accepts a single argument, which specifies the maximum number of idle connections that can be maintained to the upstream servers. If a request is made that exceeds this number, NGINX will close the least recently used idle connection to make space for a new one. It is important to tune this parameter based on the server's resources and the expected traffic patterns. Setting this value too low may result in inefficiencies due to frequent reconnects, while setting it too high could lead to resource exhaustion.
Overall, using the keepalive directive is a recommended practice in NGINX configurations that involve upstream server communication, as it facilitates better resource utilization and response times. Monitoring the performance impacts after changes is also prudent to ensure optimal settings are achieved.
Config Example
upstream backend {
server backend1.example.com;
server backend2.example.com;
keepalive 16;
}Ensure that upstream servers support keepalive connections; otherwise, it may lead to dropped connections.
A value that is too high may exhaust system resources, leading to performance issues.