【问题标题】:Does Django Channels uses ws:// protocol prefix to route between Django view or Channels app?Django Channels 是否使用 ws:// 协议前缀在 Django 视图或 Channels 应用程序之间进行路由?
【发布时间】:2023-03-21 03:47:01
【问题描述】:

我正在使用 Daphne 运行 Django + Channels 服务器。达芙妮服务器落后于 Nginx。我的 Nginx 配置看起来像最后给出的。

当我尝试连接到ws://example.com/ws/endpoint 时,我收到NOT FOUNT /ws/endpoint 错误。

对我来说,Daphne 似乎正在使用协议来路由到 Django 视图或 Channels 应用程序。如果它看到http 它会路由到 Django 视图,当它看到ws 它会路由到 Channels 应用程序。

使用以下 Nginx 代理传递配置,URL 始终具有 http 协议前缀。所以我在日志中得到 404 或 NOT FOUND。如果我将 proxy_pass 前缀更改为 ws Nginx 配置失败。

在这种情况下设置频道的理想方式是什么?

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;

    }
}

【问题讨论】:

    标签: django nginx django-channels daphne


    【解决方案1】:

    是的,正如问题 Channels 根据协议头 wshttp/https 检测路由

    proxy_pass http://0.0.0.0:8000/; 中使用ws 前缀是不可能的。要转发协议信息,应包含以下配置。

    proxy_set_header X-Forwarded-Proto $scheme;
    

    这会将架构/协议 (ws) 信息转发到 Channels 应用。并根据这些信息进行通道路由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 2016-09-24
      • 2023-03-06
      • 2017-09-03
      相关资源
      最近更新 更多