Question
Django-channels with supervisor, nginx and daphne not activating web sockets. How to configure them.
nginx configuration file
upstream appserver {
server unix:/home/urban/run/gunicorn.sock failtimeout=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/gunicornstart
user=urban
autostart=true
autorestart=true
redirectstderr=true
stdoutlogfile=/home/urban/logs/gunicorn-error.log
[program:serverworkers]
command=/home/urban/bin/python /home/urban/hometdj/www/manage.py runworker
directory=/home/urban/hometdj/
user=web
autostart=true
autorestart=true
redirectstderr=true
stopasgroup=true
[program:serverinterface]
command=/home/urban/bin/daphne -b 127.0.0.1 -p 8000 yourapp.asgi:channellayer
directory=/home/urban/hometdj/
autostart=true
autorestart=true
stopasgroup=true
user=web
stdoutlogfile = /home/urban/logs/gunicorn-error.log
settings.py file
ASGIAPPLICATION = “hometdj.routing.application”
CHANNELLAYERS = {
“default”: {
“BACKEND”: “channelsredis.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),
),
})