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/*;

我的 nginx-conf 在 /etc/nginx/sites-available/myapp.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;
        }
}

在我的應用程式的 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://... 存取我的網域會導致逾時。

編輯:正如史蒂芬指出的那樣,我的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 (或任何與伺服器/後端相關的東西),而且我完全一無所知。

EDIT2:我需要彈性 IP 才能正常運作嗎?有什麼建議麼?

相關內容