How to enable the Keep-Alive extension?

Enabling Keep-Alive can help website performance and provide a better user experience. Allows the visitor’s browser to reuse a single TCP connection to load the page content.

Keep-Alive is usually enabled by default on your origin server. But if it is disabled, there are some solutions to activate it on your own.

This tutorial will show you the steps to enable Keep-Alive and the files you need to prepare beforehand. We’ll also go over the reasons why you should enable Keep-Alive.

What is Keep Alive?

Keep-Alive, also known as keep-alive, is a communication pattern between a web server and a client that reduces the number of HTTP requests and speeds up a web page.

When Keep-Alive is activated, the client and server agree to keep the connection open for subsequent requests or responses.

How does Keep Alive work?

By default, HTTP connections are closed at the end of data transactions. This means that clients create a new connection to request each file on a page, and servers close these TCP connections after sending the data.

However, if a server needs to respond to multiple HTTP requests simultaneously and serve a single file for each new TCP connection, the site’s page load time will increase. This can lead to a poor user experience.

To overcome this problem, website owners should enable the Keep-Alive header to limit the number of new connections.

By enabling the Keep-Alive connection header, clients can download all content, such as , CSS, images, and videos, over a single TCP connection instead of sending a separate request for each file.

Here is an image showing how Keep-Alive works:

Advantages of enabling Keep-Alive

Some of the benefits of enabling Keep-Alive are:

  • Keep-Alive reduces CPU and memory usage: due to a lower amount of generation of HTTP requests. This will benefit users of all hosting platforms (free hosting, shared hosting, VPS).
  • Provides HTTP pipelining: delivery of requests over the same TCP connection, without the need to wait for a response from the server.
  • Modern browser support: many browsers use persistent connections automatically, as long as the destination servers support keep-alive. A modern browser usually allows per domain.
  • SEO Boost: activating Keep-Alive improves the performance of the site, which affects its performance in search engine optimization ().
  • Reduced network congestion: Reducing the number of TCP connections between servers and clients frees up network bandwidth.
  • SSL/TLS performance improvement: with Keep-Alive, a connection is only opened once, avoiding the need for additional handshakes and improving site latency.
See also  WooCommerce Tutorial: How to Install eCommerce in WordPress

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

HTTP requirements before activating Keep-Alive

The file you need to prepare before enabling the Keep-Alive HTTP header depends on the server you are using and your access privileges.

Make sure you have access to one of the following files before continuing with this guide:

  • .htaccess: is a directory-level configuration file that can be used to change the features and functions of sites hosted on the .
  • httpd.conf: is the main Apache configuration file. Please note that you may not have access to this file if your website is running on shared hosting.
  • nginx.conf: is the main configuration file of .

Then follow these steps to enable Keep-Alive:

Step 1 – Analyze your website speed

Although many hosting providers have Keep-Alive enabled by default, some of them may have this header disabled for performance reasons.

To check if Keep-Alive is enabled on your server, run one using a tool like .

If the scan tool shows that Keep-Alive is disabled, go to the next step.

Step 2Enable Keep-Alive

To activate Keep-Alive, you must explicitly request it through the HTTP header by accessing .htaccess or to the main configuration file of your web server. If you enable Keep-Alive, the HTTP response header will display:

Connection: keep-alive.

The following tutorial will cover four different methods to enable Keep-Alive on your server.

Option 1: Edit the .htaccess file

Enable Keep-Alive and override any server settings by adding the following code to your .htaccess file:

Header set Connection keep-alive

This method should work on most Linux-based shared hosting providers. If you use , using the File Manager.

Make sure to test your website for any errors after editing the file .htaccess.

Option 2: Enable Keep-Alive on Apache via httpd.conf file

If you have access to the Apache configuration file, it is possible to enable the extension from there.

See also  How to Clear Browser Cache: Complete Guide

To locate the httpd.conf file, enter the following at the command line:

find / -name httpd.conf

The following parameters affect the Keep-Alive functionality in Apache, from enabling the keep-alive to setting the idle connection timeout:

  • Keep Alive: set Keep-Alive On to enable the extension or KeepAlive Off to disable it.
  • MaxKeepAliveRequests: this directive sets the maximum number of user requests that the server will maintain during each keep-alive connection. Consider setting its value to 50 requests for a single connection. Increase the limit if your server needs to deliver more responses.
  • KeepAliveTimeout: this value specifies how long the server waits for new requests from a client. It is recommended to keep the connection idle for five to ten seconds and increase it if necessary. Setting this value too high can cause a high load on the server and waste memory resources.

This is the configuration you should have:

# # 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

Option 3: Enable it in NGINX

NGINX is a server and reverse proxy that has Keep-Alive enabled by default. However, in some cases it may be disabled. Users can enable it using ngx_http_core_module.

Look for the keepalive_disable value. In most cases, this will be the reason why Keep-Alive is not working.

Before you remove the value to enable Keep-Alive, make sure you find out why it was disabled in the first place.

Option 4: Enable Keep-Alive on Windows Server (IIS)

If you’re using a Windows-based server, enable the Keep-Alive extension via the command line.

The following command will enable it:

appcmd set config /section:httpProtocol /allowKeepAlive:true

If you want to disable Keep-Alive, use:

appcmd set config /section:httpProtocol /allowKeepAlive:false

See also  FTP files

Windows Server users can also enable the Keep-Alive header by editing the configuration files directly or by writing WMI scripts.

Step 3 – Check the changes

Once Keep-Alive is enabled, run another scan with GTMetrix or the to see if the extension is active.

It is also possible to check the HTTP header through the terminal using the following command:

curl -I http://example.com/example.php

In the following example, we will check the HTTP header for .com:

curl -i http://.com/index.php

This gives us the following results:

HTTP/1.1 301 Moved Permanently Date: Tue, 27 Jul 2021 01:55:24 GMT Transfer-Encoding: chunked Connection: keep-alive Cache-Control: max-age=3600 Expires: Tue, 27 Jul 2021 02:55:24 GMT Location: https://www..com/index.php

Connection: keep-alive shows that Keep-Alive is functional.

conclusion

Activating Keep-Alive is a great way to optimize your websiteas it helps improve speed and performance, ensuring faster loading times and greater efficiency.

By enabling the Keep-Alive header, the client and server can reuse a single TCP connection for multiple requests and responses. This eliminates the need to establish new connections for each HTTP request or response.

Let’s recap the steps to activate the Keep-Alive header:

  1. Analyze your site: use a tool like GTMetrix to check if Keep-Alive is functional.
  2. Enable Keep Alive: access the .htaccess or main configuration file of your server to enable Keep-Alive via the HTTP header. Windows Server users can do this through the command line.
  3. Try the changes: run another scan to see if Keep-Alive is active.

We hope this article has helped you to enable Keep-Alive. If you have any questions, let us know in the comments section below.

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...