Improving Website Performance – Enabling Keep-Alive

Go Tech One Success Case: How to Increase Sales with a High-Performance Hosting
Domain Registration x Trademark Registration: Everything you need to know

In this tutorial you will learn four different methods to enable Keep-Alive. Keep-Alive allows the browser of a visitor download all content (such as JavaScript , CSS , images, videos, etc.) through a persistent TCP connection instead of making different requests for each file. This provides increased speed and performance, as the visitor’s browser will be able to get everything through a single, persistent HTTP connection. In short, Keep-Alive is a standard for communication between a web server and a browser with the potential to dramatically reduce the amount of requests and speed up a web page. Here is a picture that will help you understand the difference and benefits of Keep-Alive:

how keep alive works

Advantages to enable Keep-Alive:

  • Keep-Alive reduces CPU and memory due to a smaller amount of generated HTTP requests. This will benefit all users of the hosting platform (free hosting, shared hosting, VPS)
  • Enabling Keep-Alive provides HTTP pipelining (delivery requests through the same TCP connection)
  • HTTPS requests need more time and CPU resources. Keep-Alive will greatly benefit your website if you use HTTPS and SSL.
  • reduced latency and increased overall speed and load performance.
  • The Keep-Alive is supported by all modern browsers
  • Enable Keep-Alive will also benefit your site in terms of SEO and ranking due to better site performance.

In short, Keep-Alive is a great way to reduce the use of resources and increase the speed of your site at the same time.

What do you need?

Before beginning this guide, you need the following:

  • Access to the file .htaccess ;
  • Access Httpd.conf  (optional);
  • Access to HttpCoreModule  (optional).

Step 1 – Analyzing your site

First, you must analyze a website with a tool like GTmetrix  to determine if the keep-alive is enabled or disabled on the server. Here are the results after the analysis of a test page:

GTmetrix scan keep alive 0

On some servers or hosting providers, the keep-alive is enabled by default. If your analysis gives a score of 100%, there is nothing more that needs to be done.

Step 2 – Enabling Keep-Alive

There are several ways to activate the Keep-Alive and everything depends on your server or hosting provider.

Here are some options:

Option 1 – the .htaccess file Edition

To enable Keep-Alive, add the following code to your .htaccess file. Enable the Keep-Alive using. .htaccess  override any server settings and activate the connection.

<IfModule mod_headers.c>
Header SET CONNECTION keep-alive
</ IfModule>

This method should work on most shared hosting providers Linux. If you do not know where to find the .htaccess , take a look  at this tutorial .

Option 2 – Enabling Keep-Alive in via Apache httpd.conf

If you have access to the Apache configuration file, you can enable the extension there. Here is the appearance of the configuration:

#
# 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
  • Section KeepAlive On enables the extension .
  • MaxKeepAliveRequests sets the maximum number of requests allowed for a single connection. 50 requests for a connection is a great way to start.
  • Section KeepAliveTimeout specifies how long the server waits for new requests from a client. It is recommended to start with a lower value such as 5 or 10 seconds and increase it later if necessary. Setting this value too high can cause high server load.

If you can not locate the file httpd.conf , run the following command on the command line:

find / -name Httpd.conf

Option 3 – Enabling Keep-Alive in NGINX

The Keep-Alive is enabled by default on NGINX, however, in some cases, it can be disabled. You can activate it using HttpCoreModule . Look for the keepalive_disable value, which is in many cases the reason keep-alive is not working. Before you activate it, make sure you know why he went off first before attempting any changes.

Option 4 – Windows Server (IIS)

If you are using a Windows-based server, you can easily activate the Keep-Alive extension using the command line.

The following command will enable:

appcmd set config / section: httpProtocol / allowKeepAlive: true

And if you want to disable it, use:

appcmd set config / section: httpProtocol / allowKeepAlive: false

You can also consult the  official tutorial  Microsoft for some extra options.

Step 3 – Testing changes

After the Keep-Alive is fully enabled, run another check with GTmetrix  or any other site performance analysis tool to see if everything is working. Here are the results after the Keep-Alive was enabled:

GTmetrix scan keep alive 1

You can also check the Keep-Alive is working checking your HTTP header. This can be done via terminal using the following command:

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

Here’s an example:

-i curl 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 The part means that Keep-Alive is working.

Conclusion

In short, activate the Keep-Alive is a great way to improve the speed and performance of your website. The persistent TCP connection will ensure faster load times and more efficiently, keeping your visitors satisfied.

If you want to further improve your site, these articles will also help:

Improving Website Performance: Taking advantage of the browser’s cache

Improving site performance: Compression Gzip

Improving site performance: scale images Server

Improving site performance: Minificando CSS, HTML and JavaScript

Improving Website Performance: Using Progressive JPEG images