Search code examples
dockernginxwsgilets-encryptasgi

How to proxy both WSGI and ASGI via Uvicorn or Daphne with nginx and SSL proxy?


I have a small project that uses Django Channels (websockets) and it works fine locally. I've added SSL to my docker-based deployment with the letsencrypt-companion-container docker image, which, exposes 443, handles all the SSL, then funnels requests/responses vi port 80 into my A/WSGI adapter (uvicorn or daphne).

When under SSL, my client code is getting this error in the JS console:

`WebSocket connection to 'wss://my_server.com/ws/echo/' failed: Error during WebSocket handshake: Unexpected response code: 404`

It isn't clear to me why. How do I make this work?

Here is my nginx.conf, if it helps:

upstream wsgi {
    server web:8000;
}

upstream asgi {
    server web:8000;
}

server {
    listen 0.0.0.0:80;

    location / {
        proxy_pass http://wsgi;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /ws {
        proxy_pass ​http://asgi;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }

    location /staticfiles/ {
        alias /home/app/web/staticfiles/;
    }
}

Solution

  • I was facing a similar issue, I got a 403 error on my django server. After checking this and setting my ALLOWED_HOSTS = "*" the server worked.

    Being honest it doesn't look like a best practice to me, but while I find something better, is a way to have the server up and running.

    Make sure also that you include this configuration on your nginx