EC2+nginx+django+gunicorn+letsencrypt ssl -> タイムアウト

EC2+nginx+django+gunicorn+letsencrypt ssl -> タイムアウト

私は数時間苦労しており、SE で同様の質問をたくさん調べました。しかし、問題を解決することができませんでした。

当然の疑問に答えると:

  • 私のec2(ubuntu)インスタンスにはファイアウォールがありません
  • 443ポートは受信/送信用に開いています
  • 作成されたSSLファイルsudo certbot certonly --standalone
  • 暗号化されていないリクエストは正常に動作します
  • ドメインの A レコードを EC2 インスタンスのパブリック DNS を指すように設定しました。ただし、名前空間レコードのオプションはありません。
  • そうだ、私はバカだ

以下の追加以外は nginx.conf ファイルには触れていません:

include /etc/nginx/sites-enabled/*;

/etc/nginx/sites-available/myapp.conf にある私の nginx-conf (...../sites-enabled/myapp.conf にシンボリック リンクがあります):

server {
        listen 80;
        listen 443 default ssl;   

        ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem;

        server_name mydomain;

        location /static/ {
                autoindex on;
                alias /home/ubuntu/MYAPP/static/;
        }

        location /data/ {
                autoindex off;
                alias /home/ubuntu/MYAPP/data/;
        }

        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/MYAPP/app.sock;
        }
}

私の APPS の settings.py に以下を追加しました:

ALLOWED_HOSTS = [
  'localhost',
  '127.0.0.1',
  'mywebsite.com']

今のところ、次のコメントをアウトしました

#SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
#SECURE_SSL_REDIRECT = True
#SESSION_COOKIE_SECURE = True
#CSRF_COOKIE_SECURE = True

発行nc -vz localhost 443によりConnection to localhost 443 port [tcp/https] succeeded!

netstat -ntlp与える

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -   

しかし、curl -v localhost:443結果として

*   Trying 127.0.0.1:443...
* Connected to localhost (127.0.0.1) port 443 (#0)
> GET / HTTP/1.1
> Host: localhost:443
> User-Agent: curl/7.71.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Server: nginx/1.18.0 (Ubuntu)
< Date: Fri, 08 Jan 2021 15:14:04 GMT
< Content-Type: text/html
< Content-Length: 264
< Connection: close
< 
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
* Closing connection 0

https://... 経由でドメインにアクセスしようとすると、タイムアウトになります。

編集: Steffen が指摘したように、私の curl コマンドは間違っていました。正しいコマンドは次のとおりです。

curl -v https://localhost:443

生じる

*   Trying 127.0.0.1:443...
* Connected to localhost (127.0.0.1) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /home/ubuntu/anaconda3/ssl/cacert.pem
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: CN=mydomain.com
*  start date: Jan  8 10:37:06 2021 GMT
*  expire date: Apr  8 10:37:06 2021 GMT
*  subjectAltName does not match localhost
* SSL: no alternative certificate subject name matches target host name 'localhost'
* Closing connection 0
* TLSv1.3 (OUT), TLS alert, close notify (256):
curl: (60) SSL: no alternative certificate subject name matches target host name 'localhost'
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

現在、/var/log/nginx/error.log にはエラーは表示されません。

/var/log/nginx/access.log 

ブラウザで https 経由で開こうとすると、リクエストが表示されません。http のみの場合、リクエストはログに記録されます。つまり、リクエストは nginx に届かないようですね。

私はこれまで nginx (またはサーバー/バックエンド関連のもの) を使用したことがなく、まったくの無知です。

編集2: これを機能させるにはElastic IPが必要ですか? 何か提案はありますか?

関連情報