Keep-Alive is a Directive in Apache that is used to maintain a persistent connection between client and server, which prevents a connection from breaking. It is also known as HTTP keep-alive, When we put KeepAlive Directive "on" it the allows same TCP connection for HTTP communication instead of creating a new connection for every new request.
1-We can enable Keep-Alive explicitly requesting via HTTP header.
In case we are not able to access our server configuration file, we can add HTTP headers by using .htaccess file.The .htaccess file can be used as a configuration control file which will affect if placed in any directory of our choice where we want to work it.Write the following code :
Header set Connection keep-alive
By adding this code in .htaccess file enable us to include Keep-Alive headers to our requests, which will override other settings.
2- If we can access the apache configuration file (i.e. /etc/apache2/apache2.conf for Debian/Ubuntu), you can turn on keep-alive in this file.In a fresh Apache installation it is bydefault enabled.Keep-Alive can be set by editing few settings in Apache configuration file as below.
2.1-Enabling and Disabling KeepAlive
a- To enable : KeepAlive On
b- To disable : KeepAlive Off
It provides an option to set the max number of requests for every KeepAlive connection.A value 100 is good enough for almost any scenario in case of apache2.4. However, this value can be increased depending on the amount of files in a web page that the server will deliver.
It prevents unused connections to exist for too long. It sets a time, which specifies that after what time server can create new request from client.
The number of seconds Apache httpd will wait for a subsequent request before closing the connection is ideal between 5 to 10 seconds in case of apache2.4. Postfixing ms the timeout can be set in milliseconds.