30
HTTP/2 in nginx Yoko TAMADA @tmd45 2016-03-11 feedforce Inc.

HTTP/2 in nginx(2016/3/11 社内勉強会)

Embed Size (px)

Citation preview

Page 1: HTTP/2 in nginx(2016/3/11 社内勉強会)

HTTP/2 in nginx

Yoko TAMADA @tmd452016-03-11 feedforce Inc.

Page 2: HTTP/2 in nginx(2016/3/11 社内勉強会)

第一部:WWW 通信プロトコル基礎

     〜 HTTP/1.1 を振り返る

第二部:高速化の歩み 〜 SPDY 実験

第三部:16 年ぶりの新鋭 HTTP/2 !

今回のお話

… は、1時間くらいかかるし、あちこちで聞き

まくって飽きたのでやめました。

「やっぱり動かしてみよう」と思い立って3日、

思った以上にさっくり動いたのでそんな話と某

所で聞いた注意点など

Page 3: HTTP/2 in nginx(2016/3/11 社内勉強会)

そういえば

● HTTP/2 って必要なの?

○ もはや論じる余地なし

○ むずかしい話は端折りますけど、

HTTP/1 から HTTP/1.1 にしなかっ

た人がいますか?

■ いるかもしれんけど(涙)

○ 対応時期を考える必要はあれども、

今後絶対に対応必須です!

Page 4: HTTP/2 in nginx(2016/3/11 社内勉強会)

● 用意したサーバ

○ ConoHa■ GMO のオール SSD クラウド VPS■ メモリ 1GB, 2 Core, SSD 50GB 底辺プラン

○ CentOS 7.2○ 作業用ユーザ作成して SSH ログイ

ンしたぞ

前提

Page 5: HTTP/2 in nginx(2016/3/11 社内勉強会)

● ソースコードからビルド; nginx-build

● 安全な HTTPS 通信設定

● HTTP/2 の利用

アジェンダ

Page 6: HTTP/2 in nginx(2016/3/11 社内勉強会)

nginx-build

Page 7: HTTP/2 in nginx(2016/3/11 社内勉強会)

● nginx-build を使おう

○ https://github.com/cubicdaiya/nginx-build

● go get するので go の準備から

nginx をソースコードからビルドする

Page 8: HTTP/2 in nginx(2016/3/11 社内勉強会)

go get nginx-build

# cd /usr/local/src# wget https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz# tar -C /usr/local/ -xzf go1.6.linux-amd64.tar.gz# echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile

$ mkdir ~/golang$ echo "export GOPATH=$HOME/golang" >> ~/.bashrc$ echo "export PATH=$PATH:$GOPATH/bin" >> ~/.bashrc

$ go get -u github.com/cubicdaiya/nginx-build

Page 9: HTTP/2 in nginx(2016/3/11 社内勉強会)

● ビルドに必要なパッケージ

○ gcc○ wget, git, mercurial

● HTTP モジュールで必要になるライブラリ

○ pcre-devel /* 正規表現 */○ zlib-devel /* gzip 圧縮 */○ openssl-devel /* SSL/TLS */

yum install

Page 10: HTTP/2 in nginx(2016/3/11 社内勉強会)

# mkdir ~/nginx# vi ~/nginx/configure

Custom configuration

#!/bin/sh

./configure \--sbin-path=/usr/sbin/nginx \--conf-path=/etc/nginx/nginx.conf \--with-http_gzip_static_module \--with-http_ssl_module \--with-http_v2_module \

Page 11: HTTP/2 in nginx(2016/3/11 社内勉強会)

# vi ~/nginx/module3rd.cfg

3rd-party module

[headers-more-nginx-module]form=giturl=https://github.com/openresty/headers-more-nginx-module.gitrev=v0.29

Page 12: HTTP/2 in nginx(2016/3/11 社内勉強会)

$ mkdir work$ nginx-build -d work [-v 1.9.12] \ -c nginx/configure -m nginx/module3rd.cfgnginx-build: 0.7.1Compiler: gc go1.62016/03/09 00:39:09 Download nginx-1.9.12........2016/03/09 00:39:29 Enter the following command for install nginx.

$ cd work/nginx/1.9.12/nginx-1.9.12 $ sudo make install

build & make install

Page 13: HTTP/2 in nginx(2016/3/11 社内勉強会)

$ nginx -Vnginx version: nginx/1.9.12built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure arguments: --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-http_gzip_static_module --with-http_ssl_module --with-http_v2_module --add-module=../headers-more-nginx-module

nginx の構成確認

Page 14: HTTP/2 in nginx(2016/3/11 社内勉強会)

● 問題点

○ CentOS 7 の systemd でサービス管理す

