• サーバのSSL証明書が正しく更新されたなかったので調査
  • 原因はnginxの設定ミス
  • 気づくには面倒くさがらずに、出力されるログを理解して読むことが大事。

※ 内容の一部を隠してあります。

# /usr/bin/certbot-auto renew --dry-run

...
-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/xxx.conf
-------------------------------------------------------------------------------
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for xxx
Waiting for verification...
Cleaning up challenges
Attempting to renew cert (xxx) from /etc/letsencrypt/renewal/xxx.conf produced an unexpected error: Failed authorization procedure. xxx (http-01): urn:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://xxx/.well-known/acme-challenge/0oAWq_LY7XgLFdCSZbZMx_JuwnbsjnA4dPvyksIlQmU: "<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><cen". Skipping.
...

502 Bad Gateway …!

最初に入れたときはうまくいったのになぜ。。?

とりあえずログを見よう。

# /var/log/nginx/error.log

...
2018/03/02 10:24:07 [error] 7942#7942: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xxx, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "xxx"
...

あれ、5000番ポートをソケットして返そうとしている…?

対象のサービスは 3031で返すようにしているので、5000ってなんだろう。。

↓対象のサービスの設定

upstream my_apps {
  server 127.0.0.1:3031;  # <-- ここで3031番と設定しているので、5000番を返そうとしているのはなぜ。。?
}
server {
    listen       80;
    location /.well-known/ {
        root /xxx/yyyy;
    }
    location /api {
        include uwsgi_params;
        uwsgi_pass my_apps;
        break;
    }
}

server {
    listen 443 default ssl;
    ssl on;
...

もしかして

まだ見ていないファイルにnginxの設定が書かれている。。?

root@myserver $ grep -r 500 /etc/nginx/
/etc/nginx/sites-available/redash:  server 127.0.0.1:5000;
/etc/nginx/conf.d/redash.conf:#   server 127.0.0.1:5000;

redash…そういえば入れていた!

https://redash.io/

redashは今は使っていないので、設定ファイルはコメントアウトして systemctl restart nginx

改めて certbot renew --dry-run すると成功

# /usr/bin/certbot-auto renew --dry-run
...
Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/xxx/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
-------------------------------------------------------------------------------

ログは大事なことが書かれているのでしっかり読もう。


WEB U PROJECT

Freelance Web Enginner.
Python / Data / AWS / GCP


[解決] Let’s Encrypt でのSSL証明書更新で 502エラー | Web U Project