簡體   English   中英

React請求Django API時,出現net::ERR_SSL_PROTOCOL_ERROR

[英]When React requests to Django API, net::ERR_SSL_PROTOCOL_ERROR is occured

堆棧:AWS Lightsail、React、DRF、Django、Gunicorn、NGINX

我使用 Let's Encrypt 創建了證書。

※ 我關注了這個網站:
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

因此,我可以毫無錯誤地訪問 https://mydomain。

但是當 React 調用 Django API 時,會出現 net::ERR_SSL_PROTOCOL_ERROR。

我認為我需要在 DRF、Django 中設置一些東西。

導致在 syslog 和 /var/log/nginx/error.log 中沒有記錄錯誤。

我用谷歌搜索但找不到我想要的。

我可以征求意見嗎? 謝謝你。


編輯:下面是 NGINX 的 conf 文件。

server {
    listen 80;
    server_name mydomain;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name mydomain;

    ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        root /react_project_path/build;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}

server {
    listen 8000;
    server_name mydomain;

    charset utf-8;

    location / {
        include proxy_params;
        proxy_pass https://unix:/django_project_path/gunicorn.sock;
    }

    location /static/ {
        alias /django_project_path/static/;
    }

    location /media/ {
        alias /django_project_path/media/;
    }
}

編輯:netstat 結果。

網絡統計


編輯:現在我檢查我的日志。

我只是添加了一行代碼。

