Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using keep-alive feature in .htaccess

I want to use the keep-alive feature in Apache. How can I do this with my host (.htaccess file), and what are the best values for the parameters like KeepAliveTimeout?

like image 761
webkul Avatar asked Feb 04 '10 14:02

webkul


People also ask

How do I enable Keep-Alive in Apache?

Apache. If you have access to your Apache configuration file ( httpd. conf ), you can turn on Keep-Alive there. To enable HTTP Keep-Alive , set to KeepAlive On or to disable it set to KeepAlive Off .

Should Enable Keep-Alive?

Enabling Keep-Alive ensures that a single TCP connection is used to transfer multiple files from the server to the browser. This helps your page load faster as the browser doesn't need to establish multiple connections to retrieve all your page resources.

What is the purpose of Keep-Alive in HTTP?

The HTTP keep-alive header maintains a connection between a client and your server, reducing the time needed to serve files. A persistent connection also reduces the number of TCP and SSL/TLS connection requests, leading to a drop in round trip time (RTT).


4 Answers

If Keep-alive is turned on in the Apache configuration, all you need is just set an HTTP header Connection: keep-alive. E.g. add following lines to your .htaccess file:

<ifModule mod_headers.c>
    Header set Connection keep-alive
</ifModule>
like image 125
pronskiy Avatar answered Oct 05 '22 11:10

pronskiy


You can't control keepalive behaviour in an .htaccess. Keepalives are a host-level feature, not one where different directories can behave differently depending on the per-directory htaccess info.

If you are on the kind of basic shared hosting that only gives you .htaccess to configure your sites, you can't change the keepalive settings. Presumably the hosting company will have set them appropriately, or just left them on the default settings, which are usually fine.

like image 27
bobince Avatar answered Oct 05 '22 11:10

bobince


Yes Keep-alive behavior can be controlled in .htaccess file. First check the server setting by printing $_SERVER and if

[HTTP_CONNECTION] => keep-alive

is there then you just have to include the setting in your .htaccess file. Add the following line at the end of .htaccess file in your project's root directory.

<ifModule mod_headers.c>
    Header set Connection keep-alive
</ifModule>
like image 20
anil kumar Avatar answered Oct 05 '22 10:10

anil kumar


If you have SSH access to your server you should edit the Apache config file. Use these settings as a starter:

  • KeepAlive: on
  • KeepAliveTimeout: 3 seconds
  • MaxKeepAliveRequests: 60

This should work for most basic server setups with average traffic. You can always tweak the settings to suit your own needs. See here for more detailed info about this: http://www.giftofspeed.com/enable-keep-alive/

If you don't have access to your server you should contact your host. Changing the keepalive settings on your own by editing the .htaccess file will probably don't work.

like image 27
William Dresker Avatar answered Oct 05 '22 09:10

William Dresker