如何测试HTTP Keepalive是否真正起作用

发布于 2024-10-03 16:23:52 字数 186 浏览 7 评论 0原文

我知道 HTTP keep-alive 在 HTTP 1.1 中默认处于启用状态,但我想找到一种方法来确认它确实有效。

有谁知道从网络浏览器进行测试的简单方法(例如如何理解wireshark)。我知道我需要通过同一 TCP 连接查找多个 HTTP 请求,但我不知道如何在wireshark 或任何其他方式中确认这一点。

谢谢!

I know HTTP keep-alive is on by default in HTTP 1.1 but I want to find a way to confirm that it is actually working.

Does anyone know of a simple way to test from a web browser (EG how to make sense of wireshark). I know I need to look for multiple HTTP requests over the same TCP connection but I don't know how to confirm that in wireshark or any other way.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〃安静 2024-10-10 16:23:52

正如 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

As Ron Garrity said on ServerFault, you can use Curl like this:

curl -Iv http://www.aptivate.org 2>&1 | grep -i 'connection #0'

And it outputs these two lines if keep-alive is working:

* Connection #0 to host www.aptivate.org left intact
* Closing connection #0

And if keep-alive is not working, then it just outputs this line:

* Closing connection #0
浅暮の光 2024-10-10 16:23:52

如果您使用的是 Windows Vista 或更高版本,则可以使用资源管理器。 “网络”选项卡将列出所有打开的 TCP 连接及其启动的进程。打开带有一个选项卡的浏览器,浏览到您的页面并进行测试。

If you're on Windows Vista or later, you can use Resource Manager. The Network tab will list all open TCP connections and the process they were started by. Open a browser with one tab, browse to your page, and test.

妳是的陽光 2024-10-10 16:23:52
  1. 最准确的方法是多次 curl 相同的 URL。
curl -v http://weibo.com -o /dev/null http://weibo.com -o /dev/null

如果输出包含重新使用现有连接,则 HTTP 保持活动功能正在运行。例如,

* 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
  1. 另一种快速方法是使用 ab 进行测试。但某些 HTTP 服务器可能不会返回 Connection: keep-alive 标头,即使它们已经打开 keep-alive 功能(例如 uwsgi)。在这种情况下,ab 不会发送保持活动请求。这使得 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 已启用。

  1. The most accurate way is to curl the same URL multiple times.
curl -v http://weibo.com -o /dev/null http://weibo.com -o /dev/null

If the output contains Re-using existing connection, then the HTTP keep-alive feature is working. For example,

* 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
  1. Another quick way is to test with ab. But some HTTP servers might not return the Connection: keep-alive header even when they've already turned on keep-alive feature, such as uwsgi. In such cases, ab does NOT send keep-alive requests. That makes ab can only do "positive" detection on HTTP keep-alive.
ab -c 5 -n 50 -k https://www.google.com/

If the result shows

...
Complete requests:      50
Failed requests:        0
Keep-Alive requests:    50    # Pay attention to this line
Total transferred:
...

Then the HTTP keep-alive is enabled.

梦萦几度 2024-10-10 16:23:52

首先,尝试在 Wireshark 中捕获目标网站的流量,并使用以下过滤器将其限制为您需要的流量:

tcp port 80 and host targetwebsite.com

然后在浏览器中加载页面或通过您拥有的任何工具获取该页面。如果目标网页自行刷新或刷新其中的值之一,请将其保持打开状态,直到其中至少发生一项更改。

现在您已经有了足够的数据,您可以停止 Wireshark 中的捕获过程。

您应该看到几十条记录,它们的协议应该是 TCP 或 HTTP。为了快速简单的检查,您不需要 TCP 记录。因此,让我们通过应用另一个过滤器来删除它们。窗口顶部有一个“过滤器”字段。在那里输入 http,wireshark 将隐藏除具有 HTTP 协议的记录之外的所有记录。

现在选择一条记录并查看下一级详细信息,您可以在所有记录下方的第二个框中找到这些详细信息。为了确保您看到的是正确的位置,第一行以“Frame XYZ”开头。第四行以“传输控制协议”开头。查找“SRC Port”和“DST Port:”后面的端口号。根据记录,其中一个数字属于网络服务器(通常为 80),另一个显示您终端的端口号。

现在检查几个不同的 GET 记录。要了解请求是否是 GET 记录,请检查信息列。如果您端的端口号被多次使用,则所有这些请求都是通过 HTTP keepalive 发出的。

请记住,即使网络服务器支持 keepalive,大多数浏览器也会打开多个连接。因此,不要通过仅找到一个不同的端口来结束您的评估。

First, try to capture the traffic to the target website in Wireshark and limit it to what you need with a filter like:

tcp port 80 and host targetwebsite.com

Then load the page in a browser or fetch it by any tool you have. If the target web page refreshes itself or one of the values in it, leave it open until you have at least one change in it.

Now you have enough data and you can stop capturing procedure in Wireshark.

You should see dozens of records and their protocol should be TCP or HTTP. For the purpose of your quick simple check, you will not need TCP records. So, lets remove them by applying another filter. In top of the window there is a "filter" field. Type http there, and wireshark will hide all records but those which have a HTTP protocol.

Now select a record and look at the next level of details, which you can find in the 2nd box bellow all records. Just to be sure you are looking at the right place, the first line there starts with "Frame XYZ". The fourth line starts with "Transmission Control Protocol". Look for the port numbers after "SRC Port" and "DST Port:". Depending on the record, one of these numbers belongs to the webserver (typically 80) and the other one shows port number in your end.

Now check a couple of different GET records. To know if the request is a GET record, check the Info column. If the port numbers in your end are used several times, all those requests were made through HTTP keepalive.

Remember that most browsers will open multiple connections, even if the webserver supports keepalive. So, DO NOT conclude your evaluation by finding just one different port.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文