How does Keep-Alive work?
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:
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 </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.
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:
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
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: