[Fixed]-Deploying Django Channels with Daphne + NGINX using SSL

1πŸ‘

I do have a working configuration for Django+Daphne+nginx+ssl without any issues, I run daphne via supervisor with the following config file:

[program:project]

directory=<project_directory>
command=daphne -u <path_to_socket>/daphne.sock --root-path=<project_directory> <project>.asgi:channel_layer
stdout_logfile = <log_path>
stderr_logfile= <error_log_path>

[program:project_asgi_workers]

command=python <project_directory>/manage.py runworker
stdout_logfile=<log_file_path_2>
stderr_logfile=<log_error_path_2>
process_name=asgi_worker%(process_num)s
numprocs=2
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8                        ; Set UTF-8 as default encoding
autostart=true
autorestart=true
redirect_stderr=true
stopasgroup=true

To stop and start these workers I run the commands:

  • sudo supervisorctl stop all
  • sudo supervisorctl start all

Inside nginx I have the following configuration to connect to my websockets:

location /pulse_events/ {
    proxy_pass http://unix:<path_to_socket>/daphne.sock;
    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;
}

I used on this project daphne version 1.4.1, asgi-redis 1.4.3, redis 2.10.6 and channels 1.1.8.

If you still have issues, maybe it’s also a good idea to check your routing and consumers for django channels.

0πŸ‘

your nginx expects a wss request not a ws request.

πŸ‘€doubleo46

0πŸ‘

I use Django + Daphne + Nginx + SSL and here is my nginx mysite.conf. Your conf file is missed for handling /ws request. And ws and wss will be handle with this parameters. Please be sure you have a backend socket server like Daphne(i see you have), and run this server in bash and accept request in 8443(this is important because most servers permit only in this socket). And enjoy it..

location /ws {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

proxy_pass http://127.0.0.1:8443;
}
πŸ‘€CumaTekin

0πŸ‘

server {
    #https
    listen 443 ssl;
    server_name my_site.com www.my_site.com my_ip;
    root /usr/share/nginx/html;

    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;
    fastcgi_read_timeout 3000;

    gzip on;
    gzip_vary on;
    gzip_min_length 10240;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
    gzip_disable "MSIE [1-6]\.";
    #your SSL configuration
    ssl on;
    ssl_certificate /home/django/ssl/my_site.com.chained.crt;
    ssl_certificate_key /home/django/ssl/my_site.key;

    client_max_body_size 4G;
    keepalive_timeout 500;

    add_header Strict-Transport-Security "max-age=31536000";

    include /etc/nginx/default.d/*.conf;


    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/static;
    }

    # Proxy the static assests for the Django Admin panel
    location / {
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $host;
       proxy_redirect off;
       proxy_buffering off;

       proxy_pass http://unix:/home/django/mysite.sock;
    }

    location /ws {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_pass http://127.0.0.1:8443;
    }


}


server {
        listen 80;
        listen [::]:80;

        server_name my_site.com www.my_site.com my_ip;
        return 301 https://$server_name$request_uri;
}
πŸ‘€CumaTekin

Leave a comment