【问题标题】:Websocket error when using Elastic Beanstalk with Django channels将 Elastic Beanstalk 与 Django 通道一起使用时出现 Websocket 错误
【发布时间】:2017-01-17 05:33:33
【问题描述】:

我正在尝试让一个由 django 频道支持的聊天应用程序通过负载均衡器在 AWS Elastic Beanstalk 上工作。

我基本上是在修改 https://github.com/jacobian/channels-example 的代码以使用 Elastic Beanstalk。我可以使用命令在本地成功运行它

python manage.py runserver

问题是当我使用 Elastic Beanstalk 部署它时,启动聊天应用程序时出现以下错误

WebSocket connection to 'wss://mydomain.com/test/' failed: Error 
during WebSocket handshake: Unexpected response code: 200

我尝试了https://stackoverflow.com/a/29831723/3667089 提出的解决方案,但它只是显示了不同的错误代码

WebSocket connection to 'wss://mydomain.com/test/websocket' failed: 
Error during WebSocket handshake: Unexpected response code: 404

我也已经将负载平衡器侦听器端口更改为 TCP 80,并获得了 SSL 证书以将安全侦听器端口更改为 SSL 443,但仍然出现相同的错误。

我还阅读了Websockets with socket.io on AWS Elastic Beanstalk,但没有为 Django 配置代理服务器的选项,我认为它默认使用 Apache。

我在配置 Elastic Beanstalk 以使其正常工作时缺少什么?

有什么办法可以改变这种情况,以便我们可以使用 asgi 运行 daphne 服务器?

【问题讨论】:

  • 我在 AWS VPS 上运行频道,我必须使用主管才能让它工作。频道文档说您需要同时运行服务器 (venv/bin/daphne app.asgi:channel_layer) 和工作人员 (python manage.py runwoker) 才能使事情正常进行。如果您愿意,我可以在答案中发布我的 supervisord.conf,但我不确定 Elastic Beanstalk 上的工作原理。
  • @Brobin 是的,我们将不胜感激
  • 抱歉,我来晚了,但我可以知道您使用的是什么负载均衡器吗?如果是经典的,它本身就不支持 websockets。

标签: django websocket redis amazon-elastic-beanstalk django-channels


【解决方案1】:

我不在 Elastic Beanstalk 上,但这是我的 VPS 配置。带有 nginx 和主管的 Ubuntu 14.04。 Supervisor 的工作是确保服务器和工作进程始终处于运行状态。 Nginx 监听 localhost 上的 8000 端口并将其转发到 8080 和 443。

# nginx.conf
server {
    listen 8080 default_server;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 default_server ssl;
    server_name example.com;

    # ... SSL stuff

    # Send root to the ASGI server
    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    # Static Files
    location /static/ {
        root /home/ubuntu/project;
    }

    # Media Files
    location /media/ {
        root /home/ubuntu/project;
    }
}

这是我的主管配置。我通过简单地重启主管sudo service supervisor restart来启动服务器

# supervisord.conf
[program:project_server]
directory=/home/ubuntu/project/
command=/home/ubuntu/project/venv/bin/daphne project.asgi:channel_layer --port 8000 --bind 0.0.0.0

[program:project_worker]
process_name=project_worker%(process_num)s
numprocs=3
directory=/home/ubuntu/project/
command=/home/ubuntu/project/venv/bin/python /home/ubuntu/project/manage.py runworker

[group:project]
programs=project_server,project_worker

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 1970-01-01
    • 2020-02-10
    • 2020-05-06
    • 2020-05-14
    • 2015-07-15
    • 2016-03-09
    • 2017-07-25
    • 2020-11-10
    相关资源
    最近更新 更多