Speed Enable Keep Alive

keep alive

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 that file and request the other things that the HTML references like CSS, javascript or images.

When keep alive is not enabled this process can increase the time it takes to download the page.

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.

server request

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; you should read this before you change any setting unless you are familiar with .htaccess)

<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


Enable keep-alive in NGINX

Keep alive issues can be tackled using the HttpCoreModule. there is a specific directive you should look out for... "keepalive_disable". If you see this make sure you know why it is disabling keep-alive before removing.

Enable keep-alive in Litespeed

Keep-alive is default but your server may be using what is called "smart keep-alive". This is a setting within Litespeed that is specifically for high volume websites. When this setting is on it will appear to pagespeed tools that keep-alive is disabled.

"Smart keep alive" will request the initial file (the HTML file) with a connection close in the HTTP header. It will then request all the other files (css, js, images, etc.) with keep alive enabled. This allows more users to be able to connect at once when there are many concurrent requests.

Tip (litespeed servers only): Unless you are indeed a high traffic site you can (and probably should) disable smart keep alive in the config and once you do so all of your connections will use keep alive.

Patrick Sextonby Patrick Sexton