Keep-Alive is an HTTP header that allows a web server to use a single connection for multiple requests from a web browser. Servers running HTTP/1 often have keep-alive turned on to improve website performance. The keep-alive header is not used in HTTP/2 since it is the default behavior of the HTTP/2 protocol.
When Keep-Alive is enabled on a web server, it creates persistent connections between the server and clients (website visitors). The TCP connection stays open until it is closed or times out. Since each TCP connection must complete a handshake process, multiple connections increase the page load time. Keep-Alive provides a way for browsers to download all webpage assets, such as images and CSS files, over a single connection.
The disadvantage of Keep-Alive is that it requires more system resources from the web server. If websites on a server receive a lot of traffic, it may have several — possibly several thousand — persistent connections open at once. Eventually, the server may not be able to handle new connections and become unresponsive. Apache provides the following directives to prevent servers from reaching maximum capacity:
- KeepAliveTimeout - the maximum time a persistent connection can stay open while waiting for new requests
- MaxKeepAliveRequests - the maximum number of requests allowed within a single connection, before it needs to be reset
To enable keep-alive on an Apache server, add the following code to a server-wide include file or the .HTACCESS file of a specific website:
<IfModule mod_headers.c> Header set Connection keep-alive </IfModule>