Improving Website Performance – Enabling Keep-Alive

Improving Website Performance – Enabling Keep-Alive

How does Keep-Alive work?

In this tutorial you will learn 4 different methods to enable keep-alive. Keep-Alive allows a visitor’s browser to download all the content (such as JavaScript, CSS, images, videos and 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 will be able to 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 drastically reduce request amount and speed up a web page. Here is a picture that will help in understanding the difference and benefits of Keep-Alive:

How Keep-Alive Works

Advantages of enabling Keep-Alive:

  • Keep-Alive reduces the usage of CPU and memory 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 same TCP connection)
  • HTTPS requests need more CPU time and resources. Keep-Alive will greatly benefit your website if you use HTTPS and SSL.
  • Reduced latency and overall increase in loading speed and performance.
  • Keep-Alive is supported by all modern browsers
  • Enabling Keep-Alive will also benefit your website in terms of SEO and ranking due to better site performance.

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

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, there is nothing more that needs to be done.

Step 2 — Enabling Keep-Alive

There are several ways to turn on Keep-Alive and it all depends on your server or hosting provider. Here are a few options:

Option 1 – Editing .htaccess file

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

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

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

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

If you have access to the Apache config file, you can enable the extension from there. 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
  • KeepAlive On section enables the extension.
  • MaxKeepAliveRequests section sets the max number of allowed requests for a single connection. 50 requests for one connection is a great place to start.
  • 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 increasing it later if required. Settings this value too high can cause 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 enable it using HttpCoreModule. Look for the value keepalive_disable, which is in a lot of cases the reason 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)

If you are 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:

appcmd set config /section:httpProtocol /allowKeepAlive:false

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

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 terminal using the following command:

curl -I

Here is an example:

curl -i

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

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


To sum up, 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.

If you wish to improve your website even further, these articles will also help:

Improving website performance: Leveraging Browser Cache

Improving website performance: Gzip Compression

Improving website performance: Serving Scaled Images

Improving website performance: Minifying CSS, HTML and JavaScript

Improving website performance: Using Progressive JPEG images

The Author


Domantas G. / @domantas

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.


May Weather Reply

October 29 2018

Thank you for the terrific article! My site went from 5-8 seconds down to 0-2 fully loaded. I did have an issue where adding the code to .htaccess didn't work but found a fix by installing 'Litespead Cache', editing my wp-config.php and then adding the code through admin dashboard.


    Gediminas B.

    Replied on November 06 2018

    That's awesome, May! Sounds like a really huge website speed improvement, nicely done ;)

Leave a reply




This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in Website