Before we start, let’s discuss on some basics that help you in easy understanding what keep-alive is and why do you need it.
I’m certain that, once you get along with knowing the significance of keep-alive, you will enable it by default and start recommending to others as well.
It is the Load time that matters always. This is a well-known fact obviously. But how do I do that?
There are several techniques for achieving a quicker load time and one of them is by enabling keep-alive.
Keep alive is a communication between the server and the browser with the potential to absolutely reduce the number of requests and helps in a quick load of a web page.
In other terms, it is also called as Persistent Connection.
Technically, a single TCP connection to send and receive multiple HTTP requests. So this would reduce the network latency.
Keep alive utterly reduces the latency of the continuous requests. It also boosts the performance and speeds up the load of the web page.
Further, it does transfer all the requested files through a single connection. By doing so, it avoids the repeated opening and closing of new connections every time unnecessarily.
Kindly refer the pictorial representation on how the files are rendered from the server when requested from.
Enabling Keep-Alive – fetches and serves all the requested files from the server in a single communication itself. (Quicker load time, better performance and even more)
Disabling Keep-Alive – communicates with the server for every requested file and thus allows unnecessary multiple connections. (Load time lag and more)
This eventually overcomes the generation of multiple connections which affects the loading time of the page, as it absorbs maximum resources on the web server.
About enabling keep-alive,
By doing this, the website’s response time would be much faster than you expect it to load.
It is done by requesting through HTTP header.
Simple saying, it is done using “Connection: Keep-Alive” HTTP header.
In this case, all you need to do is a simple change in the settings from “Connection: Closed” to “Connection: Keep-Alive”
However, to enable keep alive, you need to know the web server you are using, as it purely depends on it and also the access you have in it.
Before that, you need to do a primary check on some tools similar to GTmetrix to verify whether or not Keep-Alive is enabled.
Some of the web servers would’ve enabled Keep-Alive by default, just like for the website, you can find in the below screenshot.
Let’s see how to enable keep-alive in Word press.
As discussed above, enabling HTTP keep-alive helps the server to open the connection at once for the entire request. Disabling it makes the server to open the connection for every single request.
Before you start analyzing, I would strongly recommend you to verify, if the keep-alive is enabled or not. As nowadays, most of the hosting providers would’ve activated it already by default.
Though, there are several methods to reduce the load time of a website, enable keep-alive plays a vital role in it.
In fact, most of the active pro-bloggers choose this as an adaptive technique to load their website quickly.
So, let’s enable keep-alive. 🙂
I am going to discuss, how to keep-alive via .htaccess in word press and in various web-servers (Apache, Nginx and Lite speed).
In order to enable keep-alive, you need to insert the following code to your .htaccess file. That’s it. This should do the rest of the trick.
The above code, when added to your .htaccess file, will enable persistent connection and will override any web server.
Turning Keep-Alive ON in Apache is pretty easy if you have access to your Apache configuration file (httpd.conf).
You could access it via SSH (/usr/local/apache/conf/httpd.conf).
Kindly view the applicable sections as indicated below:
Here I have set the number of requests to 75 to be called in a single connection and the keep-alive timeout is initially set to 10 seconds.
It is nothing but the time, the server waits for a newer request.
Alternatively, you could enable keep-alive using cPanel in WHM.
Kindly refer the below screenshot, which explains the step by step process, on how the configuration is set.
All you need to do is to access the Apache configuration.
You could locate it in the Service configuration menu section.
Then by clicking Global configuration, you could be able to see plenty of options there.
As indicated in the below screenshot, turn the Keep-Alive button ON to default.
Having the Keep-Alive settings enabled, we could experience a drastic change in the performance by reducing CPU and network overhead, which is required to close and open connections.
Nginx creates separate connections to the upstream servers by ending all the client connections. It also supports keep-alive for clients and upstream servers.
Keepalive_requests: This indicates the number of requests a client can make using a single keepalive connection only. Normally the default request is 100. It can vary depending upon a large number of requests from a single client.
Keepalive_timeout: This indicates the time duration, until which keep-alive connection is kept open.
Keepalive: This indicates the number of keepalive connections to an upstream server, which is open for every process.
If you want to enable keepalive connections to the upstream servers, you should add the following directives in the configuration.
If you are a Nginx user, you can see that the Keep-Alive is already enabled by default.
But very rarely, in some cases, this setting would have been disabled.
Consider if the settings are disabled and before you do any change to enable it, I would rather suggest that you run a ground check and study the reason why it was disabled.
Once you have made sure, you can enable it using the HttpCoreModule.
All you need to find in there is “keepalive_disable”, as in most of the cases; this would be the main reason why keep alive is not working.
Kindly refer the screenshot below:
Usually, keep-alive is defaulted to ON. But in some cases, your server might use a setting which is “smart keep-alive”.
This setting is found within Litespeed and is normally used for high volume websites.
When you leave this setting the way it is, Google will display that keep-alive is disabled, when analyzed in the PageSpeed tool.
Kindly refer the below screenshot which indicates the smart keep alive and the keep alive options to turn ON and OFF in the Litespeed Web server’s default configuration settings.
It requests for the first file (HTML) with a connection near to the HTTP header.
It turns off the keep alive for certain types of files and does not leave keep alive all the time.
For instance, it enables keep-alive for images, JS, CSS. It could disable it for the other metrics like HTML files.
You need to turn off the smart keep-alive if you would like to make use of keep-alive every time.
Nowadays, many web-owners aren’t showing careful thought about the HTTP connection.
As a result, they mistakenly believe that the Keep-Alive is enabled by default.
This is because of the very fact that most of the users are using shared hosting web servers and without the knowledge of the users, they may close connections.
Even though you have the access to httpd.conf, you have no option other than using the settings which your hosting providers have made.
Your hosting provider usually does this to limit the resource usages.
Thus, it is wise that you smarten up and analyze the settings and make sure that your connections are Keep-alive.
You could cross-check your website’s present load time using Google’s Lighthouse Chrome.