Django Channels在HTTPS背后的工作原理

13

在安装了 letsencript SSL 之后,Django-channels Websocket 在 AWS 服务器上工作正常。我尝试了另一个证书,但 wss 不起作用。我看到了这个在线部署示例,该示例显示 channels 可以在 https 后面工作:

https://django-channels-example.herokuapp.com/

我按照 andrewgodwin 在这里的建议进行操作:

https://github.com/django/channels/issues/248

我将 daphne 指向了端口8000:

daphne -b 0.0.0.0 vp.asgi:channel_layer --port 8000 -v 2

我在JavaScript中使用了相同的端口:

chatsock = new WebSocket( ws_scheme + '://' + window.location.host + ":8000/chat" );

我的 Nginx 配置:

server {
        listen 80;
        server_name mysite.com www.example.com;
        return 301 https://www.example.com$request_uri;
}

server{
        listen 443 ssl;
        server_name mysite.com www.example.com;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        root /home/ubuntu/vp;

        access_log /var/log/nginx/guni-access.log;
        error_log /var/log/nginx/guni-error.log info;

        location /wss/ {
                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_pass http://0.0.0.0:8000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

    location / {
        proxy_pass http://0.0.0.0:8000;
        proxy_set_header    HOST    $host;
        proxy_set_header    X-Real-IP   $remote_addr;
        proxy_set_header    X-Forwarded-for $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        port_in_redirect off;
        proxy_connect_timeout 300;
    }

    location ~ /.well-known {
                allow all;
        }

    location /static/ {
        alias /home/ubuntu/vp/static/;
        expires 30d;
    }
}

我的浏览器显示:

Firefox 无法连接到 wss://example.com:8000/chat 服务器。

有什么建议吗? 谢谢。


1
据说这还不可用,但开发人员正在努力支持此功能。请参阅以下详细信息。https://github.com/django/daphne/pull/36 https://github.com/django/daphne/issues/35 - JJK
1个回答

15
我建议改变你的东西如下。 javascript:
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var chatsock = new ReconnectingWebSocket(ws_scheme + '://' + window.location.host + window.location.pathname);

nginx:

server {
 listen 443 ssl;
 server_name server.domain.com;

 ssl on;
 ssl_certificate /path_to_server_certificate.crt;
 ssl_certificate_key /path_to_server_key.key;

  ## static files (path should be changed)
  location /static/ {
    autoindex off;
    alias /blabla/static/;
  }

  ## app
  location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
  }

}

3
对于使用2号频道的任何人,这个答案在2.1.1频道(部署在Heroku上)中也适用。我只是在我的模板脚本中添加了 var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";(它在http下工作但在https下不起作用),现在一切都正常运行。 - Yobmod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接