19

What is the difference between:

Header set Connection keep-alive

and

KeepAlive on

in Apache htaccess?

What code and options we have to put in the header of a php file? And what in htaccess file?

Sam
  • 15,254
  • 25
  • 90
  • 145

1 Answers1

43

If you simply set the header Connection: keep-alive it isn't going to be enough. The client will think it's a keep-alive connection but the server may decide to close the connection. Additionally, the client doesn't know how many requests can be served through the keep-alive connection. There's an additional header that is used to track requests sent through a keep-alive connection that looks like this:

Keep-Alive: timeout=15, max=100

which tells the client that it can send up to 100 more requests on the current keep-alive connection (and it counts down as you continue to use said keep-alive connection) and that the client has 15 seconds to make any additioanl requests before the connection is closed.

Simply using the header isn't sufficient to establish a keep alive connection because the server needs to negotiate it. Both ends need to know about the keep-alive and both ends need to do proper accounting. You need to tell apache to handle keep-alive on its end and simply sending the header isn't going to do that. You need to turn keep-alive on using the second directive:

KeepAlive on

And additionally, you can tweak the keep-alive mechanism with directives like:

KeepAliveTimeout 15
MaxKeepAliveRequests 100
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks @Jon Lin that is one awesome clear well written answer! Wish you a Good day I will contact my server admins. – Sam Aug 24 '12 at 07:32
  • 1
    On shared hosting the use of KeepAlive is often not allowed: when putting it in .htaccess the server says `Error occurred: 500 - internal server error` :( :( :( – Sam May 04 '14 at 20:55
  • 1
    ` Header set Connection keep-alive ` worked for me on my shared hosting source: http://www.feedthebot.com/pagespeed/keep-alive.html – retrovertigo Dec 29 '14 at 22:50
  • Dear @Jon thanks (again) for this amazing insight. Can you please clarify: what kind of code goes into the `page.php` file's header, and what code goes into Apache's `.htaccess`? in a shared hosting environment with comment from @retrovertigo included in it, thanks and Cheers mate! I've set a bounty for this :) – Sam Apr 03 '15 at 11:28
  • 1
    @Sam if your hosting provider won't allow you to use the `KeepAlive` directive in yuor htaccess file, then I don't think there's anything you can do with your php scripts to enable keep alive. Not sure how retrovertigo's suggestion works if the underlying apache mechanism has keep alive turned OFF. – Jon Lin Apr 04 '15 at 21:04
  • Can anyone tell what's the maximum timeout we can have for keep-alive. I remember I read somewhere it can't be more than the TCP connection timeout but am not sure? – Ram Patra Apr 20 '18 at 14:35
  • @JonLin What is the disadvantage if we don't use KeepAliveTimeout & MaxKeepAliveRequests in server config ? I want to know this use-case consider this keepAlive is turned on server and client is sending request with keepAlive header and it will be a persistent connection. Now if client is sending request in every 1 second then TCP connection will be same. Does a new socket will be created for each request ? – Sumit Kandoi Apr 13 '22 at 15:36