はじめに
本記事は、前記事の続きとなります。
前回は、DockerComposeを使用してリバプロを立てた後、Let's Encryptの証明書を発行し、CertBotを使用したHTTP-01チャレンジの認証から証明書の発行まで行いました。
DockerコンテナでCertbotを使ってHTTP-01 チャレンジする方法(無停止更新の準備)
今回は、発行した証明書を使ってHTTPSでアクセスするところまで記します。
証明書発行からHTTPSでアクセスするまで
証明書を移動させる
CertBotにて発行した証明書は通常、/etc/letsencrypt/archive
に追加されます。
volumes:
・・・・
- ./certbot/conf/archive:/etc/letsencrypt/archive
- ./certbot/conf/live:/etc/letsencrypt/live
このままだと、リバースプロキシで証明書が参照できないので、
コンテナで立っているリバースプロキシのディレクトリに移動させます。
sudo mv /etc/letsencrypt/archive/example(domain) /home/docker/reverse-proxy/certbot/conf/archive/
シンボリックリンクを移動させる
証明書と同じく、証明書のシンボリックリンク(参照)を移動させます。
sudo mv /etc/letsencrypt/live/example(domain) /home/docker/reverse-proxy/certbot/conf/live/
シンボリックリンクの参照パスは、相対パスなので変更する必要はないです。
権限の変更
基本的に所有者や権限は環境によって異なるとは思いますが、秘密鍵などの機密ファイルは、権限を最小限にしましょう。
パス | 権限 |
---|---|
reverse-proxy/certbot/conf/archive/example(domain) | 700(drwx------) |
同上配下のcert1.pem・chain1.pem・fullchain1.pem | 644(-rw-r--r--) |
同上配下のprivkey1.pem | 600(-rw-------) |
reverse-proxy/certbot/conf/live/example(domain) | 700(drwx------) |
証明書の更新設定をする
証明書の更新設定を行わないと/etc/letsencrypt/archive/example(domain)
に入ってしまいます。
証明書のシンボリックリンクの(アーカイブ)や、証明書の保管先のパスを変更する必要があります。
・/etc/letsencrypt/renewal/example(domain).conf
archive_dir = /etc/letsencrypt/archive/example(domain)
cert = /etc/letsencrypt/live/example(domain)/cert.pem
privkey = /etc/letsencrypt/live/example(domain)/privkey.pem
chain = /etc/letsencrypt/live/example(domain)/chain.pem
fullchain = /etc/letsencrypt/live/example(domain)/fullchain.pem
現在、上記のようになっているのでそれぞれパスを変更します。
archive_dir = /home/docker/reverse-proxy/certbot/conf/archive/example(domain)
cert = /home/docker/reverse-proxy/certbot/conf/live/example(domain)/cert.pem
privkey = /home/docker/reverse-proxy/certbot/conf/live/example(domain)/privkey.pem
chain = /home/docker/reverse-proxy/certbot/conf/live/example(domain)/chain.pem
fullchain = /home/docker/reverse-proxy/certbot/conf/live/example(domain)/fullchain.pem
そして、以下コマンドを実行してテストを行います。
sudo certbot renew --dry-run
シミュレーションは問題なく完了しました。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example(domain).conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for example(domain)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/home/docker/reverse-proxy/certbot/conf/live/example(domain)/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
特定のドメインの証明書更新の強制は、以下コマンドで可です。
※一定時間のリクエストリミットがあるため注意
sudo certbot renew --cert-name example(domain) --force-renewal
証明書の更新確認をする
以下コマンドを実行することで、証明書が更新されたか確認できます。
notBeforeが発行日時、notAfterが有効期限です。
sudo openssl x509 -startdate -enddate -noout -in /home/docker/reverse-proxy/certbot/conf/live/example(domain)/fullchain.pem
notBefore=Jun 22 12:34:53 2024 GMT
notAfter=Sep 20 12:34:52 2024 GMT
更新前の証明書は削除されません。
気になる方は古い証明書を削除するスクリプト等を仕込みましょう。
archive/xxx1がxxx2になるイメージです。
なお、シンボリックリンクは自動でxxx1からxxx2に変更されます。
httpsでアクセスさせる
それでは次に、HTTPSでアクセスできるようにnginxの設定ファイルを書き換えます。
/home/docker/reverse-proxy/certbot/conf.d/reverse-proxy.conf
server {
listen 80;
server_name example(domain);
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
proxy_pass http://webapp01-web-1/;
proxy_redirect off;
}
}
現在、httpアクセスの場合の記述のみになっています。
これにhttpsでアクセスあったときの転送先等を以下のように追加していきます。
ssl_certificate、ssl_certificate_keyにそれぞれ証明書や秘密鍵を指定します。
本記事では外部プロキシ間のみSSL化しているため、proxyパスは、HTTP指定しています。
HTTPでアクセスがあった場合は、301リダイレクトさせています。
server_name example(domain);
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example(domain);
ssl_certificate /etc/letsencrypt/live/example(domain)/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example(domain)/privkey.pem;
location / {
proxy_pass http://webapp01-web-1/;
proxy_redirect off;
}
}
これで、httpアクセスがあれば、httpsに301リダイレクトするようになりました。
おわりに
鍵やディレクトリのアクセス権は慎重に適切に設定したいです。
もしも更新で失敗したり、証明書が適切に更新されない(別のディレクトリに生成等)場合は、以下を確認しましょう。
・アクセス権・所有者
コンテナ内、ホストともに適切に設定されているか。
・renewalの設定(conf)
アーカイブや証明書類のパス指定が正しいか。
・liveのシンボリックリンクのパス
コンテナ内、ホストともに参照先に辿り着けるか。(パスが異なる場合は、相対パス指定)
関連記事
https://it.hasethblog.com/archives/286
Docker ComposeのリバースプロキシでHTTP-01 チャレンジからHTTPSでアクセスするまでの方法。