nginxでhttps2設定

仕事の都合でローカルにhttp2を立てたのでその記録
ローカルで行う事を前提にしているので環境としてdockerを採用して証明書は自己証明書で進める。

nginxの用意

dockerでnginxのイメージからコンテナを用意

docker run -it -d --name http2 -p 80:80 -p 443:443 nginx

docker内で証明書作ったり設定変えたりするので-itを指定
httpsでの接続になるので443のポートフォワーディング設定をするが、最初はポート80番で受け付ける様になっているので確認のために80番も追加しておく

この状態でブラウザ等のhttp通信が行えるものでhttp://localhostに対してアクセスしてWelcome to nginx!が表示されればOK

f:id:YuukTsuchida:20180917234453p:plain

コンテナの立ち上げまで終わったらbashログインを設定を変更をしていく。

docker exec -it http2 bash

ただ、nginxのコンテナにはvi等普通はインストールされているものがいろいろないのでインストールしておく。

$ apt-get update
$ apt-get install vim
$ apt-get install less 
$ apt-get install openssl 

余計なパッケージインストールすることになるのであまりよくなさそうだが目的がhttp2設定なので気にしないでおく!!

nginxの設定ファイルを確認

nginx のグローバル設定は /etc/nginx/nginx.conf にある。
ひとまずこいつの中身を確認

$ less /etc/nginx/nginx.conf

# 以下 nginx.conf

user  nginx;            # nginxのプロセス起動ユーザー
worker_processes  1;    # 実行プロセス数の指定 auto とすると自動設定をしてくれる。 CPUのコア数と揃えるのが良さそう

# エラーログの指定 最初は出力先 その後にレベル
# レベルは debug, info, notice, warn, error, crit, alert, emerg がある
error_log  /var/log/nginx/error.log warn;   

# PIDが格納されるファイル 基本みることはなさそう
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;   # コネクション数
}

# httpモジュールの設定
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

この中にルーティングの設定を書けばよいのだが、別の設定ファイルに逃がすことも可能になっている。(というかデフォルトでそうなっている)
include /etc/nginx/conf.d/*.conf; となっているのが該当の設定
/etc/nginx/conf.d/.conf をおいておけば読み込んで有効にしてくれるのでルート毎でファイルを作ってやるのが良いのかも

ルーティングの設定を確認

/etc/nginx/conf.d/ を見ると default.conf が存在している。(wllcome to nginx! を表示する設定がすでにある)
今度はこいつの中身を確認

less /etc/nginx/conf.d/default.conf

# 以下 default.conf
server {
    # ポートの指定
    # default_serverを指定すると他の全てにマッチしない場合に使われるサーバー
    listen       80;  
    server_name  localhost;     # ドメインの指定

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    # URI に対してどこをルートにするか
    # ただ、ディレクトリやファイルに対して適切なアクセス権を設定してやる必要がある
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    // エラーページの設定
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

実際に設定をしてやる

中身の確認はしたので実際に設定をしていく

http2 の設定をするためには証明書が必要なので事故証明書で生成をする

$ openssl req -new -x509 -sha256 -newkey rsa:2048 -days 365 -nodes -out /etc/nginx/ssl/ssl.pem -keyout /etc/nginx/ssl/ssl.key

生成が終わったらsslの設定と同じ様な設定をする。
具体的には ssl_certificate ssl_certificate_key の追加を行い、listenにhttp2の設定をする。

server {
    listen       443 ssl http2 default_server;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    ssl_certificate /etc/nginx/ssl/ssl.pem;
    ssl_certificate_key /etc/nginx/ssl/ssl.key;

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

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

default.confを書き換えたらnginxの再起動を行う

$ nginx -s reload

再起動あとにブラウザでアクセスをして開発ツールでプロトコルがh2になっていればOK。

とりあえず設定完了。
Dockerログインしたりしていろいろ作業しているのであまりやりたくないな(これならvagrantと変わらん)
Dockerfile覚えたらそっちでの設定方法も調べるか