在本站之前的文章中,我介绍过两种查看 HTTP/2 流量的方法:1)在 Chrome 地址栏输入 chrome://net- internals/#http2
,使用 Chrome 自带的 HTTP/2 调试工具;2)使用 Wireshark 调试 HTTP/2 流量。
方案 1 使用方便,但受限于 Chrome 浏览器,对于 Chrome 不支持的 h2c(HTTP/2 Cleartext,没有部署 TLS 的 HTTP/2)协议无能为力。同时,这个工具显示的信息经过了解析和筛选,不够全面。而在方案 2 中,Wireshark 位于服务端和浏览器之间,充当的是中间人角色,用它查看 HTTP/2 over HTTPS 流量时,必须拥有网站私钥或者借助浏览器共享对称密钥,才能解密 TLS 流量,配置起来比较麻烦。
而本文要介绍的 nghttp2,是一个用 C 实现的 HTTP/2 库,支持 h2c。它可以做为其它软件的一部分,为其提供 HTTP/2 相关功能(例如 curl 的 HTTP/2 功能就是用的 nghttp2)。除此之外,它还提供了四个有用的 HTTP/2 工具:
本文简单介绍 nghttp、h2load 这两个工具。
nghttp2 官方文档中有详细的编译依赖说明。对于我一直在用的 Ubuntu 14.04 LTS 来说,安装依赖是一件再简单不过的事情:
sudo apt-get install g++ make binutils autoconf automake autotools-dev libtool
pkg-config zlib1g-dev libcunit1-dev libssl-dev libc-ares-dev libxml2-dev
libev-dev libevent-dev libjansson-dev libjemalloc-dev cython python3-dev
python-setuptools
解决依赖问题后,获取源码并编译安装即可:
# install spdylay
git clone https://github.com/tatsuhiro-t/spdylay.git
cd ./spdylay
autoreconf -i
automake
autoconf
./configure
make
sudo make install
cd ../
# install nghttp2 apps
git clone https://github.com/tatsuhiro-t/nghttp2.git
cd ./nghttp2
autoreconf -i
automake
autoconf
./configure --enable-app
make
sudo make install
一切妥当后,nghttp2 提供的几个工具就可以直接用了。
nghttp 做为一个功能完整的 HTTP/2 客户端,非常适合用来查看和调试 HTTP/2 流量。它支持的参数很多,通过官方文档或者 nghttp -h
都能查看。最常用几个参数如下:
-H':method: PUT'
;以下是使用 nghttp 访问 nghttp2 官网的结果。从调试信息中可以清晰看到 h2c 协商以及 Server Push 的整个过程:
nghttp -nvu http://nghttp2.org
[ 0.147] Connected
[ 0.147] HTTP Upgrade request
GET / HTTP/1.1
host: nghttp2.org
connection: Upgrade, HTTP2-Settings
upgrade: h2c
http2-settings: AAMAAABkAAQAAP__
accept: */*
user-agent: nghttp2/1.9.0-DEV
[ 0.291] HTTP Upgrade response
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c
[ 0.291] HTTP Upgrade success
[ 0.291] recv SETTINGS frame <length=12, flags=0x00, stream_id=0>
(niv=2)
[SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
[SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[ 0.291] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
(niv=2)
[SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
[SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[ 0.291] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
; ACK
(niv=0)
[ 0.291] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
(dep_stream_id=0, weight=201, exclusive=0)
[ 0.291] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
(dep_stream_id=0, weight=101, exclusive=0)
[ 0.291] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
(dep_stream_id=0, weight=1, exclusive=0)
[ 0.291] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
(dep_stream_id=7, weight=1, exclusive=0)
[ 0.291] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
(dep_stream_id=3, weight=1, exclusive=0)
[ 0.291] send PRIORITY frame <length=5, flags=0x00, stream_id=1>
(dep_stream_id=11, weight=16, exclusive=0)
[ 0.291] recv (stream_id=1) :method: GET
[ 0.291] recv (stream_id=1) :scheme: http
[ 0.291] recv (stream_id=1) :path: /stylesheets/screen.css
[ 0.291] recv (stream_id=1) :authority: nghttp2.org
[ 0.291] recv (stream_id=1) host: nghttp2.org
[ 0.291] recv (stream_id=1) user-agent: nghttp2/1.9.0-DEV
[ 0.291] recv PUSH_PROMISE frame <length=59, flags=0x04, stream_id=1>
; END_HEADERS
(padlen=0, promised_stream_id=2)
[ 0.291] recv (stream_id=1) :status: 200
[ 0.291] recv (stream_id=1) date: Mon, 07 Mar 2016 13:02:35 GMT
[ 0.291] recv (stream_id=1) content-type: text/html
[ 0.291] recv (stream_id=1) content-length: 6654
[ 0.291] recv (stream_id=1) last-modified: Mon, 29 Feb 2016 15:38:08 GMT
[ 0.291] recv (stream_id=1) etag: "56d465e0-19fe"
[ 0.291] recv (stream_id=1) link: </stylesheets/screen.css>; rel=preload; as=stylesheet
[ 0.291] recv (stream_id=1) accept-ranges: bytes
[ 0.291] recv (stream_id=1) x-backend-header-rtt: 0.00093
[ 0.291] recv (stream_id=1) server: nghttpx nghttp2/1.9.0-DEV
[ 0.291] recv (stream_id=1) via: 2 nghttpx
[ 0.291] recv (stream_id=1) x-frame-options: SAMEORIGIN
[ 0.291] recv (stream_id=1) x-xss-protection: 1; mode=block
[ 0.291] recv (stream_id=1) x-content-type-options: nosniff
[ 0.291] recv HEADERS frame <length=251, flags=0x04, stream_id=1>
; END_HEADERS
(padlen=0)
; First response header
[ 0.471] recv DATA frame <length=6654, flags=0x01, stream_id=1>
; END_STREAM
[ 0.471] recv (stream_id=2) :status: 200
[ 0.471] recv (stream_id=2) date: Mon, 07 Mar 2016 13:02:35 GMT
[ 0.471] recv (stream_id=2) content-type: text/css
[ 0.471] recv (stream_id=2) content-length: 39082
[ 0.471] recv (stream_id=2) last-modified: Mon, 29 Feb 2016 15:38:08 GMT
[ 0.471] recv (stream_id=2) etag: "56d465e0-98aa"
[ 0.471] recv (stream_id=2) accept-ranges: bytes
[ 0.471] recv (stream_id=2) x-backend-header-rtt: 0.000479
[ 0.471] recv (stream_id=2) server: nghttpx nghttp2/1.9.0-DEV
[ 0.471] recv (stream_id=2) via: 2 nghttpx
[ 0.471] recv (stream_id=2) x-frame-options: SAMEORIGIN
[ 0.471] recv (stream_id=2) x-xss-protection: 1; mode=block
[ 0.471] recv (stream_id=2) x-content-type-options: nosniff
[ 0.471] recv (stream_id=2) x-http2-push: 1
[ 0.471] recv HEADERS frame <length=61, flags=0x04, stream_id=2>
; END_HEADERS
(padlen=0)
; First push response header
[ 0.964] recv DATA frame <length=16384, flags=0x00, stream_id=2>
[ 0.964] recv DATA frame <length=16384, flags=0x00, stream_id=2>
[ 0.964] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
(window_size_increment=37620)
[ 0.964] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=2>
(window_size_increment=32768)
[ 1.538] recv DATA frame <length=6314, flags=0x01, stream_id=2>
; END_STREAM
[ 1.538] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
; ACK
(niv=0)
[ 1.538] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
(last_stream_id=2, error_code=NO_ERROR(0x00), opaque_data(0)=[])
h2load 是一个支持 HTTP/2 的压测工具,可以用来测试 HTTP/2 服务的稳定性、QPS 等信息。它支持的参数也可以通过官网文档或者
h2load -h
来查看。下面是一个简单例子:
h2load https://example.com -n 100 -c 10
starting benchmark...
spawning thread #0: 10 total client(s). 100 total requests
TLS Protocol: TLSv1.2
Cipher: ECDHE-RSA-AES128-GCM-SHA256
Application protocol: h2
progress: 10% done
progress: 20% done
progress: 30% done
progress: 40% done
progress: 50% done
progress: 60% done
progress: 70% done
progress: 80% done
progress: 90% done
progress: 100% done
finished in 589.98ms, 169.50 req/s, 2.19MB/s
requests: 100 total, 100 started, 100 done, 100 succeeded, 0 failed, 0 errored, 0 timeout
status codes: 100 2xx, 0 3xx, 0 4xx, 0 5xx
traffic: 1.29MB (1353790) total, 53.42KB (54700) headers (space savings 24.97%), 1.24MB (1295000) data
min max mean sd +/- sd
time for request: 17.63ms 73.47ms 32.06ms 10.24ms 78.00%
time for connect: 81.84ms 168.23ms 119.76ms 26.44ms 60.00%
time to 1st byte: 119.09ms 201.01ms 158.06ms 23.86ms 70.00%
req/s : 17.00 28.63 23.12 3.20 60.00%
本文先介绍这么多,有关 nghttp2 的更多使用心得以后结合实际案例再写。
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8