How to Enable keep alive GTmetric Page Speed

Share!

What is keep-alive?

In short, keep-alive is a bit of communication between the web server and the web browser that says “you can grab more than just one file”.

In order to display webpages a browser must request files from a web server somewhere. There is a brief communication where the browser asks for a file and the web server says yes or no. If the browser and server had to have this communication for each file transferred then the entire affair would take longer than if the server could just say “grab whatever files you need”.

When a web browser wants to display a webpage it must first get the HTML file. It will then read the HTML file and request more files that the HTML references like CSS or images or whatever file the HTML is requesting. When keep alive is not enabled this can add alot of time to the downloading of a webpage. Webpages are often a collection of many files and if a new connection (the brief communication) has to be opened for each and everyone of those files it could take significantly longer to display that webpage.

More officially the definition of HTTP keep-alive would be something like “a method to allow the same tcp connection for HTTP conversation instead of opening new one with each new request”.

Some people mistakenly believe that they do not have to worry about this because HTTP connections nowadays are by default persistent (keep-alive enabled). While this is true, many people use shared hosting environments or web servers that may close connections unbeknownst to the user. This is done for performance reasons and since millions of pages are hosted in shared environments, there is a definite need to determine if your connections are keep-alive.

Enable keep alive GTmetric Page speed

Enable keep alive GTmetric Page speed

How to enable keep-alive

Since keep-alive is default, there is some reason your pages aren’t using it. This means your webserver is configured to close connections or your webhost has disabled it. You can still change it though. There are many scenarios, and I will try to cover the ones that are most common.

To enable keep alive you must add a HTTP header to your requests. If you are not seeing keep alive enabled it is likely because when you are requesting a page or resource your HTTP headers are saying “connection: close”. This will disable keep-alive and tells a server to shut the connection as soon as it is done loading that one file. If we change that to “connection: keep-alive” it will keep the connection open so all files can be retrieved before closing the connection.

Change HTTP header through .htaccess

Keep-alive is enabled by explicitly requesting it via your HTTP header. Doing so will override any higher settings. If you do not have access to your webserver config file you can add HTTP headers yourself using an .htaccess file. (please know that any changes to your .htaccess file can mess alot of things up)

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

Adding this to your .htaccess file will add keep alive headers to your requests, which will over ride most webserver or host limitations.

Enable keep-alive in Apache

If you are able to access your Apache config file, you can turn on keep-alive there. The applicable sections are shown 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 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 100

Share!

One thought on “How to Enable keep alive GTmetric Page Speed

Leave a comment