Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket error when using Elastic Beanstalk with Django channels

I am trying to get a chat app powered by django channels to work on AWS Elastic Beanstalk with a load balancer.

I am basically modifying the code from https://github.com/jacobian/channels-example to work with Elastic Beanstalk. I am able to successfully run it locally on with the command

python manage.py runserver

The problem is when I deploy it with Elastic Beanstalk, I get the following error when the chat app is launched

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

I tried solutions proposed at https://stackoverflow.com/a/29831723/3667089 but it just showed a different error code

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

I also already changed the load balancer listener port to TCP 80 and obtained a SSL certificate to change the secure listener port to SSL 443 but still get the same error.

I also read Websockets with socket.io on AWS Elastic Beanstalk but there isn't an option to configure the proxy server for Django, I think it is using Apache by default.

What am I missing for the configuration of Elastic Beanstalk to make it work?

Is there any way to change this so we can run daphne server with asgi?

like image 575
user3667089 Avatar asked Sep 09 '16 06:09

user3667089


1 Answers

I'm not on Elastic Beanstalk, but here is my configuration for a VPS. Ubuntu 14.04 with nginx and supervisor. Supervisor's job is to make sure that the server and worker process are always running. Nginx listens to port 8000 on localhost and forwards that to 8080 and 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;
    }
}

Here's what my configuration for supervisor looks like. I start the server by simply restarting supervisor 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
like image 124
Brobin Avatar answered Nov 10 '22 17:11

Brobin