What is HTTP Keep-Alive?

HTTP keep-alive, also known as HTTP persistent connection or HTTP connection reuse, allows a TCP connection to remain open for multiple HTTP requests and responses.

Since the HTTP connections close after each request by default, the browser has to create new connections to request each of the files(e.g. JS, CSS, etc)whenever a user visits the site. This will increase the page loading times.

To stop this from happening, HTTP keep-alive is used where all the web page resources can be fetched over a single connection.


Keep-alive: parameters


Keep-alive has two parameters namely timeout and max which are separated by a comma.

The timeout parameter represents the minimum amount of time, in seconds which the connection has to be kept open.

The max parameter represents the number of requests that can be sent to the connection before closing it.

Keep-alive:timeout=5, max=1000

The keep-alive can be enabled by “KeepAlive On” and disabled by “KeepAlive Off”.

An example of HTTP Keep-alive header:

HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
Content-Length: 1845
Keep-Alive: timeout=1000, max=20
Server: Apache

1. Requirements before enabling Keep-alive header

Though the file we need to prepare to enable Keep-Alve depends on the server, we should at least have access to have one of the following files,

  • .htacess - .htacess files or distributed configuration files helps in providing a way to make configuration changes on a per-directory basis. It can be used to manage the functions of sites hosted in Apache.
  • nginx.conf - It is the main configuration file of Nginx.
  • httpd.conf - it is the primary apache configuration file.

2. Enabling the Keep-Alive Server

a.) Enabling using the .htaccess file:

If in case, keep-alive is not enabled in the server, it can be turned on by placing the following code in the .htaccess file.

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

Using this method overrides any settings made in the configuration file of the server. It means we don’t need server-side access to enable it. Though there are other ways to enable a keep-alive header for Apache, the easiest way to do this is by editing the .htacess file.

b.) Enabling using http.conf file:

In order to locate the httpd.conf file, type the following command in the command line:

> find / -name httpd.conf

After the file has been opened, then do the following steps: Set the KeepAlive as “On”. Set the MaxAliveRequests. It says the maximum number of user requests that will be maintained during each Keep-Alive connection. Set the KeepAliveTimeOut*. It indicates how long the server can wait for a new request from the client. Try not to set this value too high because it may cause a heavy load on the server.

The final code will be like this:

KeepAlive On
MaxKeepAliveRequests 50
KeepAliveTimeOut 10

c.) Enabling in Nginx:

Nginx server has keep-alive enabled by default. In exceptional cases where it will be disabled, the users can enable it using ngxhttpcore_module.

keepalive_disable - If the keepalive is not working in Nginx, then this setting should be changed.

Test the changes:

Once the Keep-alive header has been enabled, we can check it in the terminal itself.

Type the following command, with your domain name in it.

> curl -I http://atatus.com/

If the keep-alive is enabled, then the connection will be set to keep-alive.

Connection: keep-alive

If we arent able to see this in the terminal, then it means, that the keep-alive is disabled.

3. Web browsers using Keep-Alive connections

The modern web browsers using persistent connections are as follows:

  • Google Chrome
  • Firefox
  • Internet Explorer(since 4.01)
  • Opera(since 4.0)
  • Safari

In Internet Explorer, versions 6 and 7 use two persistent connections, whereas version 8 use six persistent connections. Persistent connections timeout after 60 seconds of inactivity which can be changed via the Windows registry.

In Firefox, persistent connections timeout after 115 seconds of inactivity which can be changed in the configuration.

4. Benefits of Keep-Alive

  • The usage of the CPU is reduced because of fewer new connections and TLS handshakes because of the persistent connection between the client and the server.
  • Because of fewer TCP connections, network congestion will be drastically reduced.
  • It also enables the HTTP pipelining of requests and responses.
  • Since the TLS handshakes are reduced there is also a decrease in the latency.

5. Disadvantages of Keep-Alive

If a client fails to close the connection even after all the data it wants has been received, then the resources needed to have the connection open will not be available for other clients. This affecting the ability of the server also the unavailability of the resources depends on the server’s architecture and configuration.

A condition known as race condition may occur in which the server closes the TCP connection when a client sends the request. In this situation, the server should send a 408 request timeout status code to the client before closing the connection. When the client receives this 408 code, it may open a new connection to the server and resend the request.

But not all clients will re-send the request and many will do so only if the request has an idempotent HTTP method.


It is important to verify the proxy server configurations and the applications to know if they support keep-alive. Almost all the browsers support persistent connections.

We can use HTTP/1.1 which keeps the connection alive by default for all requests unless the response contains a “Connection: close” header within it.

The newer protocol HTTP/2 also uses the same idea and allows multiple concurrent requests or responses to be multiplexed over a single connection.

The keep-alive server should be kept enabled always because it has more pros than cons. One of them is, that it improves the website speed. An increase in site speed is important as it is important for SEO and to draw more users to our site. If you want to know why site-speed matters, do check out this blog.

Further Reading

  1. What is Latency?
  2. Guide to Containers
  3. RuntimeError: Working outside of application context in flask

Monitor your software stack for free with Atatus.

Start your free trial
Request a Demo

Free 14-day trial. No credit card required. Cancel anytime.

Ready to see actionable data?

Avail Atatus features for 14 days free-trial. No credit card required. Instant set-up.