How to Enable Keep Alive in NGINX


how to enable keep alive in nginx

Keep Alive is a useful feature that allows web servers to maintain persistent TCP/IP connections with client web browsers. This not only reduces server load but also improves website performance. Here’s how to enable Keep Alive in NGINX.

 

How to Enable Keep Alive in NGINX

Here are the steps to enable Keep Alive in NGINX. It is quite easy to enable Keep Alive in NGINX.

Keep Alive connections are supported only in HTTP/1.1 protocol.

 

1. Enable Keep Alive in NGINX

We will use the keepalive_timeout directive to enable keep alive

Here’s the syntax


keepalive_timeout timeout [header_timeout];
  • keepalive_timeout – server directive to enable keep alive
  • timeout – number of seconds for keep alive timeout
  • header_timeout (optional) – sets a value in “Keep-Alive: timeout=time” response header field

 

Open your NGINX config file in a text editor


$ sudo vim /etc/nginx/nginx.conf

 

Add the following line to your http/server/upstream context. You can also add it to location context, if you want to enable it only for specific URL locations. In our example, we will add it to the http block

 


http{

 ...

 keepalive_timeout 75;

 ...

}

 

To disable keep alive, you can use


keepalive_timeout 0;

 

By default, keep alive is enabled in most NGINX distributions. If it is not working for you, open the NGINX config file as shown above and look for keepalive_disable directive. It would have disabled keep alive in NGINX. Remove it and restart NGINX server to enable keep alive.

 

2. Enable HTTP/1.1 protocol (optional)

By default, NGINX uses HTTP/1 protocol while talking to upstream servers. So if you have added keep alive directive to the upstream block as shown below


upstream your_upstream {
 keepalive 100;
 server 10.51.11.5:80;
 server 10.51.11.6:80;
 ...
}

We will upgrade HTTP protocol to 1.1 when we enable keep alive.

To do that simply add the following directive in location context that refers to upstream servers, as shown below


location / {
 ...
 proxy_pass http://your_upstream;
 proxy_read_timeout 500;
 proxy_connect_timeout 500;
 ...

 # Default is HTTP/1, keepalive is only enabled in HTTP/1.1
 proxy_http_version 1.1;

 # Remove the Connection header if the client sends it,
 # it could be "close" to close a keepalive connection
 proxy_set_header Connection "";
}

 

3. Reload NGINX Server

That’s it! You have enabled keep alive in NGINX server. Remember to reload/restart NGINX server after you enable keep alive, to apply changes.

$ nginx -s reload

 

4. Test Keep Alive in NGINX

Go to Google Page Speed tool and enter your website. It will analyze your web page and tell you whether Keep Alive is enabled on it or not.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!