LoginSignup
3
2

More than 5 years have passed since last update.

パッケージのみで CentOS の Nginx を HTTP/2 対応にする

Last updated at Posted at 2017-12-03

HTTP/2

CentOS 7.4 から OpenSSL が 1.0.2 にバージョンアップされたため、完全にパッケージのみで Nginx を HTTP/2 に対応させることが可能になりました。

Apache は「パッケージのみで CentOS の Apache を HTTP/2 対応にする」を参照してください。

Nginx

Nginx は 1.9.5 から HTTP/2 がサポートされました。1

リポジトリ

EPEL リポジトリおよび Nginx 公式リポジトリのいずれでも Nginx 1.12.2 がインストールされますが、設定ファイルなどの内容が異なります。

EPEL リポジトリ

yum -y install epel-release && yum -y update && yum -y install nginx
sed -i'/^#    / s/^#//' /etc/nginx/nginx.conf

EPEL リポジトリで 1.12.2 がインストールできます。
EPEL リポジトリの設定ファイル /etc/nginx/nginx.conf には SSL の設定例がコメントアウトされて記載されているので、これをアンコメントしてそのまま利用します。

Nginx 公式リポジトリ

cat << "_EOF_" > /etc/yum.repos.d/nginx.repo && yum -y install nginx
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
_EOF_
cat << "_EOF_" > /etc/nginx/conf.d/default-ssl.conf
server {
    listen       443;

    location / {
        root   /usr/share/nginx/html;
        index  index.html;
    }

    ssl on;
    ssl_certificate     /etc/pki/nginx/server.crt;
    ssl_certificate_key /etc/pki/nginx/private/server.key;
}
_EOF_

Nginx 公式リポジトリの設定ファイルには SSL の設定例が記載されていないため、新たに設定ファイルを追加して記述します。

SSL 証明書生成

mkdir -p /etc/pki/nginx/private
make -f /etc/pki/tls/certs/Makefile /etc/pki/nginx/server.crt
openssl rsa -in /etc/pki/nginx/server.key -out /etc/pki/nginx/server.key
mv -v /etc/pki/nginx/{,private/}server.key

openssl パッケージに含まれる Makefile でテスト証明書が作成できます。

FirewallD

firewall-cmd --add-service=http{,s} --permanent && firewall-cmd --reload && firewall-cmd --list-services

起動と自動起動設定

systemctl start nginx && systemctl enable $_

ブラウザで確認

nginx.png

デベロッパーツールの Network タブで Protocol が h2 になっているのが確認できます。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2