Website

Improving Website Performance – Enabling Keep-Alive

Introduction

How does Keep-Alive work?

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

How Keep-Alive Works

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:

Keep-Alive not fully working

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.

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

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:

And if you wish to disable it, use:

You can also refer to an official tutorial from Microsoft for a few extra options.
a

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:

Keep-Alive fully functional

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:

The results are:

The Connection: keep-alive part signifies that Keep-Alive is functional.

Conclusion

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:

Improving website performance: Leveraging Browser Cache

Improving website performance: Gzip Compression

Improving website performance: Serving Scaled Images

Improving website performance: Minifying CSS, HTML and JavaScript

Improving website performance: Using Progressive JPEG images

Add Comment

Click here to post a comment

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!

More in Website
Improving Website Performance – Gzip Compression

Close