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
?
7 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>
-
5
-
For more ifmodule keep alive settings see httpd.apache.org/docs/2.2/mod/core.html#keepalive– BlowsieJan 23, 2013 at 9:25
-
4
-
Can anyone guess why this doesn't work for me in Apache 2.4? KeepAlive is set to off in in apache2.conf. I can enable it via vhost conf file but not in htaccess. Jan 5, 2017 at 15:51
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.
-
Thanks bobince ,what should be the timeout time , as my site doesn't have a great amount of traffic thanks– webkulFeb 4, 2010 at 15:19
-
If your hosts haven't changed it, the default in Apache 2.0 is 15 seconds, and in 2.2 it's 5 seconds. Either setting is perfectly reasonable.– bobinceFeb 4, 2010 at 15:23
-
2Sorry but in Apache you can use the .htaccess adding this code <ifModule mod_headers.c> Header set Connection keep-alive </ifModule> ... and it works! About the timing, the server default, often, is good.– Simbus82Oct 3, 2014 at 9:53
-
better answers from anil kumar & pronskiy below ---
<ifModule mod_headers.c> Header set Connection keep-alive </ifModule>
in.htaccess
works for me as well. Here is another helpful article: feedthebot.com/pagespeed/keep-alive.html Dec 29, 2014 at 22:53 -
I have root access to the server, but i dont want to enable keep alive for all my sites, so its not possible to enable to just one? Oct 22, 2016 at 13:35
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>
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.
It very much depends on your site and the amount of traffic it receives. If a user comes to your site, then clicks through to another page within the KeepAliveTimeout setting (default is 15), a new TCP does not have to be created. This can really help with overhead.
On the other hand, any Apache processes that are currently tied up w/ existing visitors will not be able to talk to the new ones. So you may have to increase the total number of Apache processes that are available.
In short... it requires tweaking.
-
thanks for the answering but the keep-alive is not working in my site how can i enable and my site doesn't have a lot amount of traffic thanks– webkulFeb 4, 2010 at 15:01
Paste the following code in your .htaccess file:
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
Then use this website: https://varvy.com/pagespeed/ to check if it's enabled.