我知道HTTP 1.1默认开启了keep-alive,但是我想找到一种确认它是否真正起作用的方法。
有人知道从Web浏览器中测试的简单方法吗(例如如何理解Wireshark)? 我知道需要在同一个TCP连接上查找多个HTTP请求,但我不知道如何在wireshark或其他任何方式中确认它。
谢谢!
我知道HTTP 1.1默认开启了keep-alive,但是我想找到一种确认它是否真正起作用的方法。
有人知道从Web浏览器中测试的简单方法吗(例如如何理解Wireshark)? 我知道需要在同一个TCP连接上查找多个HTTP请求,但我不知道如何在wireshark或其他任何方式中确认它。
谢谢!
如Ron Garrity在ServerFault上所说,您可以这样使用Curl:
curl -Iv http://www.aptivate.org 2>&1 | grep -i 'connection #0'
如果 keep-alive 正常工作,它将输出这两行:
* Connection #0 to host www.aptivate.org left intact
* Closing connection #0
如果 keep-alive 不起作用,则会输出此行:
* Closing connection #0
如果您使用的是Windows Vista或更高版本,可以使用资源管理器。网络选项卡将列出所有打开的TCP连接及其启动的进程。打开一个带有一个选项卡的浏览器,浏览到您的页面并进行测试。
curl
访问同一个URL。curl -v http://weibo.com -o /dev/null http://weibo.com -o /dev/null
如果输出包含Re-using existing connection
,则HTTP keep-alive功能正在工作。例如:
* TCP_NODELAY set
* Connected to weibo.com (180.149.138.251) port 80 (#0)
> GET / HTTP/1.1
> Host: weibo.com
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< ...
< ...
<
{ [236 bytes data]
* Connection #0 to host weibo.com left intact
* Found bundle for host weibo.com: 0x56324121d9a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host weibo.com
* Connected to weibo.com (180.149.138.251) port 80 (#0)
> GET / HTTP/1.1
> Host: weibo.com
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< ...
< ...
<
{ [236 bytes data]
* Connection #0 to host weibo.com left intact
ab
。但是,有些HTTP服务器即使已经开启了keep-alive功能(如uwsgi),它们可能不会返回Connection: keep-alive
头文件。在这种情况下,ab
不会发送keep-alive请求,这使得ab
只能对HTTP keep-alive进行“正向”检测。ab -c 5 -n 50 -k https://www.google.com/
...
Complete requests: 50
Failed requests: 0
Keep-Alive requests: 50 # Pay attention to this line
Total transferred:
...
然后启用HTTP keep-alive。
首先,尝试在Wireshark中捕获目标网站的流量,并使用过滤器将其限制为所需内容,例如:
tcp port 80 and host targetwebsite.com
netstat
。 - Brad