How to increase website speed: Enable Keep Alive

by Anoop Naik on 7th April, 2014 last updated 23rd July, 2017


Everyone wants websites to load faster. Faster website loading times equals happy visitors and also keeps the search engine bot happy. This is a part of a series on "how to increase website speed" and have faster loading time.

Today, we will look into how enabling Keep-Alive requests will improve our page loading time. Before we dig deep into how we can achieve faster speed, lets understand what Keep-Alive is.

How to increase website speed by enabling Keep Alive?

Lets take an example of a supermarket to make the concept of Keep-Alive requests more clear. Just imagine that you are going to a supermarket to buy some daily needs and that supermarket has come up with a new rule which allows you to buy only one item on each visit(no supermarket has such kinda rule but lets consider). That would mean if you wanted to buy 5 items, you would have to visit the supermarket 5 times. Crazy right?

Default behavior of most shared hosting servers are similar to the weird supermarket we described above. When using such shared hosting servers, the web browser has to create a new connection to communicate and receive a file from the server.

First, it requests the HTML file using a new connection and the connection closes on receiving the file. The browser then reads the HTML file and checks if any other files are required to render or show the complete page. After analyzing the files needed, it creates a new connection to request each of those files. Lets say your HTML had 3 stylesheets, 2 Javascript and 5 image files, that would mean additional 10 requests to get these resources.

So, creating numerous connections for resources from a single webpage slows the loading time as well as utilizes more resources(sockets) on the server. We can overcome this and transfer all the files necessary for rendering a page through a single connection by enabling the Keep-Alive.

How to know if Keep-Alive is enabled?

The first step is to check whether Keep-Alive is enabled. Nowadays, some servers enable them by default but you need to make sure that they are enabled for sure. To check if they are enabled, just follow the following steps:

  • Open the developer tools of your browser.
  • Load your website and then hop onto the Network tab in the developer tools.
  • Click on any file on the left to display its headers.
  • In the Response section of the request, check for Connection attribute. If you see "Connection: close", Keep-Alive is disabled. "Connection: keep-alive" is what you will be aiming for.

There is another simpler way to know whether Keep-Alive is enabled. Just do a website speed test on WebPageTest and it will give you an "A" if all of your files are delivered through Keep-Alive.

How to enable Keep-Alive?

If Keep-Alive was not enabled, lets quickly go ahead and enable them. To enable the Keep-Alive, you need to change the HTTP headers of your requests and to do that you need to do some changes in the .htaccess file. The htaccess rule to enable is given below but be careful while editing the .htaccess file since it can mess up your whole website if you do not do it right.

Header set Connection keep-alive

Modifying Keep-Alive Settings

By default, 100 requests can be made on a single connection. If your webpage has more than 100 resources that needs to be loaded, then you wanna change the default value of MaxKeepAliveRequests so that all your files are transferred through a single connection. You can also change the time between two persistent connections. You can edit these values in the Apache config file, httpd-default.conf.

# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
KeepAlive On

# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
MaxKeepAliveRequests 100

# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
KeepAliveTimeout 5

Enabling Keep-Alive on NGINX server

Keep-Alive will be enabled on NGINX servers by default but by some reason if it has not been enabled, you can try removing the "keepalive_disable" directive in nginx.conf and .conf files located in conf.d folder or set it to "none" as mentioned in the Nginx HttpCoreModule Documentation.

That wraps the tutorial on how to increase website speed using Keep-Alive Requests.

(Note: This technique can be used to speed up your wordpress blog or any other normal website)

Leave a Reply