How to Enable Keep Alive in NGINX

How to Enable Keep Alive in NGINX

NGINX supports Keep Alive headers that allow you to keep a TCP connection between browser and server alive in order to transfer data. It can even be used to transfer multiple files or large data, without needing a new connection every now and then. It is also known as persistent HTTP connection. Keep Alive connections improve website speed and performance since you can use the same connection to transfer multiple files or large data. In this article, we will learn how to enable Keep Alive in NGINX.

What is Keep Alive in NGINX

Generally, a web browser creates a separate TCP connection for each request. This can be time consuming since each request requires authentication and multiple hand offs. Each web page sends multiple requests and using a separate TCP connection for each request will only slow down your website, especially as your website and visitors grow over time. By using Keep Alive headers, a web server creates and uses a single TCP connection for multiple requests, thereby speeding up your website.

How to Enable Keep Alive in NGINX

By default, Keep Alive header is enabled in NGINX. So you do not need to do anything explicitly. If you find that it is disabled, then look for keepalive_disable directive in HttpCoreModule. If it has a value other than none, change it to none.

Restart NGINX server to apply changes.

$ sudo nginx -t 
$ sudo systemctl restart nginx

How to Check if Keep Alive is Enabled in NGINX

The easiest way to check if Keep Alive is enabled on a web server is to open its web page on your web browser and check the response headers, instead of checking it from the server side. There are many online tools that directly do this and tell you if Keep Alive is enabled in NGINX.

Keep Alive Settings

NGINX provides a couple of useful settings to customize Keep Alive headers.

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

In this article, we have learnt how to enable Keep Alive header in NGINX. In fact, we do not need to do much about it, as it is enabled by default and its default settings are good enough for even high traffic websites.

Also read:

How to Use Git Shallow Clone
How to Redirect 403 to 404 in Apache Server
How to Install Pip in Ubuntu
How to Redirect Subfolder to Root in Apache
How to Block User Agent in Apache

Leave a Reply

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