django / daphne

Django Channels HTTP/WebSocket server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No route found for path in production

Elendiar opened this issue · comments

Hello. Have some issue in production.
In dev i successfully connecting to ws://localhost:8000/ws/informing/1243/.
In production i cant connect to ws://localhost/project_name/ws/informing/1243/ with error ValueError: No route found for path 'project_name/ws/informing/1243/'.
I can make it work with changing regex

websocket_urlpatterns = [
    re_path(r"^ws/informing/(?P<ou>\d+)/$",  # .*
            InformingConsumer.as_asgi()),
]

to r"project_name/ws/informing/(?P<ou>\d+)/$" or r".*ws/informing/(?P<ou>\d+)/$".
But isnt there more convinient way with setting up?

Nginx:

location /project_name/ws/ {
    set $service project_name-ws;
    
    proxy_pass http://$service;

    proxy_read_timeout 60s;
    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-Host $server_name;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

Daphne exposes a --root_path option, which sets the same in the ASGI scope, which is equivalent to SCRIPT_NAME under WSGI.

https://asgi.readthedocs.io/en/latest/specs/www.html?highlight=root_path#http-connection-scope

This should be what you're looking for.