From what I understand, a HTTP connection could either be keep-alive
or close
.
I sent a HTTP request to a server:
GET /page1/ HTTP/1.1 Host: server.com Connection: keep-alive
And it responded with:
HTTP/1.1 200 OK Connection: keep-alive, close
Essentially, I believe the server is bugged because a response like keep-alive, close
is ambiguous.
However, as the receiver, how should we handle such a message? Should we interpret this header value as keep-alive
or close
?
HTTP keep-alive, a.k.a., HTTP persistent connection, is an instruction that allows a single TCP connection to remain open for multiple HTTP requests/responses. By default, HTTP connections close after each request.
A keepalive (KA) is a message sent by one device to another to check that the link between the two is operating, or to prevent the link from being broken.
All modern browsers use persistent connections as long as the server has Keep-Alive enabled. In order to check if your pages are delivered with a Keep-Alive header, you can use the HTTP Header Checker tool. This will display the Connection: Keep-Alive field if the HTTP Keep-Alive header is enabled.
Use “KeepAlive On” to enable it. To disable, just use “KeepAlive Off”. It sets the maximum number of requests for every Keep-Alive connection.
TL; DR: Chrome interprets this response header as keep-alive
and maintain a peristent connection while Firefox closes each connection.
I stumbled over this question as I tried to optimize the page loading time for my website.
In the referenced RFC I didn't find anything about how multiple entries in the Connection
header may be properly handled. It seemed to me like the implementation may choose from two possibilites:
close
inside, you may close the connection after transmissionSo, I needed to find out. Let's make some deeper investigation:
I noticed that Chrome was always sending a HTTP/1.1 request with Connection: keep-alive
and my Apache default configuration was always responding with a Connection: close
header. So I began investigating and took a look at the TCP segments with Wireshark.
Chrome has to fetch 14 elements to display the website, mostly of them static things like images or css files. And it took in complete 14 TCP connections and that took a lot of time (approximately 1,2 seconds). After each request for an image (e.g.) there came a TCP segment with the FIN
flag set to 1.
So what about Chrome vs. Firefox? Chrome seems to have a maximum number of concurrent connections to one server of 6. Firefox has a more granular configuration and distinguishs persistent (maxium of 6, seen in about:config) and non-persistent (the maximum numbers differed a lot in different sources). But wait... Both, Chrome and Firefox are sending HTTP/1.1 request headers with Connection: keep-alive
, so both should be limited to 6 (as this is a request for opening up a persistent connection).
I decided to try a simple trick and added the following lines to my .htaccess
in the web root folder:
<ifModule mod_headers.c> Header set Connection keep-alive </ifModule>
The server now responds with:
Connection: keep-alive, close
Now I took a look at the TCP segments again: There were only 9 connections from Chrome to my server now and only 3 with the FIN
flag set to 1. So this trick seemed to work. But why were there those 3 connections, that closed the connection after data transmission? These were the PHP requests, as the HTTP header X-Powered-By: PHP/5.4.11
confirmed.
And what about Firefox? There were still those 14 requests!
How to fix that and get the fcgi processes to work with keep-alive too?
I added the following lines to my virtualhost section of the httpd.conf configuration:
KeepAlive On KeepAliveTimeout 5 MaxKeepAliveRequests 100
and removed the ones added in the .htaccess
. Now the server isn't sending any confusing - Connection: keep-alive, close
, but only Connection: keep-alive
and everything works fine!
Conclusion:
A header with the connection field set to
HTTP/1.1 200 OK Connection: keep-alive, close
will be interpreted by Chrome as keep-alive
while Firefox seems to close each connection. It seems to depend on the actual implementation.
So if you're willing to implement a client for handling response headers that contain Connection: keep-alive, close
, I would propose to try using keep-alive if you need more than one request. The worst thing that may happen: The server will close the connection and you need to connect again (that's exactly the other option you would have had!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With