自サイトで証明書エラーが発生した

2025年4月26日

■ 結論

1: firewallの解除

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Nginx HTTP                 DENY        Anywhere
Nginx HTTPS                ALLOW       Anywhere
22                         LIMIT       Anywhere
Nginx HTTP (v6)            DENY        Anywhere (v6)
Nginx HTTPS (v6)           ALLOW       Anywhere (v6)
22 (v6)                    LIMIT       Anywhere (v6)

HTTP通信がDENYなので、ALLOWにする。

$ sudo ufw allow 'Nginx HTTP'
Rule updated
Rule updated (v6)

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere
Nginx HTTPS                ALLOW       Anywhere
22                         LIMIT       Anywhere
Nginx HTTP (v6)            ALLOW       Anywhere (v6)
Nginx HTTPS (v6)           ALLOW       Anywhere (v6)
22 (v6)                    LIMIT       Anywhere (v6)

HTTP通信がALLOWになった。

2. nginx.confの修正

server {
    listen       80;
    listen  [::]:80;
    server_name  gungi.2410.dev;

    location / {
        return 301 https://$host$request_uri;
    }
    location /.well-known/acme-challenge/ {
        root /var/■■■/_letsencrypt;
    }
}

これまで80番portへのアクセスをすべてhttpsにリダイレクトしていた。証明書更新に必要なアクセス(location /.well-known/acme-challenge/{~~})だけ切り分けた。

3. 証明書の更新

$ sudo certbot renew --webroot --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /■■■■■■■■■■■■■.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for gungi.2410.dev

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
  /■■■■■■■■■■■■■■■■■■■/gungi.2410.dev/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

更新テストに成功した。--dry-runを消して十個すると、実際に証明書の更新処理が走る。

■ 調査経緯

サーバを立てたまましばらく放置していたら、証明書の有効期間が切れたまま更新されていなかった。


 証明書を自動更新にしたはずなんだけど、サーバ運用するの初めてだからよくわかってない。
なんでだ? と思いながらサーバを確認。
$ sudo certbot certificates

証明書のExpiry DateにINVALID: EXPIREDの記載が確認できる。

前回サーバを建てる際に参考にした記事や、似たような証明書問題にあたって対応したブログを調べる。

  • nginxでhttpsサイトを公開しよう! (https://qiita.com/Kuroi_Cc/items/ec2e9b9e20b268f0d155)
  • (備忘)Dify - 証明書期限切れで「保護されていない通信」となった話(Let's Encrypt) (https://note.com/ko_yamazaki/n/n7242fdf2a1dd)
  • Ubuntu 20.04にNginxをインストールする方法 (https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04-ja)
とりあえず、$ sudo certbot renew --webroot --dry-runで証明書を手動更新してみる。
    $ sudo certbot renew --webroot --dry-run
    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Processing /■■■■■■■■■■■■■.conf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Simulating renewal of an existing certificate for gungi.2410.dev
    
    Certbot failed to authenticate some domains (authenticator: webroot). The Certificate Authority reported these problems:
      Domain: gungi.2410.dev
      Type:   connection
      Detail: 35.72.40.212: Fetching http://gungi.2410.dev/.well-known/acme-challenge/■■■■■■■■■■■■_■■■■■■■■: Timeout during connect (likely firewall problem)
    
    Failed to renew certificate gungi.2410.dev with error: Some challenges have failed.
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    All simulated renewals failed. The following certificates could not be renewed:
      /■■■■■■■■■■■■/gungi.2410.dev/fullchain.pem (failure)
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    なんか「Timeout」って言ってる。
    どうやら、証明書の更新には80番portを使用しているらしい。443番portを使いたいから証明書がほしいのに、使えない443を強制していたら証明書の期限が切れたときに更新できるわけない。

    $sudo ufw ~~ でHTTP通信(80番port)を認可した。

    再度、$ sudo certbot renew --webroot --dry-runを実行。

    $ sudo certbot renew --webroot --dry-run
    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Processing /■■■■■■■■■■■■■.conf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Simulating renewal of an existing certificate for gungi.2410.dev
    
    Certbot failed to authenticate some domains (authenticator: webroot). The Certificate Authority reported these problems:
      Domain: gungi.2410.dev
      Type:   connection
      Detail: 35.72.40.212: Fetching https://gungi.2410.dev/.well-known/acme-challenge/■■■■■■■■■■■■_■■■■■■■■: Error getting validation data
    
    Failed to renew certificate gungi.2410.dev with error: Some challenges have failed.
    

    エラーの内容が変わった。「Error getting validation data」。は?

    実はHTTPサーバを建てた後、botのアクセスが酷くてフィルターを少しかけたり設定を変えていた。その影響か、80番portを解放してもHTTPサーバ側で別途アクセス制御してた。

    なんだっけ〜?と思いながらnginx.confの80番監視箇所を修正した。

    再度、$ sudo certbot renew --webroot --dry-runを実行して、「success」の文字を確認。--dry-runを消してコマンドを実行して、証明書の更新完了。


コメント