Skip to content

Improving Website Performance – Enabling Keep-Alive

How does Keep-Alive work?

In this tutorial, you will learn four different methods to enable keep-alive. Keep-Alive allows a visitor’s browser to download all the content (such as JavaScript, CSS, images, videos, etc.) through a persistent TCP connection instead of making different requests for each file. This will provide a speed and performance boost as your visitor’s browser can get everything through a single, persistent HTTP connection. In short, Keep-Alive is a communication pattern between a web server and a browser with the potential to reduce request amount and speed up a web page drastically. Here is a picture that will help in understanding the difference and benefits of Keep-Alive:

Advantages of enabling Keep-Alive:

  • Keep-Alive reduces CPU and memory usage due to a smaller amount of generated HTTP requests. This will benefit all hosting platform users (free hosting, shared hosting, VPS)
  • Enabling Keep-Alive provides HTTP pipelining (delivery of requests via the same TCP connection)
  • HTTPS requests need more CPU time and resources. Keep-Alive will significantly benefit your website if you use HTTPS and SSL.
  • Reduced latency and an overall increase in loading speed and performance.
  • All modern browsers support Keep-Alive
  • Enabling Keep-Alive will also benefit your website in terms of SEO and ranking due to better performance.

In short, Keep-Alive is a great way to reduce resource usage while simultaneously increasing your website speed.

What you’ll need

Before you begin this guide, you’ll need the following:

  • Access to .htaccess file
  • Access to httpd.conf (optional)
  • Access to HttpCoreModule (optional)

Step 1 — Analyzing your site

Firstly, you should analyze a website with a tool such as GTMetrix to determine whether Keep-Alive is enabled or disabled on your server. Here are the results after analysis of a test page:

Keep-Alive not fully working

On some servers or hosting providers, Keep-Alive is enabled by default. If your analysis gives a 100% score, nothing more needs to be done.

Step 2 — Enabling Keep-Alive

There are several ways to turn on Keep-Alive, depending on your server or hosting provider. Here are a few options:

Option 1 – Editing .htaccess file

Adding the following code to your .htaccess file to enable Keep-Alive should do the trick. Enabling Keep-Alive using .htaccess will override any server settings and allow the persistent connection.

<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>

This method should work on most Linux-shared hosting providers. If you do not know where to find .htaccess, try referring to this tutorial.

Option 2 – Enabling Keep-Alive in Apache via httpd.conf file

You can enable the extension from there if you have access to the Apache config file. Here is what the configuration should look like:

#
# 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 50

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 10
  • The KeepAlive On section enables the extension.
  • The MaxKeepAliveRequests section sets the max number of allowed requests for a single connection. Fifty requests for one connection is a great place to start.
  • The keepAliveTimeout section specifies how long the server waits for new requests from a client. It is recommended to start with a smaller value, such as 5 or 10 seconds, and increase it later if required. Settings this value too high can cause a high server load.

If you cannot locate the httpd.conf file, run the following command in the command line:

find / -name httpd.conf

Option 3 – Enabling Keep-Alive in NGINX

Keep-Alive is enabled by default on NGINX; however, in some cases, it can be disabled. You can allow it to using HttpCoreModule. Look for the value keepalive_disable, which is, in many cases, why Keep-Alive is not working. Before enabling it, make sure to know the reason why it’s disabled in the first place before attempting any changes.

Option 4 – Windows Server (IIS)

Using a Windows-based server, you can easily enable the Keep-Alive extension using the command line.

The following command will enable it:

appcmd set config /section:httpProtocol /allowKeepAlive:true

And if you wish to disable it, use the following:

appcmd set config /section:httpProtocol /allowKeepAlive:false

You can also refer to an official tutorial from Microsoft for a few extra options.
a

Step 3 — Testing the changes

Once Keep-Alive is fully enabled, run another scan with GTMetrix or any other website performance analysis tool to see if everything is working. Here are the results after Keep-Alive has been turned on:

Keep-Alive fully functional

It is also possible to check whether Keep-Alive is functioning by checking your HTTP header. This can be done via the terminal using the following command:

curl -I http://example.com/example.php

Here is an example:

curl -i http://hostinger.com/index.php

The results are:

HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Server: nginx
Date: Fri, 23 Dec 2016 18:58:14 GMT
Content-Type: text/html
Content-Length: 178
Location: https://www.hostinger.com/index.php

The Connection: keep-alive part signifies that Keep-Alive is functional.

Conclusion

In summary, enabling Keep-Alive for your website is a great way to improve speed and performance. The persistent TCP connection will ensure faster load times and higher efficiency, thus keeping your visitors happy.