fastcgi_keep_conn

The `fastcgi_keep_conn` directive controls whether to keep the connection to the FastCGI server open after a response is received.

Syntaxfastcgi_keep_conn on | off;
Defaultoff
Contexthttp, server, location
Argumentsflag

Description

The fastcgi_keep_conn directive in NGINX, applicable in the http, server, and location contexts, is a flag that determines whether to maintain a persistent connection between NGINX and the FastCGI server after the completion of a request. By default, once a request is processed, the connection to the FastCGI server is closed. However, setting this directive to 'on' will keep the connection open for future requests, reducing latency for subsequent requests and potentially improving performance in high-traffic environments where multiple requests are sent to the same FastCGI server.

When fastcgi_keep_conn is enabled, connections are reused instead of being continuously opened and closed, which conserves resources and enhances throughput. It is particularly useful for applications hosted in configurations where multiple back-to-back requests are common, like dynamic content generation in PHP or similar technologies. However, keeping connections open can tie up server resources, and thus, should be used judiciously depending on the application’s behavior and the server's overall load capabilities.

Config Example

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_keep_conn on;
}

Enabling fastcgi_keep_conn may lead to resource exhaustion if too many connections are kept open without being utilized.

If the FastCGI server has a limit on the number of simultaneous connections, ensure that enabling this directive does not exceed this limit.

← Back to all directives