What are the benefits of keepalive in Nginx
Creating new TCP connections can consume a lot of resources such as memory and CPU usage. However, keeping your connection alive in Nginx can reduce this usage. That’s another reason why keepalive for HTTPS connections is highly recommended. Enabling the keepalive can assist you in improving the user experience and performance of your website. It permits the browser to load page content with a single TCP connection. Another benefit that I would like to mention here is that it also improves the web page speed due to its ability to deliver several files over the same connection, reducing latency and speeding up the loading of the web pages.
How to use keepalive in Nginx
Now, let’s check how we can utilize keepalive connection in Nginx. For this, open up the Nginx configuration file in your nano editor by writing out the below-given in the terminal:
The Nginx configuration file will look like this:
Keepalive connections can improve speed by lowering the network and CPU overhead associated with closing and opening the connections. Keepalives are supported by Nginx for both upstream servers and clients. All client connections are terminated, and after that, Nginx establishes independent connections to the upstream servers.
What is keepalive_timeout in Nginx
The keepalive_timeout value in the Nginx configuration file indicates how long the server has to wait to get requests from a client. In another way, we can say that it indicates the number of seconds an idle keepalive connection will stay open. It is best to leave the idle connection open for about six to ten seconds. If the keepalive_timeout value is set too high, the server will be overloaded, and RAM resources will be wasted. Syntax of keepalive_timeout in Nginx is mentioned below:
Context of keepalive_timeout in Nginx: server, http, and location
Now, in the Nginx configuration file, we are setting the keepalive_timeout value to 10 seconds:
What is keepalive_disable in Nginx
The keepalive_disable option permits you to disable the keepalive feature for specific browser families. The syntax of keepalive_disable in Nginx is:
Context of keepalive_disable in Nginx: server, http, and location
Now, we will only disable the “msie6” browser to utilize the keepalive feature:
What is keepalive_requests in Nginx
Over a single keepalive connection, the keepalive_requests value indicates the maximum number of requests it can handle. The default value for keepalive_requests is 100. However, higher values can be set, which tends to be useful in testing with a load generation utility that sends many requests from a single client. The syntax of keepalive_requests in Nginx is:
Context of keepalive_requests in Nginx: server, http, and location
Now, we will set “100000” as a maximum number of requests that can be served over a single connection:
Keepalive connections in upstream servers
You can enable the keepalive connections for upstream servers, limiting the number of idle keepalive connections stored in each worker process cache. When the upstream server reaches the keepalive value, the connections used the least are closed.
Add the below-given lines in your Nginx configuration file for setting the value for your upstream server:
Here, we have added “16” as the number of idle keepalive connections to an upstream server that remains open. There is no such thing as a default value of keepalive for upstream servers:
To save any of the changes you have made in the Nginx configuration file, press “CTRL+O”:
After that, restart the Nginx service on your system:
Nginx is an efficient HTTP load balancer that can be utilized in various deployment situations, and the feature that assists Nginx in performing this functionality is keepalive. Both upstream servers and clients support keepalive connections. In this post, you have learned about what is keepalive in Nginx. Moreover, we have also explained the keepalive_requests, keepalive_timeout, keepalive_disable directives and their usage in Nginx.