server {
    listen 443 ssl http2;
    server_name mydomain;

    error_log /var/log/nginx/error.log debug;

    ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
    ...

下面是 NGINX 的 error.log。 (我在這里編輯了路徑、令牌、會話以供私人使用。)

2020/09/09 15:46:29 [debug] 3322#3322: accept on 0.0.0.0:443, ready: 0
2020/09/09 15:46:29 [debug] 3322#3322: posix_memalign: 0000556469DEEA20:512 @16
2020/09/09 15:46:29 [debug] 3322#3322: *13 accept: 218.235.196.18:8093 fd:3
2020/09/09 15:46:29 [debug] 3322#3322: *13 event timer add: 3: 60000:23210254
2020/09/09 15:46:29 [debug] 3322#3322: *13 reusable connection: 1
2020/09/09 15:46:29 [debug] 3322#3322: *13 epoll add event: fd:3 op:1 ev:80002001
2020/09/09 15:46:29 [debug] 3322#3322: *13 http check ssl handshake
2020/09/09 15:46:29 [debug] 3322#3322: *13 http recv(): 1
2020/09/09 15:46:29 [debug] 3322#3322: *13 https ssl handshake: 0x16
2020/09/09 15:46:29 [debug] 3322#3322: *13 tcp_nodelay
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL ALPN supported by client: h2
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL ALPN supported by client: http/1.1
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL ALPN selected: http/1.1
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_do_handshake: -1
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_get_error: 2
2020/09/09 15:46:29 [debug] 3322#3322: *13 reusable connection: 0
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL handshake handler: 0
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_do_handshake: 1
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL: TLSv1.2, cipher: "ECDHE-RSA-CHACHA20-POLY1305 TLSv1.2 Kx=ECDH Au=RSA Enc=CHACHA20/POLY1305(256) Mac=AEAD"
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL reused session
2020/09/09 15:46:29 [debug] 3322#3322: *13 reusable connection: 1
2020/09/09 15:46:29 [debug] 3322#3322: *13 http wait request handler
2020/09/09 15:46:29 [debug] 3322#3322: *13 malloc: 0000556469DF3390:1024
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_read: -1
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_get_error: 2
2020/09/09 15:46:29 [debug] 3322#3322: *13 free: 0000556469DF3390
2020/09/09 15:46:29 [debug] 3322#3322: *13 http wait request handler
2020/09/09 15:46:29 [debug] 3322#3322: *13 malloc: 0000556469DF3390:1024
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_read: 813
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_read: -1
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_get_error: 2
2020/09/09 15:46:29 [debug] 3322#3322: *13 reusable connection: 0
2020/09/09 15:46:29 [debug] 3322#3322: *13 posix_memalign: 0000556469E3DC90:4096 @16
2020/09/09 15:46:29 [debug] 3322#3322: *13 http process request line
2020/09/09 15:46:29 [debug] 3322#3322: *13 http request line: "GET /character/ironclad HTTP/1.1"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http uri: "/character/ironclad"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http args: ""
2020/09/09 15:46:29 [debug] 3322#3322: *13 http exten: ""
2020/09/09 15:46:29 [debug] 3322#3322: *13 posix_memalign: 0000556469EAAA50:4096 @16
2020/09/09 15:46:29 [debug] 3322#3322: *13 http process request header line
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Host: mydomain"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Connection: keep-alive"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Upgrade-Insecure-Requests: 1"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Sec-Fetch-Site: same-origin"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Sec-Fetch-Mode: navigate"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Sec-Fetch-User: ?1"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Sec-Fetch-Dest: document"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Referer: https://mydomain/"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Accept-Encoding: gzip, deflate, br"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "Cookie: csrftoken=some_data; sessionid=some_data"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "If-None-Match: W/"5f577837-8e2""
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header: "If-Modified-Since: Tue, 08 Sep 2020 12:25:27 GMT"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http header done
2020/09/09 15:46:29 [debug] 3322#3322: *13 event timer del: 3: 23210254
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 0
2020/09/09 15:46:29 [debug] 3322#3322: *13 rewrite phase: 1
2020/09/09 15:46:29 [debug] 3322#3322: *13 test location: "/"
2020/09/09 15:46:29 [debug] 3322#3322: *13 using configuration "/"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http cl:-1 max:1048576
2020/09/09 15:46:29 [debug] 3322#3322: *13 rewrite phase: 3
2020/09/09 15:46:29 [debug] 3322#3322: *13 post rewrite phase: 4
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 5
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 6
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 7
2020/09/09 15:46:29 [debug] 3322#3322: *13 access phase: 8
2020/09/09 15:46:29 [debug] 3322#3322: *13 access phase: 9
2020/09/09 15:46:29 [debug] 3322#3322: *13 access phase: 10
2020/09/09 15:46:29 [debug] 3322#3322: *13 post access phase: 11
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 12
2020/09/09 15:46:29 [debug] 3322#3322: *13 try files handler
2020/09/09 15:46:29 [debug] 3322#3322: *13 http script var: "/character/ironclad"
2020/09/09 15:46:29 [debug] 3322#3322: *13 trying to use file: "/character/ironclad" "/react_project_path/build/character/ironclad"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http script var: "/character/ironclad"
2020/09/09 15:46:29 [debug] 3322#3322: *13 trying to use dir: "/character/ironclad" "/react_project_path/build/character/ironclad"
2020/09/09 15:46:29 [debug] 3322#3322: *13 trying to use file: "/index.html" "/react_project_path/build/index.html"
2020/09/09 15:46:29 [debug] 3322#3322: *13 internal redirect: "/index.html?"
2020/09/09 15:46:29 [debug] 3322#3322: *13 rewrite phase: 1
2020/09/09 15:46:29 [debug] 3322#3322: *13 test location: "/"
2020/09/09 15:46:29 [debug] 3322#3322: *13 using configuration "/"
2020/09/09 15:46:29 [debug] 3322#3322: *13 http cl:-1 max:1048576
2020/09/09 15:46:29 [debug] 3322#3322: *13 rewrite phase: 3
2020/09/09 15:46:29 [debug] 3322#3322: *13 post rewrite phase: 4
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 5
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 6
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 7
2020/09/09 15:46:29 [debug] 3322#3322: *13 access phase: 8
2020/09/09 15:46:29 [debug] 3322#3322: *13 access phase: 9
2020/09/09 15:46:29 [debug] 3322#3322: *13 access phase: 10
2020/09/09 15:46:29 [debug] 3322#3322: *13 post access phase: 11
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 12
2020/09/09 15:46:29 [debug] 3322#3322: *13 try files handler
2020/09/09 15:46:29 [debug] 3322#3322: *13 http script var: "/index.html"
2020/09/09 15:46:29 [debug] 3322#3322: *13 trying to use file: "/index.html" "/react_project_path/build/index.html"
2020/09/09 15:46:29 [debug] 3322#3322: *13 try file uri: "/index.html"
2020/09/09 15:46:29 [debug] 3322#3322: *13 generic phase: 13
2020/09/09 15:46:29 [debug] 3322#3322: *13 content phase: 14
2020/09/09 15:46:29 [debug] 3322#3322: *13 content phase: 15
2020/09/09 15:46:29 [debug] 3322#3322: *13 content phase: 16
2020/09/09 15:46:29 [debug] 3322#3322: *13 content phase: 17
2020/09/09 15:46:29 [debug] 3322#3322: *13 content phase: 18
2020/09/09 15:46:29 [debug] 3322#3322: *13 http filename: "/react_project_path/build/index.html"
2020/09/09 15:46:29 [debug] 3322#3322: *13 add cleanup: 0000556469E3EB80
2020/09/09 15:46:29 [debug] 3322#3322: *13 http static fd: 14
2020/09/09 15:46:29 [debug] 3322#3322: *13 http set discard body
2020/09/09 15:46:29 [debug] 3322#3322: *13 http ims:1599567927 lm:1599567927
2020/09/09 15:46:29 [debug] 3322#3322: *13 http im:"W/"5f577837-8e2"" etag:"5f577837-8e2"
2020/09/09 15:46:29 [debug] 3322#3322: *13 xslt filter header
2020/09/09 15:46:29 [debug] 3322#3322: *13 HTTP/1.1 304 Not Modified
2020/09/09 15:46:29 [debug] 3322#3322: *13 write new buf t:1 f:0 0000556469EAAF10, pos 0000556469EAAF10, size: 189 file: 0, size: 0
2020/09/09 15:46:29 [debug] 3322#3322: *13 http write filter: l:1 f:0 s:189
2020/09/09 15:46:29 [debug] 3322#3322: *13 http write filter limit 0
2020/09/09 15:46:29 [debug] 3322#3322: *13 posix_memalign: 0000556469EAC6D0:512 @16
2020/09/09 15:46:29 [debug] 3322#3322: *13 malloc: 0000556469EB15C0:16384
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL buf copy: 189
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL to write: 189
2020/09/09 15:46:29 [debug] 3322#3322: *13 SSL_write: 189
2020/09/09 15:46:29 [debug] 3322#3322: *13 http write filter 0000000000000000
2020/09/09 15:46:29 [debug] 3322#3322: *13 http finalize request: 0, "/index.html?" a:1, c:2
2020/09/09 15:46:29 [debug] 3322#3322: *13 http request count:2 blk:0
2020/09/09 15:46:29 [debug] 3322#3322: *13 http finalize request: -4, "/index.html?" a:1, c:1
2020/09/09 15:46:29 [debug] 3322#3322: *13 set http keepalive handler
2020/09/09 15:46:29 [debug] 3322#3322: *13 http close request
2020/09/09 15:46:29 [debug] 3322#3322: *13 http log handler
2020/09/09 15:46:29 [debug] 3322#3322: *13 run cleanup: 0000556469E3EB80
2020/09/09 15:46:29 [debug] 3322#3322: *13 file cleanup: fd:14
2020/09/09 15:46:29 [debug] 3322#3322: *13 free: 0000556469E3DC90, unused: 0
2020/09/09 15:46:29 [debug] 3322#3322: *13 free: 0000556469EAAA50, unused: 2427
2020/09/09 15:46:29 [debug] 3322#3322: *13 free: 0000556469DF3390
2020/09/09 15:46:29 [debug] 3322#3322: *13 hc free: 0000000000000000
2020/09/09 15:46:29 [debug] 3322#3322: *13 hc busy: 0000000000000000 0
2020/09/09 15:46:29 [debug] 3322#3322: *13 free: 0000556469EB15C0
2020/09/09 15:46:29 [debug] 3322#3322: *13 reusable connection: 1
2020/09/09 15:46:29 [debug] 3322#3322: *13 event timer add: 3: 65000:23215262

這一點,

我認為流量無法到達 Django 甚至 Gunicorn,並且似乎被 NGINX 中的 SSL 錯誤阻止。

但我仍然認為我沒有足夠的信息來找到錯誤的原因。

請給我建議,謝謝。

解決方法很簡單,在listen 8000后加上'ssl'即可。

...
server {
    listen 8000 ssl;
    server_name mydomain;

    ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    charset utf-8;

    location / {
        include proxy_params;
        proxy_pass https://unix:/django_project_path/gunicorn.sock;
    }

    location /static/ {
        alias /django_project_path/static/;
    }

    location /media/ {
        alias /django_project_path/media/;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM