サーバー

【Let’s Encrypt・nginx】SSL証明書の更新時に、80番ポートを開放していないとエラーになる

Let’s-Encrypt-ssl-update

Let’s EncryptのSSL証明書は有効期限は3ヶ月です。そのため3ヶ月に1回はSSL証明書の更新する必要があります。

ですが、更新時にエラーが出て、失敗してしまったので、その解決した方法を残します。

Let’s EncryptからSSL証明書を発行や更新の申請をするために、クライアントツールのCertbotを使っています。

また、webサーバーはnginxを使っています。

エラー内容

まず、証明書の更新をして、出力されたエラーは以下になります。

オプションに「--dry-run」をつけて、テスト実行しています。

[root@hogehogehoge home]# certbot renew --post-hook "systemctl restart nginx" --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/hogehogehoge.jp.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org
Simulating renewal of an existing certificate for hogehogehoge.jp
Performing the following challenges:
http-01 challenge for hogehogehoge.jp
Using the webroot path /var/www for all unmatched domains.
Waiting for verification...
Challenge failed for domain hogehogehoge.jp
http-01 challenge for hogehogehoge.jp
Cleaning up challenges
Failed to renew certificate hogehogehoge.jp with error: Some challenges have failed.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
All simulated renewals failed. The following certificates could not be renewed:
  /etc/letsencrypt/live/hogehogehoge.jp/fullchain.pem (failure)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: systemctl restart nginx
1 renew failure(s), 0 parse failure(s)

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: hogehogehoge.jp
   Type:   connection
   Detail: 111.111.111.11: Fetching
   http://hogehogehoge.jp/.well-known/acme-challenge/hogehogehogehogehogehogehogehoge:
   Connection refused

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

80番ポートを設定して解決

結論として、nginxに80番ポートを設定してあげれば解決しました。

Let’s Encryptで証明書を発行・更新する仕組みとして、http経由で.well-knownにアクセスして、証明書を発行・更新します。

そのため、80番ポートをwebサーバーで設定している必要があります。

https用のポートとして443番ポートを使用してたので、80番ポートは設定していませんでした。

そもそもhttp(80番ポート)でアクセスしてきたら、httpsへリダイレクトさせる処理を入れていないのが、悪いですねぇ。。。

server {
    listen       443;
    server_name  hogehogehoge.jp
    ・
    ・
    ・
}

# 追加
server {
    listen       80;
    server_name  hogehogehoge.jp;
    if ($host = hogehogehoge.jp) {
        return 301 https://$host$request_uri;
    }
}

上記の設定をしてあげて、nginxを再起動したら、うまくいきました。

[root@hogehogehoge home]# certbot renew --post-hook "systemctl restart nginx" --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/hogehogehoge.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org
Simulating renewal of an existing certificate for hogehogehoge
Performing the following challenges:
http-01 challenge for hogehogehoge
Using the webroot path /var/www for all unmatched domains.
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/hogehogehoge/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded: 
  /etc/letsencrypt/live/hogehogehoge/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: systemctl restart nginx

参考

-サーバー
-, , ,