作者:tacy lee
写本文的目的就是想搞清除http1.1和1.0的具体区别,看过很多别人写的关于他们的概念,可能是没动手去测试,总是模模糊糊
Persistent HTTP connections
我们知道,早先的WEB网站都是由一些文字页面组成,一个页面可能就是一个简单的http请求,里面也不会嵌套css,gif类似的资源文件,所以在1.0标准实现的时候没有去考虑连接的效率(用完就丢弃,【1】),现在的页面很多都非常复杂,在里面嵌套二三十个资源文件是很正常的事情,创建二三十个tcp连接去下载这些资源,对于tcp连接的利用率非常低。
另外一方面,tcp连接打开之后,首先采用slow start算法【2】,开始先传输几个小的packet探测网络情况来确定传输速率,而且页面上嵌入的资源文件往往都比较小,也就是说如果采用http 1.0,就意味着很多的资源传输都是在slow start下完成的,效率也很低。
Persistent HTTP connections就是让连接重复利用,不需要为每个资源文件都建立一个tcp连接。也不存在slow start问题。
http规范里面对于持久连接的数量建议为2(firefox和iexplorer缺省都是采用该值),增加该值会增加服务器的压力(需要创建更多的连接),而且对于网络状况不好的情况只会导致更大的阻塞,所以不建议修改该值【3】
Clients that use persistent connections SHOULD limit the number of simultaneous connections that they maintain to a given server. A single-user client SHOULD NOT maintain more than 2 connections with any server or proxy. A proxy SHOULD use up to 2*N connections to another server or proxy, where N is the number of simultaneously active users. These guidelines are intended to improve HTTP response times and avoid congestion
* firefox 的Persistent HTTP connections设置
network.http.max-persistent-connections-per-server
* iexplorer的Persistent HTTP connections设置
http://support.microsoft.com/?kbid=282402
Pipelining
在HTTP 1.0的时候,客户端的请求都是等到前一个请求完成之后,再发送另一个请求,一个packet里面只能请求一个资源。Pipelining可以让你在一个packet里面请求多个资源,减少了packet的数量,具体请参考下面示例。
* firefox Pipelining设置
network.http.pipelining (缺省为false,推荐打开)
network.http.pipelining.maxrequests (缺省为4,最大为8,建议设置为8)
* iexplorer设置
ie没有实现pipelining
* 没有实现Pipelining的浏览器发送的packet
No. Time Source Destination Protocol Info
52 0.576526 192.168.1.61 203.81.29.137 HTTP GET /images/index_03.gif HTTP/1.1
Frame 52 (620 bytes on wire, 620 bytes captured)
Ethernet II, Src: QuantaCo_c3:16:11 (00:16:36:c3:16:11), Dst: Intel_5f:99:3a (00:0e:0c:5f:99:3a)
Internet Protocol, Src: 192.168.1.61 (192.168.1.61), Dst: 203.81.XXX.XXX (203.81.XXX.XXX)
Transmission Control Protocol, Src Port: 2320 (2320), Dst Port: http (80), Seq: 1122, Ack: 8456, Len: 566
Hypertext Transfer Protocol
GET /images/index_03.gif HTTP/1.1\r\n
Host: www.primeton.com\r\n
User-Agent: Mozilla/5.0 (Windows;U;Windows NT 5.1; en-US; rv:1.8.1.6)Gecko/20070725 Firefox/2.0.0.6\r\n
Accept: image/png,*/*;q=0.5\r\n
Accept-Language: en-us,en;q=0.5\r\n
Accept-Encoding: gzip,deflate\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n
Keep-Alive: 300\r\n
Connection: keep-alive\r\n
Referer: http://www.primeton.com/\r\n
Cookie: __utmz=33497343.1189057682.16.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); __utma=3349734
3.241348620.1172460177.1189057682.1189130143.17; __utmb=33497343; __utmc=33497343\r\n
\r\n
* 实现了Pipelining的浏览器发送的packet,可以看到,这个包里面请求了两个资源(index_03.gif和SH.jpg)
No. Time Source Destination Protocol Info
39 0.558943 192.168.1.61 203.81.29.137 HTTP GET /images/index_03.gif HTTP/1.1
Frame 39 (1514 bytes on wire, 1514 bytes captured)
Ethernet II, Src: QuantaCo_c3:16:11 (00:16:36:c3:16:11), Dst: Intel_5f:99:3a (00:0e:0c:5f:99:3a)
Internet Protocol, Src: 192.168.1.61 (192.168.1.61), Dst: 203.81.XXX.XXX (203.81.XXX.XXX)
Transmission Control Protocol, Src Port: 2309 (2309), Dst Port: http (80), Seq: 1158, Ack: 24226, Len: 1460
Hypertext Transfer Protocol
GET /images/index_03.gif HTTP/1.1\r\n
Host: www.primeton.com\r\n
User-Agent: Mozilla/5.0 (Windows;U;Windows NT 5.1; en-US; rv:1.8.1.6)Gecko/20070725 Firefox/2.0.0.6\r\n
Accept: image/png,*/*;q=0.5\r\n
Accept-Language: en-us,en;q=0.5\r\n
Accept-Encoding: gzip,deflate\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n
Keep-Alive: 300\r\n
Connection: keep-alive\r\n
Referer: http://www.primeton.com/\r\n
Cookie: __utmz=33497343.1189057682.16.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); __utma=3349734
3.241348620.1172460177.1189057682.1189130143.17; __utmb=33497343; __utmc=33497343\r\n
\r\n
Hypertext Transfer Protocol
GET /images/SH.jpg HTTP/1.1\r\n
Host: www.primeton.com\r\n
User-Agent: Mozilla/5.0 (Windows;U;Windows NT 5.1; en-US; rv:1.8.1.6)Gecko/20070725 Firefox/2.0.0.6\r\n
Accept: image/png,*/*;q=0.5\r\n
Accept-Language: en-us,en;q=0.5\r\n
Accept-Encoding: gzip,deflate\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n
Keep-Alive: 300\r\n
Connection: keep-alive\r\n
Referer: http://www.primeton.com/\r\n
Cookie: __utmz=33497343.1189057682.16.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); __utma=334973
43.241348620.1172460177.1189057682.1189130143.17; __utmb=33497343; __utmc=33497343\r\n
\r\n
chunked
http/1.0不支持chunked packet(当sender无法确定传输数据大小的时候使用),keep-alive对于http/1.0同样有效,但是不支持chuncked packet
====== 参考资料: ======
* 【1】:Nielsen, H.F., Gettys, J., Baird-Smith, A., Prud'hommeaux, E., Lie, H., and C. Lilley. [[http://www.w3.org/Protocols/HTTP/Performance/Pipeline.html|Network Performance Effects of HTTP/1.1, CSS1, and PNG]], Proceedings of ACM SIGCOMM '97, Cannes France, September 1997.[jg642]
* 【2】:Spero, S., [[http://www.w3.org/Protocols/HTTP/1.0/HTTPPerformance.html|Analysis of HTTP Performance Problems]], July 1994.
* 【3】:[[http://www.w3.org/Protocols/rfc2616|Hypertext Transfer Protocol -- HTTP/1.1]]