HTTP持久连接:修订间差异
标签:2017版源代码编辑 |
Staph aureus(留言 | 贡献) 修饰语句 太過翻譯腔 标签:2017版源代码编辑 |
||
第1行: | 第1行: | ||
{{noteTA|G1=IT}} |
{{noteTA|G1=IT}} |
||
{{HTTP}} |
{{HTTP}} |
||
'''HTTP持久连接'''({{Lang-en|'''HTTP persistent connection'''}},也称作{{Lang|en|'''HTTP keep-alive'''}}或{{Lang|en|'''HTTP connection reuse'''}})是使用同一个[[传输控制协议|TCP]]连接来发送和接收多个HTTP请求/应答 |
'''HTTP持久连接'''({{Lang-en|'''HTTP persistent connection'''}},也称作{{Lang|en|'''HTTP keep-alive'''}}或{{Lang|en|'''HTTP connection reuse'''}})是使用同一个[[传输控制协议|TCP]]连接来发送和接收多个HTTP请求/应答的方法。使用該方法可以避免為每一个新的请求/应答打开一個新的连接。 |
||
== |
== 歷史 == |
||
HTTP 0.9協議不支援持久連接。每一次請求與回應完成後,客戶端與服務端的連接均必須被切斷。 |
|||
在HTTP 1.0中,没有官方的keepalive操作。通常是在现有协议上添加一个指数。如果浏览器支持keep-alive,它会在请求的包头中添加: |
|||
HTTP 1.0協議中並未定義持久連接的實現方式,但是一些服務端和客戶端開始使用這種方式交互。如果浏览器支持keep-alive,它会在请求的[[HTTP头字段|头字段]]中添加: |
|||
Connection: Keep-Alive |
Connection: Keep-Alive |
||
然后当服务器收到请求,作出回应的时候,它也 |
然后当服务器收到请求,作出回应的时候,它也在响应中添加如下HTTP頭: |
||
Connection: Keep-Alive |
Connection: Keep-Alive |
||
並且在完成回應之後的一段時間保持TCP連接。此時,客戶端知道服務端支援持久連接,故此在客户端发送另一个请求时,会直接使用同一个连接發送這個請求。這種持久連接將一直持續到在客戶端或服務端認為無需繼續維持時主動中斷連接,或是使用 Connection: close 字段通知對方為止。 |
|||
在HTTP 1.1中所有的连接默认都是持续连接 |
在HTTP 1.1中,除非任意一方在請求時明確声明不支持持久連接,否則所有的连接默认都是持续连接。<ref>{{Cite web |url=http://tools.ietf.org/html/rfc2616#section-8.1 |title=Persistent HTTP Connections in RFC 2616 "Hypertext Transfer Protocol -- HTTP/1.1" |accessdate=2012-01-19 |archive-date=2011-03-07 |archive-url=https://web.archive.org/web/20110307142107/http://tools.ietf.org/html/rfc2616#section-8.1 |dead-url=no }}</ref> HTTP 1.1的持久连接不需要使用独立的keepalive信息,但是出於兼容性考慮,實作上仍然通常會如上述 HTTP 1.0一樣發送 Connection: Keep-Alive 字段以期盼對方不會切斷連接。 |
||
HTTP 2.0協議要求雙方必須支援復用連接。HTTP 3.0因為使用基於UDP的QUIC協議,故此不存在「連接」的概念,自然無需提供持久連接。 |
|||
== 优势 == |
== 优势 == |
||
* 较少的[[CPU]]和内存的使用(由于同时打开的连接的减少了) |
* 较少的[[CPU]]和内存的使用(由于同时打开的连接的减少了) |
||
* 允许请求和应答的[[HTTP管線化]] |
* 允许请求和应答的[[HTTP管線化]] |
||
* 降低[[拥塞控制]] ([[传输控制协议|TCP连接]]减少了) |
* 降低[[拥塞控制]] ([[传输控制协议|TCP连接]]的數量减少了) |
||
* 减少了后续请求的[[延遲 (電腦)|延遲]](无需再进行[[握手 (技术)|握手]]) |
* 减少了后续请求的[[延遲 (電腦)|延遲]](无需再进行[[握手 (技术)|握手]]) |
||
* |
* 无需关闭TCP连接即可报告错误 |
||
根据RFC 2616 (47页),用户客户端与任何服务器和代理服务器之间不应该维持超过2个链接。[[代理服务器]]应该最多使用2×<var>N</var>个持久连接 |
根据RFC 2616 (47页),用户客户端与任何服务器和代理服务器之间不应该维持超过2个链接。[[代理服务器]]連接到其他服务器或代理服务器時,应该最多使用2×<var>N</var>个持久连接,其中<var>N</var>是同时活跃的用户数。这个指引旨在提高HTTP响应时间并避免阻塞。 |
||
== 劣势 == |
== 劣势 == |
||
服務器可以建立的連接數量受到端口數與文件句柄數的限制。如需和客戶端保持一個開啟時間較長的連接,那麼服務器可以同時服務的客戶端數量則會減少。 |
|||
对于现在的广泛普及的宽带连接来说,Keep-Alive也许并不像以前一样有用。web服务器会保持连接若干秒(Apache中默认15秒),这与提高的性能相比也许会影响性能。 |
|||
為了應對這一問題,Apache 2.0 httpd的默认连接过期时间<ref>{{Cite web |url=http://httpd.apache.org/docs/2.1/mod/core.html#keepalive |title=Apache HTTP Server - KeepAlive Directive |accessdate=2012-01-19 |archive-date=2012-01-21 |archive-url=https://web.archive.org/web/20120121073949/http://httpd.apache.org/docs/2.1/mod/core.html#keepalive |dead-url=yes }}</ref>仅有15秒<ref>{{Cite web |url=http://httpd.apache.org/docs/2.0/mod/core.html#keepalivetimeout |title=Apache HTTP Server 2.0 - KeepAliveTimeout Directive |access-date=2012-01-19 |archive-url=https://web.archive.org/web/20130503121401/http://httpd.apache.org/docs/2.0/mod/core.html#keepalivetimeout |archive-date=2013-05-03 |dead-url=yes }}</ref> ,而更新版本的Apache 2.2仅有5秒。<ref>{{Cite web |url=http://httpd.apache.org/docs/2.2/mod/core.html#keepalivetimeout |title=Apache HTTP Server 2.2 - KeepAliveTimeout Directive |accessdate=2012-01-19 |archive-date=2014-05-22 |archive-url=https://web.archive.org/web/20140522123325/http://httpd.apache.org/docs/2.2/mod/core.html#keepalivetimeout |dead-url=yes }}</ref> 通過設定較短的过期时间,一個客戶端能够在最開始時快速的传输多个web页组件,而不会長期佔用服務器的進程或端口資源。<ref>{{cite web |url=http://docforge.com/wiki/Httpd/KeepAlive |title=Httpd/KeepAlive |author=Multiple (wiki) |work=Docforge |accessdate=2010-01-30 |deadurl=yes |archiveurl=https://web.archive.org/web/20100106140535/http://docforge.com/wiki/Httpd/KeepAlive |archivedate=2010-01-06 }}</ref>但是,這樣也會使得客戶端在需要發起新請求時,原先建立的持久連接可能已經被服務端關閉。這直接削弱了持久連接帶來的優勢。 |
|||
持久連接僅在客戶端需要發送多於一個請求時才能發揮優勢。在图片存放网站等的一些客戶端僅需發起一個請求的使用場景下,Keep-Alive會導致客戶端在请求完成之后的較長時間仍然與服務器保持不必要的连接,故此反而會導致性能下降。 |
|||
对于单个文件被不断请求的服务(例如图片存放网站),Keep-Alive可能会极大的影响性能,因为它在文件被请求之后还保持了不必要的连接很长时间。 |
|||
== Web浏览器 |
== Web浏览器 == |
||
[[Image:HTTP persistent connection.svg|thumb|300px|lang=zh|使用多个连接和使用持久链接的对比]] |
[[Image:HTTP persistent connection.svg|thumb|300px|lang=zh|使用多个连接和使用持久链接的对比]] |
||
[[网景导航者]](4.05版本以后)和[[Internet Explorer]](4.01版本以后)支持使用持久链接链接Web服务器和代理服务器。 |
[[网景导航者]](4.05版本以后)和[[Internet Explorer]](4.01版本以后)支持使用持久链接链接Web服务器和代理服务器。 |
||
网景 |
网景在需要打开一个连接到不同的服务器上的新持久链接時,使用[[最近最少使用算法]]终止一个空闲的持久链接。<ref>{{cite web | url=http://www.cs.wisc.edu/~cao/papers/persistent-connection.html | title=Persistent Connection Behavior of Popular Browsers | date=1998-12-09 | publisher=University of Wisconsin-Madison | accessdate=2009-07-08 | author=Zhe Wang and Pei Cao | archive-date=2007-04-04 | archive-url=https://web.archive.org/web/20070404025532/http://www.cs.wisc.edu/%7Ecao/papers/persistent-connection.html | dead-url=no }}</ref> |
||
[[Internet Explorer]]支持持久链接,IE 6和IE 7缺省使用2个持久链接,而IE 8缺省使用6个持久链接<ref>{{cite web | url=http://www.stevesouders.com/blog/2008/03/10/ie8-speeds-things-up/ | title=IE8 speeds things up | date=2008-03-10 | publisher=stevesouders.com | accessdate=2009-07-17 | archive-date=2009-08-10 | archive-url=https://web.archive.org/web/20090810230948/http://www.stevesouders.com/blog/2008/03/10/ie8-speeds-things-up/ | dead-url=no }}</ref>。持久链接在不活跃60秒后过时 |
[[Internet Explorer]]支持持久链接,IE 6和IE 7缺省使用2个持久链接,而IE 8缺省使用6个持久链接<ref>{{cite web | url=http://www.stevesouders.com/blog/2008/03/10/ie8-speeds-things-up/ | title=IE8 speeds things up | date=2008-03-10 | publisher=stevesouders.com | accessdate=2009-07-17 | archive-date=2009-08-10 | archive-url=https://web.archive.org/web/20090810230948/http://www.stevesouders.com/blog/2008/03/10/ie8-speeds-things-up/ | dead-url=no }}</ref>。持久链接在不活跃60秒后过时關閉。該時長可以在Windows注册表中修改<ref>{{cite web | url=http://support.microsoft.com/kb/813827 | title=How to change the default keep-alive time-out value in Internet Explorer | date=2007-10-27 | publisher=Microsoft | accessdate=2009-07-17 | archive-date=2009-07-22 | archive-url=https://web.archive.org/web/20090722163626/http://support.microsoft.com/kb/813827 | dead-url=no }}</ref>。 |
||
[[Mozilla Firefox]]支持持久链接。 |
[[Mozilla Firefox]]支持持久链接。瀏覽器默認在連接不活跃300秒(5分钟)后过时關閉,亦對持久连接的最大个数(每个服务器,每个代理服务器,总数)加以限制。這些限制均可於配置中修改。<ref>{{cite web | url=http://kb.mozillazine.org/Network.http.keep-alive.timeout | title=Network.http.keep-alive.timeout | publisher=mozillazine.org | accessdate=2009-07-17 | archive-date=2009-06-08 | archive-url=https://web.archive.org/web/20090608091724/http://kb.mozillazine.org/Network.http.keep-alive.timeout | dead-url=no }}</ref> |
||
[[Opera浏览器|Opera]] 4.0版本开始支持持久链接<ref>{{cite web | url=http://www.opera.com/press/releases/2000/03/28/ | title=Opera 4.0 Upgrades File Exchange: Includes HTTP 1.1 | date=2000-03-28 | publisher=Opera Software | accessdate=2009-07-08 | archive-url=https://web.archive.org/web/20100910111319/http://www.opera.com/press/releases/2000/03/28/ | archive-date=2010-09-10 | dead-url=yes }}</ref>。可以配置 |
[[Opera浏览器|Opera]] 4.0版本开始支持持久链接<ref>{{cite web | url=http://www.opera.com/press/releases/2000/03/28/ | title=Opera 4.0 Upgrades File Exchange: Includes HTTP 1.1 | date=2000-03-28 | publisher=Opera Software | accessdate=2009-07-08 | archive-url=https://web.archive.org/web/20100910111319/http://www.opera.com/press/releases/2000/03/28/ | archive-date=2010-09-10 | dead-url=yes }}</ref>。可以配置持久连接的最大个数(每个服务器,总数)。 |
||
== 参考文献 == |
== 参考文献 == |
2023年8月1日 (二) 08:48的最新版本
HTTP/HTTPS |
---|
版本 |
请求方法 |
报文主体 |
頭欄位 |
狀態碼 |
相关主题 |
HTTP持久连接(英語:HTTP persistent connection,也称作HTTP keep-alive或HTTP connection reuse)是使用同一个TCP连接来发送和接收多个HTTP请求/应答的方法。使用該方法可以避免為每一个新的请求/应答打开一個新的连接。
歷史
[编辑]HTTP 0.9協議不支援持久連接。每一次請求與回應完成後,客戶端與服務端的連接均必須被切斷。
HTTP 1.0協議中並未定義持久連接的實現方式,但是一些服務端和客戶端開始使用這種方式交互。如果浏览器支持keep-alive,它会在请求的头字段中添加:
Connection: Keep-Alive
然后当服务器收到请求,作出回应的时候,它也在响应中添加如下HTTP頭:
Connection: Keep-Alive
並且在完成回應之後的一段時間保持TCP連接。此時,客戶端知道服務端支援持久連接,故此在客户端发送另一个请求时,会直接使用同一个连接發送這個請求。這種持久連接將一直持續到在客戶端或服務端認為無需繼續維持時主動中斷連接,或是使用 Connection: close 字段通知對方為止。
在HTTP 1.1中,除非任意一方在請求時明確声明不支持持久連接,否則所有的连接默认都是持续连接。[1] HTTP 1.1的持久连接不需要使用独立的keepalive信息,但是出於兼容性考慮,實作上仍然通常會如上述 HTTP 1.0一樣發送 Connection: Keep-Alive 字段以期盼對方不會切斷連接。
HTTP 2.0協議要求雙方必須支援復用連接。HTTP 3.0因為使用基於UDP的QUIC協議,故此不存在「連接」的概念,自然無需提供持久連接。
优势
[编辑]- 较少的CPU和内存的使用(由于同时打开的连接的减少了)
- 允许请求和应答的HTTP管線化
- 降低拥塞控制 (TCP连接的數量减少了)
- 减少了后续请求的延遲(无需再进行握手)
- 无需关闭TCP连接即可报告错误
根据RFC 2616 (47页),用户客户端与任何服务器和代理服务器之间不应该维持超过2个链接。代理服务器連接到其他服务器或代理服务器時,应该最多使用2×N个持久连接,其中N是同时活跃的用户数。这个指引旨在提高HTTP响应时间并避免阻塞。
劣势
[编辑]服務器可以建立的連接數量受到端口數與文件句柄數的限制。如需和客戶端保持一個開啟時間較長的連接,那麼服務器可以同時服務的客戶端數量則會減少。
為了應對這一問題,Apache 2.0 httpd的默认连接过期时间[2]仅有15秒[3] ,而更新版本的Apache 2.2仅有5秒。[4] 通過設定較短的过期时间,一個客戶端能够在最開始時快速的传输多个web页组件,而不会長期佔用服務器的進程或端口資源。[5]但是,這樣也會使得客戶端在需要發起新請求時,原先建立的持久連接可能已經被服務端關閉。這直接削弱了持久連接帶來的優勢。
持久連接僅在客戶端需要發送多於一個請求時才能發揮優勢。在图片存放网站等的一些客戶端僅需發起一個請求的使用場景下,Keep-Alive會導致客戶端在请求完成之后的較長時間仍然與服務器保持不必要的连接,故此反而會導致性能下降。
Web浏览器
[编辑]网景导航者(4.05版本以后)和Internet Explorer(4.01版本以后)支持使用持久链接链接Web服务器和代理服务器。
网景在需要打开一个连接到不同的服务器上的新持久链接時,使用最近最少使用算法终止一个空闲的持久链接。[6]
Internet Explorer支持持久链接,IE 6和IE 7缺省使用2个持久链接,而IE 8缺省使用6个持久链接[7]。持久链接在不活跃60秒后过时關閉。該時長可以在Windows注册表中修改[8]。
Mozilla Firefox支持持久链接。瀏覽器默認在連接不活跃300秒(5分钟)后过时關閉,亦對持久连接的最大个数(每个服务器,每个代理服务器,总数)加以限制。這些限制均可於配置中修改。[9]
Opera 4.0版本开始支持持久链接[10]。可以配置持久连接的最大个数(每个服务器,总数)。
参考文献
[编辑]- ^ Persistent HTTP Connections in RFC 2616 "Hypertext Transfer Protocol -- HTTP/1.1". [2012-01-19]. (原始内容存档于2011-03-07).
- ^ Apache HTTP Server - KeepAlive Directive. [2012-01-19]. (原始内容存档于2012-01-21).
- ^ Apache HTTP Server 2.0 - KeepAliveTimeout Directive. [2012-01-19]. (原始内容存档于2013-05-03).
- ^ Apache HTTP Server 2.2 - KeepAliveTimeout Directive. [2012-01-19]. (原始内容存档于2014-05-22).
- ^ Multiple (wiki). Httpd/KeepAlive. Docforge. [2010-01-30]. (原始内容存档于2010-01-06).
- ^ Zhe Wang and Pei Cao. Persistent Connection Behavior of Popular Browsers. University of Wisconsin-Madison. 1998-12-09 [2009-07-08]. (原始内容存档于2007-04-04).
- ^ IE8 speeds things up. stevesouders.com. 2008-03-10 [2009-07-17]. (原始内容存档于2009-08-10).
- ^ How to change the default keep-alive time-out value in Internet Explorer. Microsoft. 2007-10-27 [2009-07-17]. (原始内容存档于2009-07-22).
- ^ Network.http.keep-alive.timeout. mozillazine.org. [2009-07-17]. (原始内容存档于2009-06-08).
- ^ Opera 4.0 Upgrades File Exchange: Includes HTTP 1.1. Opera Software. 2000-03-28 [2009-07-08]. (原始内容存档于2010-09-10).