Syntax: | keepalive |
---|---|
Default: | — |
Context: | upstream |
This directive appeared in version 1.1.4.
Activates the cache for connections to upstream servers.
The connections
parameter sets the maximum number of idle keepalive connections to upstream servers that are preserved in the cache of each worker process. When this number is exceeded, the least recently used connections are closed.
It should be particularly noted that thekeepalive
directive does not limit the total number of connections to upstream servers that an nginx worker process can open. Theconnections
parameter should be set to a number small enough to let upstream servers process new incoming connections as well.
When using load balancing methods other than the default round-robin method, it is necessary to activate them before the keepalive
directive.
Example configuration of memcached upstream with keepalive connections:
upstream memcached_backend { server 127.0.0.1:11211; server 10.0.0.2:11211; keepalive 32; } server { ... location /memcached/ { set $memcached_key $uri; memcached_pass memcached_backend; } }
For HTTP, the proxy_http_version directive should be set to “1.1
” and the “Connection” header field should be cleared:
upstream http_backend { server 127.0.0.1:8080; keepalive 16; } server { ... location /http/ { proxy_pass http://http_backend; proxy_http_version 1.1; proxy_set_header Connection ""; ... } }
Alternatively, HTTP/1.0 persistent connections can be used by passing the “Connection: Keep-Alive” header field to an upstream server, though this method is not recommended.
For FastCGI servers, it is required to set fastcgi_keep_conn for keepalive connections to work:
upstream fastcgi_backend { server 127.0.0.1:9000; keepalive 8; } server { ... location /fastcgi/ { fastcgi_pass fastcgi_backend; fastcgi_keep_conn on; ... } }
SCGI and uwsgi protocols do not have a notion of keepalive connections.
Syntax: | keepalive_requests |
---|---|
Default: |
keepalive_requests 1000; |
Context: | upstream |
This directive appeared in version 1.15.3.
Sets the maximum number of requests that can be served through one keepalive connection. After the maximum number of requests is made, the connection is closed.
Closing connections periodically is necessary to free per-connection memory allocations. Therefore, using too high maximum number of requests could result in excessive memory usage and not recommended.
Prior to version 1.19.10, the default value was 100.
Syntax: | keepalive_time |
---|---|
Default: |
keepalive_time 1h; |
Context: | upstream |
This directive appeared in version 1.19.10.
Limits the maximum time during which requests can be processed through one keepalive connection. After this time is reached, the connection is closed following the subsequent request processing.
Syntax: | keepalive_timeout |
---|---|
Default: |
keepalive_timeout 60s; |
Context: | upstream |
This directive appeared in version 1.15.3.
Sets a timeout during which an idle keepalive connection to an upstream server will stay open.
Syntax: | ntlm; |
---|---|
Default: | — |
Context: | upstream |
This directive appeared in version 1.9.2.
Allows proxying requests with NTLM Authentication. The upstream connection is bound to the client connection once the client sends a request with the “Authorization” header field value starting with “Negotiate
” or “NTLM
”. Further client requests will be proxied through the same upstream connection, keeping the authentication context.
In order for NTLM authentication to work, it is necessary to enable keepalive connections to upstream servers. The proxy_http_version directive should be set to “1.1
” and the “Connection” header field should be cleared:
upstream http_backend { server 127.0.0.1:8080; ntlm; } server { ... location /http/ { proxy_pass http://http_backend; proxy_http_version 1.1; proxy_set_header Connection ""; ... } }
When using load balancer methods other than the default round-robin method, it is necessary to activate them before the ntlm
directive.
This directive is available as part of our commercial subscription.