enable keep alive in nginx

How to Enable Keep Alive in NGINX

Keep Alive header allows the TCP connection between server and browser to remain open so that multiple files can be transferred between them. It is also known as persistent HTTP connection. It improves website speed & performance so it is advisable to enable Keep Alive in your website. Here is how to enable Keep Alive in NGINX.


What is Keep Alive in NGINX

Typically, browser creates a separate TCP connection for each request. This requires authentication and multiple handoffs every single time. Since every web page sends multiple requests, this can slow down your website, especially as the number of website visitors increase. When you enable Keep Alive header in a web server it creates a new connection for the first request and uses the same connection to server subsequent requests. It improves website speed and performance considerably.

Also read: How to Redirect Subdirectory to Root in NGINX

How to Enable Keep Alive in NGINX

By default, Keep Alive header is enabled in NGINX.

If it is still disabled in your web server, look for keepalive_disable value in HttpCoreModule. If it has value as something other than none, change it to none.

Restart NGINX server to apply changes.

$ sudo nginx -t 
$ sudo systemctl restart nginx

Also read : How to Enable Gzip in NGINX

How to Check if Keep Alive header is alive

You can use one of the many online tools available to check if Keep Alive is enabled on your website.

Keep Alive settings

Nginx also provides couple of additional settings to help you customize persistent HTTP connections.

  • KeepAliveRequests – Maximum number of requests a server processes before it creates a new connection. By default, it is set to 100 for optimum performance. If you set it to 0, then it will serve unlimited requests using the connection and will always remain open.
  • KeepAliveTimeout – This is the number of seconds a server has to wait for new requests, before it times out and closes the connection. If you set the value too high, it will lead to higher memory consumption. But if you keep it too low, then it will lead to frequent disconnections.

Bonus read : How to Run WordPress in NGINX

That’s it. As you can see it is very easy to enable Keep Alive header in NGINX, and it offers very powerful benefits.

Leave a Reply

Your email address will not be published. Required fields are marked *