NGINX Keepalive_Timeout: How to Improve Performance and Reduce Load

Nginx

NGINX is a popular web server that is known for its performance and scalability. One of the features that contributes to NGINX's performance is its Keepalive_Timeout directive.

 

This guide by Fixwebnode will show you how to use the Keepalive_Timeout directive.

 

When a client makes a request to an NGINX server, the server opens a connection to the client. After the request has been processed, the server can either close the connection or keep it open. If the connection is kept open, NGINX can use it to process subsequent requests from the same client. This is called a Keepalive connection.

 

The Keepalive_Timeout directive controls how long NGINX will keep a connection open after a request has been processed. This can help to improve performance by reducing the number of times that NGINX has to open and close connections. It can also help to reduce load on the server by allowing multiple requests to be processed over the same connection.

 

The default Keepalive_Timeout value in NGINX is 60 seconds. This means that NGINX will keep a connection open for 60 seconds after a request has been processed. If another request is received from the same client within 60 seconds, NGINX will use the existing connection to process the request. This can save time and improve performance.

 

However, the Keepalive_Timeout value may need to be adjusted depending on the specific application. For example, if the application is expecting a lot of short-lived requests, then a shorter Keepalive_Timeout value may be appropriate. Conversely, if the application is expecting a lot of long-lived requests, then a longer Keepalive_Timeout value may be appropriate.

 

 

 

What is the syntax for Keepalive_Timeout ?

The syntax for the Keepalive_Timeout directive is:

keepalive_timeout time [connection_header_timeout];

 

The first parameter, time, specifies the number of seconds that NGINX will keep a Keepalive connection open. The default value is 60 seconds.

 

The second parameter, connection_header_timeout, is optional. It specifies the number of seconds that NGINX will wait for a Connection: Keep-Alive header from the client. The default value is the same as the time parameter.

 

For example, the following directive will set the Keepalive_Timeout value to 30 seconds:

keepalive_timeout 30;

 

The following directive will set the Keepalive_Timeout value to 30 seconds and the connection_header_timeout value to 20 seconds:

keepalive_timeout 30 20;

 

 

How to configure Keepalive_Timeout ?

The Keepalive_Timeout directive can be configured in the NGINX configuration file. The following is an example of how to set the Keepalive_Timeout value to 30 seconds:

keepalive_timeout 30;

 

It is important to note that the Keepalive_Timeout directive only applies to connections that are established using the HTTP Keep-Alive protocol. If the client does not support HTTP Keep-Alive, then NGINX will close the connection after each request.

 

A shorter Keepalive_Timeout value will mean that NGINX will close connections more quickly. This can improve performance if the application is expecting a lot of short-lived requests. However, it can also increase the number of connections that NGINX has to open and close, which can impact performance.

 

A longer Keepalive_Timeout value will mean that NGINX will keep connections open for longer. This can improve performance if the application is expecting a lot of long-lived requests. However, it can also increase memory usage.

 

The Keepalive_Timeout directive is a powerful tool that can be used to improve the performance and reduce the load of an NGINX server. By carefully adjusting the Keepalive_Timeout value, you can optimize your application for the best possible performance.

 

 

Key points of NGINX Keepalive_Timeout

  • Keepalive_Timeout: The number of seconds that NGINX will keep a Keepalive connection open.
  • Default value: 60 seconds
  • Impact: A shorter value can improve performance for short-lived requests, but can increase the number of connections that NGINX has to open and close. A longer value can improve performance for long-lived requests, but can increase memory usage.
  • How to set: The Keepalive_Timeout directive can be set in the NGINX configuration file.

 

Here are some additional things to keep in mind when setting the Keepalive_Timeout value:

  • The Keepalive_Timeout value should be long enough to allow multiple requests to be processed over the same connection. However, it should not be so long that it causes excessive memory usage.
  • The Keepalive_Timeout value should be set to a different value for each location in your NGINX configuration file. This will allow you to fine-tune the performance of your application for each type of request.
  • You can use the keepalive_requests directive to control the maximum number of requests that can be processed over a single Keepalive connection. This can help to prevent excessive memory usage.

Conclusion

In fact, the Keepalive_Timeout directive is a powerful tool that can be used to improve the performance and reduce the load of an NGINX server.

 

By carefully adjusting the Keepalive_Timeout value, you can optimize your application for the best possible performance.

 

Here are some key takeaways from this guide:

  • The Keepalive_Timeout directive controls how long NGINX will keep a Keepalive connection open.
  • The default value is 60 seconds.
  • A shorter Keepalive_Timeout value can improve performance for short-lived requests, but can increase the number of connections that NGINX has to open and close.
  • A longer Keepalive_Timeout value can improve performance for long-lived requests, but can increase memory usage.
  • The best Keepalive_Timeout value for your application will depend on the specific application and the traffic patterns.
  • You can experiment with different values to find the one that provides the best performance.

Your Cart