How to Enable Keep Alive in WordPress | Apache | Nginx | LiteSpeed?

December 10th, 2018
16 Oct 2018
Kannan Rajendiran
No Comments

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.

Now,

 

What exactly is 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.

 

Why do you need Keep-Alive?

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.

In short:

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)

 

Keep-Alive Disabled

Keep-Alive Disabled

 

Keep-Alive Enabled

Keep-Alive Enabled

 

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,

  • It benefits all the hosting users (Free, Shared, VPS) – It completely reduces the memory and the CPU usage, as the generated HTTP requests are very small.
  • Moreover, it benefits your website for a better ranking and SEO due to excellent performance.
  • And also provides requests delivery via same TCP connection, HTTP pipelining.

By doing this, the website’s response time would be much faster than you expect it to load.

 

How to Enable Keep-Alive?

It is done by requesting through HTTP header.

Simple saying, it is done using “Connection: Keep-Alive” HTTP header.

What if the Keep-Alive is disabled already and you wish to enable it now?

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.

 

Why enable Keep-Alive in WordPress?

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.

You could verify it here, http://www.whatsmyip.org/http-headers/  or https://www.giftofspeed.com/check-keep-alive/

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. 🙂 

Now,

I am going to discuss, how to keep-alive via .htaccess in word press and in various web-servers (Apache, Nginx and Lite speed).

 

How to Enable Keep-Alive in WordPress via .htaccess?

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.

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

The above code, when added to your .htaccess file, will enable persistent connection and will override any web server.

 

How to Enable Keep-Alive in Apache?

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:

#
# 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 75
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 10

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.

 

 

How to Enable Keep-Alive in Nginx?

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.

Client keep-alive

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.

Upstream keep-alive

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.

proxy_http_version 1.1;
proxy_set_header Connection “”;

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:

 

 

How to Enable Keep-Alive in Litespeed?

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.

 

 

Know what smart keep-alive does?

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.

 

Didn’t you enable Keep-Alive by default already?

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.

Kannan Rajendiran

Kannan Rajendiran (Kan) is a technically renowned person and a specialist in providing solutions to almost all type of issues. Love to see clients smile. His expertise in the software industry for more than 15 years helped many clients to provide the best solution, he is a demi-god when website matters. He is the Founder of SpeedBoost :). Extensively attached to the latest technologies and fond of upcoming advancements.
Follow Kannan Rajendiran on

Leave a Reply

Your email address will not be published. Required fields are marked *