Report this

What is the reason for this report?

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

Posted on October 31, 2018

The Nginx configuration file:

upstream app_server {
    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/ {
        try_files $uri @proxy_to_ws;
    }

location @proxy_to_ws {
       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:homet_dj]
command=/home/urban/bin/gunicorn_start
user=urban
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/urban/logs/gunicorn-error.log

[program:server_workers]
command=/home/urban/bin/python /home/urban/homet_dj/www/manage.py runworker
directory=/home/urban/homet_dj/
user=web
autostart=true
autorestart=true
redirect_stderr=true
stopasgroup=true

[program:server_interface]
command=/home/urban/bin/daphne -b 127.0.0.1 -p 8000 yourapp.asgi:channel_layer
directory=/home/urban/homet_dj/
autostart=true
autorestart=true
stopasgroup=true
user=web
stdout_logfile = /home/urban/logs/gunicorn-error.log

The settings.py file:

ASGI_APPLICATION = "homet_dj.routing.application"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [('127.0.0.1', 6379)],
        },
    },
}

The 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),
        ),
})


This textbox defaults to using Markdown to format your answer.

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

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

It looks like your configuration is almost correct. However, there are a few changes and considerations that might solve your issue.

  1. Daphne and Supervisor: You are running Daphne on port 8000, but your NGINX configuration is pointing to a Unix socket file that Daphne is not configured to create. You should either point NGINX to the correct Daphne port or configure Daphne to create a Unix socket file.

In your supervisor configuration for server_interface, the Daphne command could be:

command=/home/urban/bin/daphne -u /home/urban/run/daphne.sock yourapp.asgi:channel_layer

This will make Daphne bind to the Unix socket file /home/urban/run/daphne.sock instead of binding to 127.0.0.1:8000.

  1. File Permissions: The Unix socket file /home/urban/run/daphne.sock should have the appropriate file permissions so that NGINX and Daphne can read and write to it. You might need to change the ownership and group of this file to match the user running Daphne and NGINX. You can use the chown command to do this:
  1. sudo chown nginx:nginx /home/urban/run/daphne.sock

Make sure you replace nginx:nginx with the actual user and group running Daphne and NGINX.

  1. Nginx Configuration: In your Nginx configuration, you have defined the location for websocket connections as /ws/, which means only requests with a path that starts with /ws/ will be handled by Daphne. However, you might want to check the path you are using in your Django Channels routing. If your Django Channels routing uses a different path, you will need to update the location in the Nginx configuration to match.

  2. Django Channels Routing: Make sure the notification_urlpatterns in your routing.py match the path you are trying to access through the websockets. For example, if you have a route defined as url(r'^/ws/notifications/$', ...) in notification_urlpatterns, it should match the /ws/ path defined in the Nginx configuration.

  3. Check Logs: If you are still encountering issues, check the log files for any error messages. The NGINX logs are located at /home/urban/logs/, as defined in your NGINX configuration. The Daphne and worker logs would be wherever you configured them to be in your supervisor configuration. These logs should provide more information about what’s going wrong.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.