Question

Converting Nginx from standard uwsgi/django to include Django channels websockets

Posted May 19, 2020 81 views
NginxDjangoRedis

Hi, I’ve spent quite a bit of time on this problem, and would be incredibly grateful for a solution. I have redis-server running on the server at port 6379, and the Nginx config at sites-available currently looks like this:

server {
    server_name mattalexkaye.com www.mattalexkaye.com;

    location /static {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:2901/static;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/projects/kaye-tech/socket/kaye_tech.sock;
    }

    location /wss/ {
        proxy_pass http://127.0.0.1:6379;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mattalexkaye.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mattalexkaye.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


}

server {
    if ($host = www.mattalexkaye.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mattalexkaye.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name mattalexkaye.com www.mattalexkaye.com;
    listen 80;
    return 404; # managed by Certbot

}

Meanwhile, I’ve followed the tutorial on setting up a Django chat room, which works locally, but haven’t been able to get it to work for the server version. My settings.py file looks like this:

ASGI_APPLICATION = "kaye_tech.routing.application"
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('localhost', 6379)],
        },
    },
}

and in the relevant frontend page the Javascript has the following:

var chatSocket = new WebSocket(
      "wss://mattalexkaye.com" + "/ws/cards/" + roomName + "/"
    );

I’ve quite possibly made some basic mistakes (though it’s not that I haven’t been refreshing Nginx), as I’m new to the whole websocket thing, but any help would be incredible

1 comment
  • Forgot to say, currently the error message is: “WebSocket connection to ‘wss://mattalexkaye.com/ws/cards/room/’ failed: Error during WebSocket handshake: Unexpected response code: 404”

Submit an answer

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