Apache – Enable Keep-Alive

Enabling keep-alive tells the browser of your visitors to establish a TCP connection once instead of multiple times to request all the web files loaded on your web page (like images, CSS scripts, Javascripts, etc..). This speeds up your website because the browser of your visitor needs to open just one persistent HTTP connection instead of having to establish multiple connections for every single file loaded by your web page.

How does Keep-Alive work?

Every time the browser of your visitor requests a file from your server there is a interaction between the browser and the server. This interaction consists of the server giving a “OK” every time the browser requests a file. If keep-alive is enabled the server only has to give this “OK” once instead of multiple times for every file because the network connection stays open. This can drastically minimize the network overhead and the CPU usage.

Keep-Alive should ALWAYS be enabled

There is no downside to enabling HTTP keep-alive when it comes to improving your website speed. Using a keep-alive connection will almost always speed up your website.

Enabling Keep-Alive for Apache

Using an htaccess file

To enable keep-alive through .htaccess you need to add the following code to your .htaccess file:

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

The beauty of using this method is that it overrides any settings made in the config file of your server. This means you don’t need server side access to enable it. Always test your website for errors or failures after editing the .htaccess file to prevent mistakes or cases where it doesn’t work out as you want to (in which case you should not use this method). If it works, keep the new settings. If it doesn’t, remove the lines from the .htaccess file.

Adjusting Apache settings

Keep-alive should be enabled automatically with every fresh Apache install – usually on a Unix (Linux) server. If it isn’t enabled there’s an easy way to enable it by editing the following settings in the Apache config file (httpd.conf):
Note: When you can’t find the http.conf file type execute find / -name httpd.conf via the command line.
– KeepAlive: Set it to “KeepAlive on” to enable keep-alive. To disable it use “KeepAlive off”.
– MaxKeepAliveRequests: Sets the maximum number of requests for every keep-alive connection. A good number to start with is 60 requests per connection.
– KeepAliveTimeout: Sets how long your server should wait for new requests from clients. The 15 second default is too high. Set it to 3 seconds to start with and gradually go up when it underperforms. Setting it to a lower amount of seconds results in less parallel connections which again results in less server load.

Enabling Keep-Alive for NGINX

NGINX supports and enables keep-alive by default. When you find out it isn’t enabled you should look for this setting in the NGINX core module (ngx_http_core_module):
– keepalive_disable: this setting could be the reason why keep-alive isn’t working. The default value is msie6(which you should leave untouched) which disables keep-alive for all older Internet Explorer versions. If you see other browsers listed try removing them to enable keep-alive for those browsers.

Windows Server (IIS)

There are various ways in which you can enable keep-alive on a Microsoft Windows run server. The easiest one is doing it through the IIS manager but there are a few other ways as well. Learn more here.

Source: giftofspeed.com