通道 Websocket 立即与 ssl 断开连接

Posted

技术标签:

【中文标题】通道 Websocket 立即与 ssl 断开连接【英文标题】:Channels Websocket immedialty disconnecting with ssl 【发布时间】:2018-08-20 21:05:38 【问题描述】:

我尝试使用 ssl 从生产服务器上的频道 docs 运行教程。 几个小时后,我设法建立了连接,但它立即断开了连接:

None - - [12/Mar/2018:17:42:22] "WSCONNECTING /ws/chat/bibou/" - -
None - - [12/Mar/2018:17:42:22] "WSCONNECT /ws/chat/bibou/" - -
None - - [12/Mar/2018:17:42:23] "WSDISCONNECT /ws/chat/bibou/" - -

我的堆栈是

ubuntu 16.04
nginx 1.10.3
channels==2.0.2
daphne==2.1.0
channels-redis==2.1.0
Twisted==17.9.0

我有教程中代码的精确复制粘贴,除了 room.html 中的这部分

var chatSocket = new WebSocket(
    'wss://' + window.location.host +
    ':8443/ws/chat/' + roomName + '/');

这是我的 nginx 配置文件

server 
    #http
    listen 80;
    server_name domain.com;
    root /usr/share/nginx/html;
    include /etc/nginx/default.d/*.conf;

    location / 
        return 301 https://$server_name$request_uri;
    


server 
    #https
    listen 443 ssl;
    listen 8443 ssl;
    server_name domain.com;
    root /usr/share/nginx/html;

    ssl_certificate "/etc/letsencrypt/live/domain.com/fullchain.pem";
    ssl_certificate_key "/etc/letsencrypt/live/domain.com/privkey.pem";
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    add_header Strict-Transport-Security "max-age=31536000";

    include /etc/nginx/default.d/*.conf;

    location /static/ 
    root /home/ubuntu;
    

    location /media/ 
        root /home/ubuntu;
    

    location / 
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://unix:/home/ubuntu/tlebrize/Project.sock;
    

    location /ws/ 
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

        proxy_pass http://unix:/home/ubuntu/tlebrize/Daphne.sock;
    

我用daphne -u Daphne.sock Project.asgi:application -v 3运行达芙妮

我也尝试绕过 nginx 并使用sudo daphne -e ssl:8443:privateKey=/etc/letsencrypt/live/domain.co/privkey.pem:certKey=/etc/letsencrypt/live/domain.co/fullchain.pem Project.settings:CHANNEL_LAYERS 但我得到了相同的结果。

消息聊天套接字意外关闭的前中断,错误代码1011 (Internal Error)并且没有原因。

【问题讨论】:

【参考方案1】:

我遇到了这个问题,因为我忘记将CHANNEL_LAYERS 包含到settings.py

服务器甚至能够在断开连接之前发送 1-2 条消息。

这导致通过 nginx 连接时出现错误 1011,而在没有 https/wss 的情况下直接连接时导致错误 1006。我尝试了 uvicorn 和 daphne。

【讨论】:

【参考方案2】:

我设法让它工作,这是 nginx 和/或使用 ReconnectingWebSocket 的问题。这是我的整个工作配置: nginx

server 
#http
listen 80;
server_name domain.co;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;

location / 
    return 301 https://$server_name$request_uri;



server 
#https
listen 443 ssl;
server_name domain.com;
root /usr/share/nginx/html;

ssl_certificate "/etc/letsencrypt/live/domain.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/domain.com/privkey.pem";
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

add_header Strict-Transport-Security "max-age=31536000";

include /etc/nginx/default.d/*.conf;

location /static/ 
root /home/ubuntu;


location /media/ 
    root /home/ubuntu;


location /ws/ 
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_pass http://127.0.0.1:8443;


location / ...

达芙妮 sudo /home/ubuntu/venv/bin/daphne -e ssl:8443:privateKey=/etc/letsencrypt/live/domain.com/privkey.pem:certKey=/etc/letsencrypt/live/domain.com/fullchain.pem Project.asgi:application -v 3

js

var chatSocket = new ReconnectingWebSocket(
    'wss://' + window.location.host +
    ':8443/ws/chat/' + roomName + '/');

【讨论】:

以上是关于通道 Websocket 立即与 ssl 断开连接的主要内容,如果未能解决你的问题,请参考以下文章

Django 通道 websocket 连接和断开连接(Nginx + Daphne + Django + Channels)

客户端立即连接和断开连接没有错误消息?

握手后 Websocket 立即断开连接(鳄梨酱)

Websocket PHP:从客户端发送到服务器的立即断开连接和奇怪的消息字符串

websocket不断断开连接

websocket基本概念