November 25, 2019
4min Read
Domantas G.
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:
In short, Keep-Alive is a great way to reduce your resource usage while increasing your website speed at the same time.
Before you begin this guide you’ll need the following:
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:
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.
There are several ways to turn on Keep-Alive and it all depends on your server or hosting provider. Here are a few options:
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
</ifModule>
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.
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
If you cannot locate the httpd.conf file, run the following command in the command line:
find / -name httpd.conf
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.
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.
a
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:
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 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.
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
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 ;)