Question

Django-channels with supervisor, nginx and daphne not activating web sockets. How to configure them.

Posted October 31, 2018 3.1k views
NginxUbuntuNetworkingDjangoUbuntu 16.04

nginx configuration file

upstream appserver {
server unix:/home/urban/run/gunicorn.sock fail
timeout=0;
}
upstream channels-backend {
server unix:/home/urban/run/daphne.sock;
}
server {

# add here the ip address of your server
# or a domain pointing to that ip (like example.com or www.example.com)
server_name hometadka.com www.hometadka.com 206.189.130.189;

keepalive_timeout 5;
client_max_body_size 4G;

access_log /home/urban/logs/nginx-access.log;
error_log /home/urban/logs/nginx-error.log;

location /static/ {
    alias /home/urban/homet_dj/static/;
}

# checks for static file, if not found proxy to app
location / {
    try_files $uri @proxy_to_app;
}

location /ws/ {
tryfiles $uri @proxyto_ws;
}

location @proxytows {
proxy_pass http://channels-backend;

    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;

}

Supervisor file;

[program:hometdj]
command=/home/urban/bin/gunicorn
start
user=urban
autostart=true
autorestart=true
redirectstderr=true
stdout
logfile=/home/urban/logs/gunicorn-error.log

[program:serverworkers]
command=/home/urban/bin/python /home/urban/homet
dj/www/manage.py runworker
directory=/home/urban/hometdj/
user=web
autostart=true
autorestart=true
redirect
stderr=true
stopasgroup=true

[program:serverinterface]
command=/home/urban/bin/daphne -b 127.0.0.1 -p 8000 yourapp.asgi:channel
layer
directory=/home/urban/hometdj/
autostart=true
autorestart=true
stopasgroup=true
user=web
stdout
logfile = /home/urban/logs/gunicorn-error.log

settings.py file

ASGIAPPLICATION = “hometdj.routing.application”

CHANNELLAYERS = {
“default”: {
“BACKEND”: “channels
redis.core.RedisChannelLayer”,
“CONFIG”: {
“hosts”: [(‘127.0.0.1’, 6379)],
},
},
}

routing.py file

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter

from notify.routing import notification_urlpatterns

application = ProtocolTypeRouter({
“websocket”: AuthMiddlewareStack(
URLRouter(notification_urlpatterns),
),
})

Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!