1

I am running Django + Channels server using Daphne. Daphne server is behind Nginx. My Nginx config looks like as given at end.

When I try to connect to ws://example.com/ws/endpoint I am getting NOT FOUNT /ws/endpoint error.

For me, it looks like Daphne is using protocol to route to either Django views or Channels app. If it sees http it routes to Django view and when it sees ws it routes to Channels app.

With following Nginx proxy pass configuration the URL always has http protocol prefix. So I am getting 404 or NOT FOUND in logs. If I change proxy_pass prefix to ws Nginx config fails.

What is the ideal way to setup Channels in the this scenario?

server {
    listen 443 ssl;
    server_name example.com

    location / {

        # prevents 502 bad gateway error
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;

        # redirect all HTTP traffic to localhost:8088;
        proxy_pass http://0.0.0.0:8000/;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header X-NginX-Proxy true;

        # enables WS support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_read_timeout 999999999;

    }
}
Pavan Kumar
  • 1,715
  • 1
  • 24
  • 48

1 Answers1

0

Yes, as in the question Channels detects the route based on the protocol header ws or http/https

Using ws prefix in proxy_pass http://0.0.0.0:8000/; is not possible. To forward the protocol information following config should be included.

proxy_set_header X-Forwarded-Proto $scheme;

This will forward the schema/protocol(ws) information to Channels app. And channels routes according to this information.

Pavan Kumar
  • 1,715
  • 1
  • 24
  • 48