python - https 背后的 django channel

标签 python django ssl wss django-channels

在我安装 letsencript ssl 之前,Django-channels websocket 在 AWS 服务器上运行良好。我尝试了另一个证书,但 wss 不工作。我看到这个在线部署表明 channel 可以在 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 的服务器建立连接。

有什么建议吗? 谢谢。

最佳答案

我建议按以下方式更改您的内容。

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;
  }

}

关于python - https 背后的 django channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427573/

相关文章:

python - 在Python中,如何卸载生成的类

python - 日期时间与我的格式不匹配

python - 使用 Django 从 View /对象列表中获取随机项目

c# - SignalR - 无法为 SSL/TLS 安全通道建立信任关系

python - 我应该使用哪个版本的 Python 进行 Web 开发?

python - 奇怪的线程行为

python - 以 Django 形式插入标题

django - 创建模型实例时未设置BooleanField默认值

java - CXF- HTTPS,无法连接到自签名服务器

authentication - Paw - 支持 https 相互(客户端证书)身份验证?