How to enable keep alive in Centos

Keep alive is an concept to allow the same tcp connection for HTTP conversation instead of opening a new one with each new request.
More simply , Keep alive is also known as a persistent connection.

How to enable keep alive

Keep-alive can be enabled using the “Connection: Keep-Alive”  in HTTP header.

Enabling keep-alive depends on what server you are using and what you have access to. We cover the most common methods here.

Enable keep-alive using .htaccess

If you do not have access to your webserver config file then you can enable keep-alive using an .htaccess file.

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

Adding above code in to your .htaccess file will add keep alive headers to your requests, and will override most webserver or host limitations.

Enable keep-alive in Apache

By default Keep-Alive is disabled in Apache server, We need to edit Apache configuration file and enable Keep-Alive in Apache on CentOS/RedHat Systems. so follow below step to enable it.

vim /etc/httpd/conf/httpd.conf

Now change the values as shown below:

# KeepAlive: Whether or not to allow persistent connections
KeepAlive On

# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. 

MaxKeepAliveRequests 100

# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
KeepAliveTimeout 15

Now restart Apache service using following command:

service httpd restart

Keep-Alive is successfully enabled in your Apache server.