[Django]-Deploying Django Channels with nginx

3👍

You will need to download Daphne.

Daphne is a high-performance websocket server for Django channels.

https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/daphne/

https://github.com/django/daphne

https://channels.readthedocs.io/en/stable/deploying.html

How you can run daphne:
daphne -b 0.0.0.0 -p 8070 django_project.asgi:application

Here is my Nginx conf for channels:

upstream django {
    server 127.0.0.1:8080;
}
upstream websockets{
    server 127.0.0.1:8070;
}

server {
    ...
    ...
   

    location /ws {
        proxy_pass http://websockets;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        ...
    }

    location / {
        proxy_pass http://django; 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        ...
    }

}

Leave a comment