What is keep alive?
- Keep alive is a method to allow the same tcp connection for HTTP conversation instead of opening a new one with each new request.
- More simply put, it is a communication between the web server and the web browser that says “you can grab more than just one file at a time”.
- Keep alive is also known as a persistent connection
How to enable keep-alive
- Keep-alive is enabled using the “Connection: Keep-Alive” HTTP header
- If keep-alive is not enabled it is likely your HTTP headers are stating “connection: close”
- Change that to “connection: keep-alive” to enable keep-alive.
- Enabling keep-alive depends on what server you are using and what you have access to. We cover the most common methods below.
Enable keep-alive using .htaccess
If you do not have access to your webserver config file you can enable keep-alive yourself using an .htaccess file.
<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 override 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.
# 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.
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
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 very 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.
How to determine if keep-alive is enabled on my pages?
The GTmetrix keep alive tool reports on keep-alive status as well as several other factors.
Why is keep-alive used?
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.
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.
When keep alive is not enabled this process can increase the time it takes to download the page and waste server resources.
Isn’t keep alive on by default?
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. You can do so by using the GTmetrix keep alive tool.