Every request served by NGINX goes through various timeouts. These timeouts, if optimized, can have a considerable impact on the server's performance. Post timeout, the resources are released and, thus, can be utilized for other requests. In the following section, we will configure various timeout directives provided by NGINX.
HTTP is a stateless, request-response-based protocol, where the client opens a TCP connection with the server, sends the request, receives the response, and then the server closes the connection to release the resources.
Now, if the client makes multiple requests to the server, for every request, the client opens a connection, transfers the data, and then the connection is closed by the server. This is quite inefficient if the web pages contain a lot of resources as the browser will open a connection for every resource.
HTTP has the keepalive mode, which instructs the server to hold a TCP connection open once the request has been completed. If the client needs to make another request, it can use this idle keepalive connection rather than creating a new TCP connection. Such a connection can be terminated when the client feels it is no longer required or the server determines that there been no activity over the connection for a certain interval of time (timeout). Modern browsers usually open multiple keepalive connections and use them to serve content.
Since keepalive connections are kept open for an interval of time they have a cost of increased resource utilization. The keepalive timeout should be optimal depending on your website and traffic load. This will improve the site's performance. If the timeout is quite large, then it can have a negative impact on performance during high traffic loads.
NGINX has a couple of directives to configure keepalive connections.
This directive configures the timeout for keepalive connections. By default, the value is set to 75
seconds. The value 0
disables keepalive connections. The directive is available under the http
, server
, and location
sections of an NGINX configuration. Here's an example:
http{ keepalive_timeout 20s; }
This directive also has a second optional time argument, which it sends back in the Keep-Alive: timeout=time
response header field. The header field is recognized by certain browsers, such as Mozilla, Konqueror, and so on, and could have a different value compared to the timeout. Here's an example:
http{ keepalive_timeout 20s 18s; } $ curl -I http://192.168.2.100/hello …...... Connection: keep-alive Keep-Alive: timeout=18 …....
This directive configures the total number of requests allowed over a keepalive connection. After the maximum number of requests are made, the server closes the connection.
By default, the directive sets 100
as the maximum number of allowed requests. The directive is available under the http
, server
, and location
sections of an NGINX configuration. Here's an example:
http{ keepalive_requests 20; }
This directive sets a timeout for transmitting data to the client. This timeout does not apply to the entire transfer but only between two successive write operations. If the timeout expires, NGINX will close the connection.
By default, the directive sets the value as 60
seconds. The directive is available under the http
, server
, and location
sections of an NGINX configuration. Here's an example:
server{ send_timeout 30s; }
This directive sets a timeout to send the request body from the client. The timeout does not apply to the complete request body but only to two successive read operations. If the client does not send anything within the set time interval, NGINX sends back the HTTP 408(Request Timed Out)
error.
By default, this directive sets the value as 60
seconds. The directive is available under the http
, server
, and location
sections of an NGINX configuration. Here's an example:
server{ client_body_timeout 30s; }
The directive sets a timeout to send the complete request header from the client. If the client does not send the complete header information within the set time interval, NGINX sends back the HTTP 408(Request Timed Out)
error.
By default, the directive sets the value 60
seconds. The directive is available under the http
and server
sections of the NGINX configuration. Here's an example:
server{ client_header_timeout 30s; }
18.191.235.176