php 服务器响应中的“连接:保持活动”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2991386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Connection: Keep-Alive" in server response
提问by ZenithM
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.
我正在尝试建立从 Silverlight 应用程序到由 Apache 服务器托管的 PHP 页面的 HTTP 持久连接(即不为每个 HTTP 请求创建新的 TCP 连接)。
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.
为此,我需要网络服务器发送其 HTTP 响应,并将“Connection”标头设置为“Keep-alive”。客户端,似乎没有任何问题,因为 Silverlight 提供的网络 API 基本上是浏览器网络功能的包装器,据我所知:因此,如果浏览器支持 HTTP 1.1 和连接:Keep-Alive默认情况下,它的请求,很好。Content-Length 也有明确定义,以便服务器知道何时必须发送响应。但是,服务器对 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 代码显式地为响应提供“连接:保持活动”:
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 ?
它是强制执行“关闭”的 PHP 或 Apache 功能(我猜是出于某些安全或性能目的)还是我只是在这里遗漏了什么?
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 ?
PS:通过嗅探数据包,我注意到没有多少网站使用“Keep-alive”并且重新建立了 TCP 连接。Keepalive 不是 HTTP 1.1 下的默认和首选行为吗?
回答by favo
The Keep-Alive functionality is not meant for persistent connections.
Keep-Alive 功能不适用于持久连接。
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.
Keep-Alive 旨在减少网站的连接数。许多请求将重新使用相同的连接,而不是为网页中的每个图像/css/javascript 创建新连接。
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.
在 Apache 中也有一些设置可以防止这种情况,例如连接上的最大请求数或请求之间的超时。这也会非常快地消耗您的资源,因为每个连接都需要自己的线程。
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.organd http://twistedmatrix.com/trac/
对于保持连接打开的服务,您可以查看http://orbited.org和http://twistedmatrix.com/trac/
回答by ZZ Coder
Since PHP doesn't manage the HTTP connection, it has no way to change this setting. You need to set that in servers. For example, you can enable keep-alive like this in Apache if you are using mod_php,
由于 PHP 不管理 HTTP 连接,因此无法更改此设置。您需要在服务器中进行设置。例如,如果您使用的是 mod_php,您可以像这样在 Apache 中启用 keep-alive,
KeepAlive On

