CentOS 7 + Nginx + rep2 で HTTPS & HTTP/2化

最近のブラウザーは、httpではなくhttpsでないと
危険なサイト扱いされて表示そのものが出来無くなってくるとの事で
rep2サイトもHTTPS化を実行


ついでにHTTP/2化も同時に実施する
HTTP/2は規格としてはHTTPSとは別立てで、HTTP Server側で一言設定を入れることでHTTPSとは別に設定が可能だが
ChromeFirefoxSafariのブラウザ側の実装として、HTTP/2はHTTPSとセットになっていないとデコードできない現実がある
その為 HTTP/2の前提条件として、HTTPSの実装が必要になる


SSL証明書については、無料の Let's Encryptを使用するとして
certbotを使用すると秘密鍵生成まで自動してくれる


Let's Encryptの非公式日本語解説サイトもあるので、一通り読んでおくと良い
(但し情報が古すぎてイマイチ使えないのだが)


CentOSの場合、EPELリポジトリcertbot パッケージが格納されているので
EPELリポジトリを導入するが、通常EPELリポジトリは導入しているのでスキップ


ちなみに今回
自宅サーバーなので、HTTPS化を実施した場合、HTTPSのPortだけ開放してHTTPのポートを閉じようと考えている
Webの色々なやり方を見ていると、HTTPSだけポート開放した例が見当たらなかったので
更新の時に試行錯誤しような予感・・・

# yum install certbot

certbot パッケージと依存関係のあるパッケージ 40個程インストール


certbotのVerは結構頻繁に変わっており、それに伴ってオプションも色々変更あるみたいなので
現行のVer(0.24.0-1.el7)のOptionを確認

# certbot --help

-------------------------------------------------------------------------------

  certbot [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ...

Certbot can obtain and install HTTPS/TLS/SSL certificates.  By default,
it will attempt to use a webserver both for obtaining and installing the
certificate. The most common SUBCOMMANDS and flags are:

obtain, install, and renew certificates:
    (default) run   Obtain & install a certificate in your current webserver
    certonly        Obtain or renew a certificate, but do not install it
    renew           Renew all previously obtained certificates that are near
expiry
    enhance         Add security enhancements to your existing configuration
   -d DOMAINS       Comma-separated list of domains to obtain a certificate for

  (the certbot apache plugin is not installed)
  --standalone      Run a standalone webserver for authentication
  (the certbot nginx plugin is not installed)
  --webroot         Place files in a server's webroot folder for authentication
  --manual          Obtain certificates interactively, or using shell script
hooks

   -n               Run non-interactively
  --test-cert       Obtain a test certificate from a staging server
  --dry-run         Test "renew" or "certonly" without saving any certificates
to disk

manage certificates:
    certificates    Display information about certificates you have from Certbot
    revoke          Revoke a certificate (supply --cert-path)
    delete          Delete a certificate

manage your account with Let's Encrypt:
    register        Create a Let's Encrypt ACME account
  --agree-tos       Agree to the ACME server's Subscriber Agreement
   -m EMAIL         Email address for important account notifications

More detailed help:

  -h, --help [TOPIC]    print this message, or detailed help on a topic;
                        the available TOPICS are:

   all, automation, commands, paths, security, testing, or any of the
   subcommands or plugins (certonly, renew, install, register, nginx,
   apache, standalone, webroot, etc.)
-------------------------------------------------------------------------------

こんな感じで実行すれば取り敢えず大丈夫かな
dry-runで試行

certbot certonly --dry-run --webroot --http-01-port 8080 -w <DocumentRoot> -d <Domain> -m <Mail Address>

HTTPのportを変更していたので、http-01-portオプションを指定したのだが、確かにcertbot は 8080でポート確認しているのに
アクセスしてきてるのがデフォルトの 80 とか
なんだ、このバカ仕様?


これじゃPort変更しているサイトは全てダメなんじゃないかと思ったり
という所で一旦終了


なんか素直に安い証明書調達したほうが楽な気がしてきた _no
なんだけど、mydns.jpだとそういうわけにもいかないしなあ


追記
サイトによっては、Nginx側にサーバーチェック用ディレクトリ設定を入れる必要があると記載がある

    location ^~ /.well-known/acme-challenge/ {
      root /usr/share/nginx/html/.well-known;

certbot実行中にディレクトリが作成されるのは確認していたけど、アクセス権の絡みで失敗していたんかな??
改めて後日検証してみるか

CentOS 7 + Nginx + rep2 で HTTPS & HTTP/2化 (SSLなう!編)

そんなわけで、取り敢えず SSLなう!で証明書は取得して
Nginxを HTTPS & HTTP/2化まではやってみようかと


MyDNS.jpは、2018/9/1時点ではWebUIからの_acme-challengeレコード投入に対応しなくなった模様
(正確には、投入できるけど検索できない)
代わりにphpスクリプトを提供
-> http://d.hatena.ne.jp/TKX/20180902#p1

MyDNS.jpはDNS情報を変更する事は可能なので、DNS認証(dns-01)でドメイン確認までは実施


mydns.jpのコントロールパネルから "_acme-challenge"のホスト名で、TXTレコードを作成

# dig @8.8.8.8 _acme-challenge.<domain> any

を何回か実行して結果が帰ってきたら、確認ボタンを押下すればOK


自分のServerで

# openssl genrsa 2048 > server_private_key.pem

を実行して、サーバー秘密鍵を生成してサイトに貼り付ければ
サーバー証明書 / 中間証明書 / フル証明書
が生成/発行される


Nginxでは使用するのは、サーバー秘密鍵とフル証明書の2つ
/etc/nginx/ssl 以下に格納


DH鍵交換に使用するパラメータファイルを作成する
Nginx 1.12.x以降はこのパラメーターファイルとconfへの指定が必須になった

# openssl dhparam 2048 -out /etc/nginx/ssl/dhparam.pem



nginxのドメイン毎の confファイルは以下な感じで (FastCGI絡みもSSL設定の追記が必要)

server {
    listen       <Port> ssl http2;
    server_name  <Domain>;

    ssl_certificate /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/server_private_key.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets on;

    # modern configuration. tweak to your needs.
    ssl_protocols 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;

    # DH Key Parameter
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    # add_header Strict-Transport-Security max-age=15768000;

    # OCSP Stapling ---
    # fetch OCSP records from URL in ssl_certificate and cache them
    ssl_stapling on;
    ssl_stapling_verify on;

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

    location / {
        root   /var/www/html;
        index  index.html index.htm index.php;
    }

    #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   /var/www/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           /var/www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  HTTPS             on;
        fastcgi_param  SSL_PROTOCOL      $ssl_protocol;
        fastcgi_param  SSL_CIPHER        $ssl_cipher;
        fastcgi_param  SSL_SESSION_ID    $ssl_session_id;
        fastcgi_param  SSL_CLIENT_VERIFY $ssl_client_verify;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
    }

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

基本的に、HTTPポートは開放せず HTTPSポートのみ空ける方針なので

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

configチェックして問題なければ

# systemctl reload nginx

Reloadして再構成して終了


後は、ブラウザーから https://でアクセスして緑色の鍵マークが表示されればOK


追記 : SSL CipherSuiteの指定については
Mozilla SSL Configuration Generatorを参考に構成しています


余談
Qualys SSL Labs の SSL Server Testは、デフォルトの 443しか対応していないようで
Port指定しても "Port 8443 is not supported"といった感じ