Home Nginx How to Enable Nginx Keepalive Connections

How to Enable Nginx Keepalive Connections

Are you looking for a way to enhance the performance and reduce the latency of your NGINX web server? If that’s the case, then you can enable the “Keepalive” connections to enhance performance and minimize the latency.

In this post, we will cover the process of enabling NGINX Keepalive connections. Additionally, we will delve into an understanding of NGINX Keepalive and the reason why we enable it.

What is Keepalive in NGINX?

Keepalive Connections are like the superheroes of web server performance! They work behind the scenes to reduce delays and load your web pages faster. Let us understand this in simple terms.

When your web browser talks to a server using HTTP, it usually creates a new connection for each request and response. But here’s the catch – creating and closing these connections can be time-consuming and inefficient, especially for complex web pages with lots of elements.

That’s where Keepalive Connections come to the rescue! They’re like friendly handshakes that keep the TCP connection between your browser and the server open even after a transaction is done. So, the next time your browser needs to fetch something from the server, it can use this existing connection instead of establishing a new one.

By using the Keepalive Connections, your web browsing experience gets a performance boost – pages load quicker, and you don’t have to wait as much. NGINX web servers are smart enough to handle Keepalive Connections, and you can tweak the timeout settings to suit your needs.

Enable NGINX Keepalive Connections

To enable keepalive connections, it’s essential to decide whether it’s for clients or upstream servers. As Keepalive connections can play a vital role in performance by reducing the CPU and network overhead required to open and close connections.

Here the NGINX takes charge by terminating the client connections and establishing separate connections to upstream servers, while also facilitating keepalives for both clients and upstream servers.

Note: Upstream server refers to a group of backend servers that are configured to handle client requests.

So to enable a keepalive connection for clients or upstream servers, you need to configure the “nginx.conf” located at the “/etc/nginx/” directory by using the nano editor.

$ sudo nano /etc/nginx/nginx.conf

After opening the configuration file, now you need to decide whether you are making the connection keep alive for the client or upstream.

Enable Keepalive Connections for Client Side in Nginx

To enable the keepalive connection for the client side, you need to use “keepalive_requests” and “keepalive_timeout” directives in the http, server, or location block in the “nginx.conf” file.

Here the “keepalive_requests”, determines the number of requests a client can make over a single keepalive connection. While the “keepalive_timeout”, defines the time for which the idle keepalive connection will stay open.

The syntax to use both directives is given below:

http {
      keepalive_timeout 60s;
      keepalive_requests 1000;
}

Note: When adjusting keep-alive connections on your web server, ensure the settings suit your needs and be mindful of other relevant configurations that can impact overall performance.

Enable Keepalive Connections for Upstream in Nginx

How to Enable Keepalive Connections For upstream in Nginx?
To enable the keepalive connections for the upstream server, you can use “keepalive” directives in the upstream block as shown below.

upstream UbuntuMint_upstream {
        keepalive 10;
}

The Keepalive connections for the “UbuntuMint_upstream” upstream (a group of servers to which Nginx can distribute incoming requests) will stay alive for 10 seconds.

Here the important thing is that you need to make sure the usage of the “proxy_http_version” and “proxy_set_header” directives in the configuration.

In the end, the configuration for upstream will look like this:

upstream UbuntuMint_upstream {
	server 127.0.0.1:8080;
	keepalive 16;
}

server {
	...

	location /http/ {
    	proxy_pass http://UbuntuMint_upstream;
    	proxy_http_version 1.1;
    	proxy_set_header Connection "";
    	...
	}
}

The Keepalive connection will stay alive for 16 seconds for the UbuntuMint_upstream backend server.

After making your desired changes in the configuration file, let’s test its syntax by typing this command:

$ sudo nginx -t

If there are no errors in the syntax, reload NGINX to apply the changes by executing the command stated below:

$ sudo service nginx reload

If you want to do any other configurations of NGINX, you can follow these related articles:

Conclusion

This helps reduce the overhead of establishing new connections for subsequent requests from the same client, leading to improved performance and reduced latency.

However, remember that enabling keepalive connections also consumes server resources, so it’s essential to set an appropriate keepalive timeout value based on your server’s requirements and load.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

2 thoughts on “How to Enable Nginx Keepalive Connections”

  1. This post is wrong because the keepalive directive in upstream does not define the number of seconds (time) but the number of keepalive connections to the backend.

    Reply
    • @Gamael,

      Yes, you are right, the upstream block control the maximum number of idle keep-alive connections to a backend server.

      However, it’s important to note that the “keepalive_timeout” directive is the one that specifically defines the time (in seconds) during which an idle keep-alive connection will remain open.

      Reply

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.