こんにちは松田です。
nginxでHTTP/2を使ってみます。
各バージョン
- CentOS 7.4
- nginx 1.14.0
- openssl 1.0.2
nginxのインストール
まずはnginxをインストールします。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# nginxリポジトリ導入
vim /etc/yum.repos.d/nginx.repo
---
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=0
---
# nginxインストール
yum install nginx --enablerepo=nginx
|
※2018/08/16現在ではnginx-1.14.0-1.el7_4.ngx.x86_64が入りました。
OpenSSLのインストール
HTTP/2はSSLが必須でopensslが必要ですが、CentOS7.4にはデフォルトで入っている為、追加インストールは不要です。
1
2
3
4
5
|
# opensslのversion確認
OpenSSL 1.0.2k-fips26 Jan 2017
# もし入っていない場合はインストール
yum install openssl
|
SSL証明書の用意
今回は自己署名証明書を使います。検証目的なので自己署名で十分です。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
cd /etc/nginx/conf.d/
# 秘密鍵の作成
openssl genrsa 2048 > server.key
# CSRの作成
openssl req -new -key server.key -out server.csr
# 証明書の作成
openssl x509 -days 3650 -req -signkey server.key -in server.csr -out server.crt
# 秘密鍵のパーミッション変更
chmod 600 server.key
|
nginxの設定
設定は簡単で listen 443 ssl http2 を指定するだけでHTTP/2が有効になります。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
vim /etc/nginx/conf.d/ssl.conf
---
server {
listen 443 ssl http2;
server_namelocalhost;
access_log/var/log/nginx/access.logmain;
ssl_certificate /etc/nginx/conf.d/server.crt;
ssl_certificate_key /etc/nginx/conf.d/server.key;
location / {
root /usr/share/nginx/html;
indexindex.html index.htm;
}
}
---
|
設定確認
1
2
3
|
nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
|
nginxの起動と確認
1
2
|
# 起動
systemctl start nginx.service
|
chromeでhttps://サーバのIP/ に接続すると無事HTTP/2でアクセスできました!
chromeのデベロッパーツールで見るとちゃんとHTTP/2が使われています。