Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Connection: Keep-Alive" in server response

I'm trying to establish a HTTP persistent connection from a Silverlight application to a PHP page (ie without creating a new TCP connection for each HTTP request) hosted by an Apache server.

To this end, I need the webserver to send its HTTP responses with the "Connection" header set to "Keep-alive". Client-side, there doesn't seem to be any issue as the network API provided by Silverlight is basically a wrapper of the browser network capabilies, from what I've read : so if the browser supports HTTP 1.1 and Connection: Keep-Alive by default for its requests, it's fine. Content-Length is also well defined, so that the server knows when it has to send the response. However, the server response to the PHP request sets systematically "Connection:" to "close", thus ending the connection and preventing a persistent connection.

I've tried some things to work around this problem : different Methods (GET and POST), explicitly giving a "Connection: keep-alive" to the response with the following PHP code at the beginning of my script :

header("Connection: Keep-alive");

The latter adds the expected header to the response, which is good, but an additionnal "Connection: close" is still appended later in the response headers.

Is it a feature of PHP or Apache which enforces "close" (for some security or performance purpose, I'm guessing) or am I just missing something here ?

Thanks in advance.

P.S. : By sniffing packets, I've noticed that not many websites use "Keep-alive" and the TCP connection is reestablished. Isn't Keepalive the default and preferred behavior under HTTP 1.1 ?

like image 955
ZenithM Avatar asked Jun 07 '10 16:06

ZenithM


People also ask

How do I keep my server connection alive?

To enable Keep-Alive, you need to explicitly request it via the HTTP header by accessing . htaccess or the main configuration file of your web server. If you turn on Keep-Alive, the HTTP response header will show Connection: keep-alive.

Should I use Connection: keep-alive?

Enabling Keep-Alive is a great way to optimize your website as it helps improve speed and performance, ensuring faster load times and higher efficiency. By turning the Keep-Alive header on, the client and server can reuse a single TCP connection for a number of requests and responses.

How do I turn off Connection: keep-alive?

Disabling persistent (keep alive) connections by setting the property keepalive. timeout. millis to 0 in the TargetEndpoint configuration of a specific API Proxy or setting the HTTPTransport.

How does TCP keep connection alive?

Introduction. TCP connections consist of two sockets, one on each end of the connection. When one side wants to terminate the connection, it sends an FIN packet which the other side acknowledges and both close their sockets. Until that happens, however, both sides will keep their socket open indefinitely.


1 Answers

The Keep-Alive functionality is not meant for persistent connections.

Keep-Alive is meant to reduce the number of connections for a website. Instead of creating a new connection for each image/css/javascript in a webpage many requests will be made re-using the same connection.

There are some settings that prevent this in Apache too, like maximum number of requests on a connection or timeouts between requests. This will also eat your resources very fast because every connection needs its own thread.

You should switch to another solution, that is made for that kind of work.

For services that keep your connection open you can take a look at http://orbited.org and http://twistedmatrix.com/trac/

like image 108
favo Avatar answered Sep 30 '22 09:09

favo