るためのファイル nginx.service が生成さ

れない

○ systemctl enable nginx.service できない

○ 自分で作る(今回はまだやってない)

$ sudo nginx$ sudo nginx -s stop

nginx 起動と停止

Page 15: HTTP/2 in nginx(2016/3/11 社内勉強会)

HTTPS 通信設定

Page 16: HTTP/2 in nginx(2016/3/11 社内勉強会)

最低限の HTTPS 通信設定

server { listen 443 default_server ssl; server_name example.com www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

...}

/etc/nginx/conf.d/example.com.conf

Page 17: HTTP/2 in nginx(2016/3/11 社内勉強会)

余談; SSL 証明書 ~Let's Encrypt!

● 使ってみた → 超簡単

$ git clone https://github.com/letsencrypt/letsencrypt$ cd letsencrypt/$ ./letsencrypt-auto --help※ DNS 設定済み, nginx 停止中

$ ./letsencrypt-auto certonly -a standalone \ -d example.com -d www.example.com※ TUI が起動 → メアド入力&規約同意

IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will expire on 2016-06-08. To obtain a new version of the certificate in the future, simply run Let's Encrypt again.

Page 18: HTTP/2 in nginx(2016/3/11 社内勉強会)

● Mozilla SSL Configuration Generator○ https://mozilla.github.io/server-side-tls/ssl-

config-generator/

安全な HTTPS 通信設定

Page 19: HTTP/2 in nginx(2016/3/11 社内勉強会)

安全な HTTPS 通信設定

server { listen 443 default_server ssl; server_name example.net www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_session_timeout 1d; ssl_session_cache shared:SSL:5m; ssl_session_tickets off;

ssl_dhparam /etc/nginx/ssl/dhparam.pem;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256'; ssl_prefer_server_ciphers on;

add_header Strict-Transport-Security max-age=15768000;

ssl_stapling on; ssl_stapling_verify on; ssl_trusted_certificate /etc/letsencrypt/live/example.com/fullchain.pem; resolver 8.8.8.8 8.8.4.4 valid=300s; resolver_timeout 5s;

/etc/nginx/conf.d/example.com.conf

Page 20: HTTP/2 in nginx(2016/3/11 社内勉強会)

● 各ディレクティブの解説

○ 『nginx 実践入門』オススメ

■ 暗号化スイート

■ HSTS

■ 安全性と互換性のバランスについ

て(ただし書籍執筆時点)

安全な HTTPS 通信設定

Page 21: HTTP/2 in nginx(2016/3/11 社内勉強会)

HTTP/2

Page 22: HTTP/2 in nginx(2016/3/11 社内勉強会)

HTTP/2 の有効化

server { listen 443 default_server ssl http2; server_name example.com www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

...}

/etc/nginx/conf.d/example.com.conf

これだけ!

Page 23: HTTP/2 in nginx(2016/3/11 社内勉強会)

● nginx 1.9.5 〜(now 1.9.12)

● SPDY 利用しようとすると怒られる

○ --with-http_spdy_module

○ listen 443 ssl spdy;

○ その他 spdy_* ディレクティブが残ってる

とエラー

Module ngx_http_v2_module

Page 24: HTTP/2 in nginx(2016/3/11 社内勉強会)

SPDY 対応ブラウザ

Page 25: HTTP/2 in nginx(2016/3/11 社内勉強会)

HTTP/2 対応ブラウザ

Page 26: HTTP/2 in nginx(2016/3/11 社内勉強会)

● SPDY や HTTP/2 を使わない通信を行うだけ

○ 基本的には HTTP/1.1 プロトコルになる

○ HTTP/2 がだめなら SPDY/3.1 で、とはな

らない(少なくとも nginx は)

○ しかし未対応というかバグで上手く動かな

いってことはあるかもね〜〜〜

未対応ブラウザってどうなるの?

Page 27: HTTP/2 in nginx(2016/3/11 社内勉強会)

● アクセス解析ツールでエラーが出たという

話も

○ 話に上がってたのは CGI ツール

○ クローリングとか必要なサイトではツー

ルでの動作確認も忘れずに

ブラウザだけじゃないかも

Page 28: HTTP/2 in nginx(2016/3/11 社内勉強会)

便利ツール

Page 29: HTTP/2 in nginx(2016/3/11 社内勉強会)

Chrome 拡張:HTTP/2 and SPDY indicator

Page 30: HTTP/2 in nginx(2016/3/11 社内勉強会)

● ソースコードからビルド; nginx-build

● 安全な HTTPS 通信設定

● HTTP/2 の利用と注意点

● 便利; HTTP/2 and SPDY indicator

今回のお話

終──────ⓣⓜⓓ