Django channels websocket connecting and disconnecting (Nginx + Daphne + Django + Channels)

2k Views Asked by At

I'm having problems to deploy this in a production virtual machine, with Nginx + Gunicorn + Daphne + Django. I had been testing it in a local virtual machine, and it works without a problem, but in production, the sockets is connecting and disconnecting. I attached my nginx config, asgi.py and routing.py. I use the command ````$ daphne -p 8010 project.asgi:application``` enter image description here

# Nginx config
upstream test_project {
        server localhost:8001;
}
upstream test_project_websocket {
        server localhost:8002;
}
server {
        listen 1881;

        location / {
                proxy_pass http://test_project;
        }
        location /ws/ {
                proxy_pass http://test_project_websocket;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_redirect off;
        }

        proxy_set_header Host $host;
}

#asgi.py
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'WebServer.settings')
django.setup()

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter
import Andon.routing # app.routing
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.layers import get_channel_layer


application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
            URLRouter(
                    Andon.routing.websocket_urlpatterns,
            ),
        ),
})
# routing.py
from django.urls import path, re_path
from . import consumers

from channels.layers import get_channel_layer

websocket_urlpatterns = [
    re_path(r'ws/andon/$', consumers.AndonConsumer.as_asgi()),
]

Edit 27-11-20: Didn't solvet yet, but found something interesting. If you are using old versions of redis, make sure you have this versions:

channels==3.0.2
channels-redis==2.4.1

Edit 27-11-20: partial solve I think i'm having some problems with the nginx config, because if i try to connect directly to the daphne port, it works just fine, but if i redirect trafic from nginx it doesn't

Edit 16-12-20: Solution I had an old version of nginx 1.10, I upgrade it to 1.16 and worked without any problem, using the config of this post: Config

1

There are 1 best solutions below

3
On

This is the config your looking for

# Connecting to daphne socket
upstream test_project_websocket {
        server localhost:8010;
}
....
    # Notice the "/" at then end of location & proxy_pass url
    location /ws/ {
        proxy_pass http://test_project_websocket/;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }

    location / {
         proxy_pass http://test_project;
    }