简体   繁体   English

服务器响应中的“连接:保持活动”

[英]“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. 我正在尝试建立由Apache服务器托管的从Silverlight应用程序到PHP页面的HTTP持久连接(即,不为每个HTTP请求创建新的TCP连接)。

To this end, I need the webserver to send its HTTP responses with the "Connection" header set to "Keep-alive". 为此,我需要Web服务器发送其HTTP响应,并将“连接”标头设置为“保持活动”。 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. 客户端方面,似乎没有任何问题,因为从我阅读的内容来看,Silverlight提供的网络API基本上是浏览器网络功能的包装:因此,如果浏览器支持HTTP 1.1和Connection:Keep-Alive默认情况下,它的请求就可以了。 Content-Length is also well defined, so that the server knows when it has to send the response. Content-Length的定义也很明确,因此服务器知道何时必须发送响应。 However, the server response to the PHP request sets systematically "Connection:" to "close", thus ending the connection and preventing a persistent connection. 但是,服务器对PHP请求的响应会系统地将“ Connection:”设置为“ close”,从而终止连接并阻止持久连接。

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 : 我已经尝试了一些方法来解决此问题:不同的方法(GET和POST),在脚本的开头使用以下PHP代码明确为响应提供“ Connection:keep-alive”:

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. 后者将期望的标头添加到响应中,这很好,但是稍后在响应标头中仍附加附加的“ Connection:close”。

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 ? 它是PHP或Apache的一种功能,它强制执行“关闭”(我猜是出于某种安全性或性能目的),或者我只是在这里遗漏了什么?

Thanks in advance. 提前致谢。

PS : By sniffing packets, I've noticed that not many websites use "Keep-alive" and the TCP connection is reestablished. PS:通过嗅探数据包,我注意到很少有网站使用“ Keep-alive”并且重新建立了TCP连接。 Isn't Keepalive the default and preferred behavior under HTTP 1.1 ? 在HTTP 1.1下,Keepalive是否不是默认行为和首选行为?

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. 代替为网页中的每个图像/ css / javascript创建新的连接,许多请求将被重新使用相同的连接。

There are some settings that prevent this in Apache too, like maximum number of requests on a connection or timeouts between requests. Apache中也有一些设置可以阻止这种情况,例如连接上的最大请求数或请求之间的超时。 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/ 对于使您的连接保持打开状态的服务,请访问http://orbited.orghttp://twistedmatrix.com/trac/

Since PHP doesn't manage the HTTP connection, it has no way to change this setting. 由于PHP不管理HTTP连接,因此它无法更改此设置。 You need to set that in servers. 您需要在服务器中进行设置。 For example, you can enable keep-alive like this in Apache if you are using mod_php, 例如,如果您使用的是mod_php,则可以在Apache中启用这种保持活动状态,

KeepAlive On

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM