将Elastic Beanstalk与Django频道一起使用时出现Websocket错误

时间:2016-09-09 06:36:29

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

我正在尝试使用由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服务器?

1 个答案:

答案 0 :(得分:0)

我不在Elastic Beanstalk上,但这是我对VPS的配置。 Ubuntu 14.04与nginx和主管。主管的工作是确保服务器和工作进程始终在运行。 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