1. 程式人生 > >使用 nghttp2 除錯 HTTP/2 流量

使用 nghttp2 除錯 HTTP/2 流量

提醒:本文最後更新於 689 天前,文中所描述的資訊可能已發生改變,請謹慎使用。

在本站之前的文章中,我介紹過兩種檢視 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:HTTP/2 客戶端;
  • nghttpd:HTTP/2 服務端;
  • nghttpx:HTTP/2 代理,提供 HTTP/1、HTTP/2 等協議之間的轉換;
  • h2load: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

nghttp 做為一個功能完整的 HTTP/2 客戶端,非常適合用來檢視和除錯 HTTP/2 流量。它支援的引數很多,通過官方文件或者 nghttp -h 都能檢視。最常用幾個引數如下:

  • -v, --verbose,輸出完整的 debug 資訊;
  • -n, --null-out,丟棄下載的資料;
  • -a, --get-assets,下載 html 中的 css、js、image 等外鏈資源;
  • -H, --header=<HEADER>,新增請求頭部欄位,如 -H':method: PUT'
  • -u, --upgrade,使用 HTTP 的 Upgrade 機制來協商 HTTP/2 協議,用於 h2c,詳見下面的例子;

以下是使用 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

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 的更多使用心得以後結合實際案例再寫。

--EOF--

提醒:本文最後更新於 689 天前,文中所描述的資訊可能已發生改變,請謹慎使用。