HTTP Keep-Alive

http keep-alive

What is HTTP Keep-Alive?

Keep-Alive is an HTTP header that allows for the connection between a browser and a server to stay open, allowing the transfer of multiple files over a single connection. Without the HTTP Keep-Alive header, a new TCP connection would need to be opened for each file that needed to be retrieved to generate a page.

HTTP Keep-Alive Disabled vs Enabled

Enabling Keep-Alive can speed up your website as the browser only needs to go through the process of connecting to the server once. Below, is an outline of what a page loading process looks like if the HTTP Keep-Alive header isn’t enabled.

  1. A browser must first create a new connection to the server to receive a file.
  2. The browser then requests the HTML file from the server; the connection is terminated once the file is received.
  3. The browser determines whether there are additional files needed to display the complete page such as CSS, JavaScript, Images, etc.
  4. If yes, the browser then opens up a separate connection for each of these files individually resulting in a longer load time and additional strain on the server.

No HTTP Keep-Alive

On the other hand, if HTTP Keep-Alive is enabled, communication between the browser and server becomes much more simple. Once a single TCP connection is opened, all files can be delivered over that single open connection. It is much more time efficient and less resource intensive to deliver pages with this method.

HTTP Keep-Alive Enabled

Enabling HTTP Keep-Alive

Enabling Keep-Alive means that the HTTP response header will show Connection: Keep-Alive. If it is not enabled, it is likely that the header will show Connection: Close. Keep-Alive is enabled by default in most cases, however, sometimes hosting companies disable Keep-Alive for performance reasons. If HTTP Keep Alive isn’t enabled, you can enable it using the following options.

.htaccess

You can enable the HTTP Keep-Alive header directly from your .htaccess file. This will include Keep-Alive headers in your requests and override most web server or host limitations.

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

Apache

If you have access to your Apache configuration file (httpd.conf), you can turn on Keep-Alive there. To enable HTTP Keep-Alive, set to KeepAlive On or to disable it set to KeepAlive Off.

Nginx

Keep-Alive is enabled by default in Nginx. Settings and syntax can be identified using the HttpCoreModule. The “keepalive_disable none | browser” setting allows you to specify which browsers you want to disable the use of Keep-Alive for. This may be useful in the case that someone is using an older browser that doesn’t behave properly with Keep-Alive. Setting this to none will enable Keep-Alive for all browsers.

HTTP Keep-Alive Settings

  • KeepAliveRequests: The maximum number of requests allowed during each Keep-Alive connection. It is recommended to set this number to 100 as that is satisfactory in most scenarios. In the case that the server needs to deliver more files, you will want to increase this setting. For an unlimited amount of request during a single connecting, set this to 0.
  • KeepAliveTimeout: This is the number of seconds your server is set to wait for new requests from clients. This setting is typically set to around 7-10 seconds. A higher number may be required when there is a larger amount of traffic to ensure there is no frequent TCP connection re-initiated. However, setting this too high will result in the waste of resources (mainly memory) as the connection will remain open even if there is no traffic.

How to Check If HTTP Keep-Alive Is Enabled on Your Pages

All modern browsers use persistent connections as long as the server has Keep-Alive enabled. In order to check if your pages are delivered with a Keep-Alive header, you can use the HTTP Header Check Tool. This will display the Connection: Keep-Alive field if HTTP Keep-Alive is enabled.

Http-Keep-Alive

Additionally, if you want to check this directly from the command line, you can use the following cURL command and check the response headers that are returned.

curl -I https://www.keycdn.com
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 31 Oct 2016 19:37:22 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 20677
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: keycdn=v2bboirnhapd98884fhi66s0v80m71f0ohj4qh727bqdmbgb0v60; expires=Tue, 01-Nov-2016 19:37:22 GMT; Max-Age=86400; path=/; domain=.keycdn.com; secure; HttpOnly
Expires: Mon, 31 Oct 2016 19:37:21 GMT
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload

Now that you know more about the benefits of the HTTP Keep-Alive header, ensure that it is enabled via your origin server. Additionally, if you know what you’re doing, you can make adjustments to its settings to suit your specific use-case.

Leave A Comment?