http2とは
HTTP/2とは、Webサイトの表示速度を向上させるための新しい通信プロトコルです。
HTTP/1.1の課題を解決し、より高速で効率的なデータ転送を実現します。
主な機能として、多重化、ヘッダー圧縮、サーバープッシュなどがあります
nginx で http2 の条件
nginx の条件
nginx では、バージョン 1.9.5 以降が対応しています。
古いバージョンならば、バージョンアップを行う必要があります。
HTTPSで通信
HTTP/2 を実現するための主な条件は、HTTPS (TLS/SSL) での通信と、サーバーとブラウザの両方で HTTP/2 がサポートされていることです。
nginx 設定
HTTP/2の設定は、とても簡単で、
nginx.conf のlistenディレクティブにhttp2を追加:
listenディレクティブにhttp2オプションを追加します。HTTPS (443番ポート) での設定が一般的です。
server {
listen 443 ssl http2;
...
}
具体的には、ssl の後ろに http2 を追記するだけです。
設定の確認と適応
nginx -t
systemctl restart nginx.service
検証
http/1.1
# curl -I https://example.com
でHTTP/1.1 を確認しました。
% ab -n 100 -c 100 https://example.com/
でサイトの速さの検証です。
# curl -I https://example.com
HTTP/1.1 200 OK
Server: nginx/1.22.1
% ab -n 100 -c 100 https://example.com/
Server Software: nginx/1.22.1
Server Hostname: example.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-AES256-GCM-SHA384,256,256
Server Temp Key: ECDH X25519 253 bits
TLS Server Name: example.com
Document Path: /
Document Length: 134109 bytes
Concurrency Level: 100
Time taken for tests: 0.431 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 13450700 bytes
HTML transferred: 13410900 bytes
Requests per second: 232.19 [#/sec] (mean)
Time per request: 430.681 [ms] (mean)
Time per request: 4.307 [ms] (mean, across all concurrent requests)
Transfer rate: 30499.25 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 64 235 18.5 235 269
Processing: 68 110 24.4 115 293
Waiting: 2 31 12.1 29 60
Total: 304 344 15.6 350 361
Percentage of the requests served within a certain time (ms)
50% 350
66% 354
75% 356
80% 357
90% 358
95% 359
98% 361
99% 361
100% 361 (longest request)
# curl -I https://example.com
でHTTP/2 を確認しました。
% ab -n 100 -c 100 https://example.com/
でサイトの速さの検証です。
http/2
# curl -I https://example.com
HTTP/2 200
server: nginx/1.22.1
% ab -n 100 -c 100 https://example.com/
Server Software: nginx/1.22.1
Server Hostname: blog.i-mndks.info
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-AES256-GCM-SHA384,256,256
Server Temp Key: ECDH X25519 253 bits
TLS Server Name: blog.i-mndks.info
Document Path: /
Document Length: 134109 bytes
Concurrency Level: 100
Time taken for tests: 0.375 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 13450700 bytes
HTML transferred: 13410900 bytes
Requests per second: 266.69 [#/sec] (mean)
Time per request: 374.973 [ms] (mean)
Time per request: 3.750 [ms] (mean, across all concurrent requests)
Transfer rate: 35030.39 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 11 230 24.2 229 273
Processing: 82 114 23.7 115 310
Waiting: 1 33 12.9 32 69
Total: 321 344 12.0 349 359
Percentage of the requests served within a certain time (ms)
50% 349
66% 353
75% 355
80% 356
90% 356
95% 357
98% 358
99% 359
100% 359 (longest request)
http1.1 と http2 を並べて表示
結果は、
# curl -I https://example.com
HTTP/1.1 200 OK
Server: nginx/1.22.1
% ab -n 100 -c 100 https://example.com/
Server Software: nginx/1.22.1
Server Hostname: example.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-AES256-GCM-SHA384,256,256
Server Temp Key: ECDH X25519 253 bits
TLS Server Name: example.com
Document Path: /
Document Length: 134109 bytes
Concurrency Level: 100
Time taken for tests: 0.431 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 13450700 bytes
HTML transferred: 13410900 bytes
Requests per second: 232.19 [#/sec] (mean)
Time per request: 430.681 [ms] (mean)
Time per request: 4.307 [ms] (mean, across all concurrent requests)
Transfer rate: 30499.25 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 64 235 18.5 235 269
Processing: 68 110 24.4 115 293
Waiting: 2 31 12.1 29 60
Total: 304 344 15.6 350 361
Percentage of the requests served within a certain time (ms)
50% 350
66% 354
75% 356
80% 357
90% 358
95% 359
98% 361
99% 361
100% 361 (longest request)
# curl -I https://example.com
HTTP/2 200
server: nginx/1.22.1
% ab -n 100 -c 100 https://example.com/
Server Software: nginx/1.22.1
Server Hostname: blog.i-mndks.info
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-AES256-GCM-SHA384,256,256
Server Temp Key: ECDH X25519 253 bits
TLS Server Name: blog.i-mndks.info
Document Path: /
Document Length: 134109 bytes
Concurrency Level: 100
Time taken for tests: 0.375 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 13450700 bytes
HTML transferred: 13410900 bytes
Requests per second: 266.69 [#/sec] (mean)
Time per request: 374.973 [ms] (mean)
Time per request: 3.750 [ms] (mean, across all concurrent requests)
Transfer rate: 35030.39 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 11 230 24.2 229 273
Processing: 82 114 23.7 115 310
Waiting: 1 33 12.9 32 69
Total: 321 344 12.0 349 359
Percentage of the requests served within a certain time (ms)
50% 349
66% 353
75% 355
80% 356
90% 356
95% 357
98% 358
99% 359
100% 359 (longest request)
ほぼ変化なし、
あれ!
なんで?