0

I try to get my Django project running with WebSockets; in the browser console I get the error

WebSocket connection to 'wss://www.xxx.com:8001/ws/asdf/1234/' failed:

settings.py:

CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels_redis.core.RedisChannelLayer",
            "CONFIG": {
                "hosts": [("config('REDIS_SERVER_NAME')", 6379)],
            },
            "ROUTING": "myproject.routing.channel_routing",
        },
    }

asgi.py:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
application = get_default_application()

nginx configuration:

server{
        server_name <IP-adress> <xxx.xxx>;

        location = /favicon.ico { access_log off; log_not_found off; }
        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
        }
        location /ws/ {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;
                proxy_pass http://127.0.0.1:8001/;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/xxx.xxx/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/xxx.xxx/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

daphne.service:

Unit]
Description=WebSocket Daphne Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/home/ubuntu/myproject
ExecStart=daphne -e ssl:8001:privateKey=/etc/letsencrypt/live/xxx.xxx/privkey.pem:certKey=/etc/letsencrypt/live/xxx.xxx/fullchain.pem myproject.asgi:application
Restart=on-failure

[Install]
WantedBy=multi-user.target

The site works just fine and ws:// was working when I tested it without the SSL certificates from certbot for the domains. Any help is appreciated...

zeus
  • 117
  • 7
  • I also have no idea how to debug or start to work on the problem. nginx log files and the console output are not of much help... what approach should I take? – zeus Nov 10 '21 at 13:16

1 Answers1

1

I figured it out! I changed my files as follows.

nginx configuration:

server{
        server_name mydomain.com www.mydomain.com
        location = /favicon.ico { access_log off; log_not_found off; }
        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
        }

        location /ws/ {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;
                proxy_pass http://unix:/tmp/daphne.sock;
        }
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

daphne.service:

[Unit]
Description=WebSocket Daphne Service
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/ubuntu/myproject
ExecStart=daphne -u /tmp/daphne.sock myproject.asgi:application
Restart=on-failure

[Install]
WantedBy=multi-user.target
zeus
  • 117
  • 7
  • 1
    Thanks man, you literally saved my life. Been on this for hours and everybody is recommending "daphne -e ssl" but this is the easiest way to setup daphne with wss. – Hasaan Ahmed Sep 15 '22 at 10:26