You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Centrifugo与Django集成报错:执行connect后因bad request断开

问题:Centrifugo与Django集成后无法接收消息,触发"bad request"断开连接

完成Centrifugo与Django的聊天应用集成后,无法看到消息,Centrifugo日志显示连接因"bad request"断开:

2024-06-12 20:42:18 [INF] disconnect after handling command client=5bf22df5-8051-463b-9c41-4a6e9d92865a command=id:6 reason="bad request" user=

该错误触发自chat.templates.chat.rooms.html中的centrifuge.connect()调用。

NGINX容器日志

192.168.65.1 - - [12/Jun/2024:17:44:26 +0000] "GET /connection/websocket HTTP/1.1" 101 15 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
192.168.65.1 - - [12/Jun/2024:17:44:30 +0000] "GET /connection/websocket HTTP/1.1" 101 15 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
192.168.65.1 - - [12/Jun/2024:17:44:42 +0000] "GET /connection/websocket HTTP/1.1" 101 15 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
192.168.65.1 - - [12/Jun/2024:17:44:44 +0000] "GET /connection/websocket HTTP/1.1" 101 15 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
192.168.65.1 - - [12/Jun/2024:17:44:58 +0000] "GET /connection/websocket HTTP/1.1" 101 15 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
192.168.65.1 - - [12/Jun/2024:17:45:01 +0000] "GET /connection/websocket HTTP/1.1" 101 15 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"

Centrifugo config.json配置

{
    "port": 8001,
    "engine": "redis",
    "redis_address": "redis://localhost:6379",
    "allowed_origins": [
        "http://localhost:9000",
        "http://127.0.0.1:9000"
    ],
    "proxy_connect_endpoint": "http://localhost:8000/chat/centrifugo/connect/",
    "proxy_publish_endpoint": "http://localhost:8000/chat/centrifugo/publish/",
    "proxy_subscribe_endpoint": "http://localhost:8000/chat/centrifugo/subscribe/",
    "proxy_http_headers": ["Cookie"],
    "namespaces": [
        {
            "name": "rooms",
            "publish": true,
            "proxy_publish": true,
            "proxy_subscribe": true
        }
    ]
}

NGINX配置文件nginx.conf

events {
    worker_connections 1024;
}

error_log /dev/stdout info;

http {
    access_log /dev/stdout;

    server {
        listen 9000;

        server_name localhost;

        location / {
            proxy_pass http://host.docker.internal:8000;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        location /connection/websocket {
            proxy_pass http://host.docker.internal:8001;
            proxy_http_version 1.1;
            proxy_buffering off;
            keepalive_timeout 65;
            proxy_read_timeout 60s;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_cache_bypass $http_upgrade;
        }
    }
}
调试与解决步骤

1. 修正Centrifugo代理端点地址

如果Centrifugo运行在容器中,配置里的localhost指向容器自身,无法访问宿主机的Django服务。需将代理端点的localhost替换为host.docker.internal(与NGINX配置一致):

"proxy_connect_endpoint": "http://host.docker.internal:8000/chat/centrifugo/connect/",
"proxy_publish_endpoint": "http://host.docker.internal:8000/chat/centrifugo/publish/",
"proxy_subscribe_endpoint": "http://host.docker.internal:8000/chat/centrifugo/subscribe/",

修改后重启Centrifugo服务。

2. 验证Centrifugo到Django的连通性

进入Centrifugo容器,用curl测试代理端点:

docker exec -it <centrifugo-container-id> sh
curl http://host.docker.internal:8000/chat/centrifugo/connect/

确认返回200状态码,说明连通正常。

3. 检查Django代理视图与路由

  • 确认Django中已正确配置Centrifugo代理视图,路由/chat/centrifugo/connect/可正常访问。
  • 若Django启用CSRF验证,需确保Centrifugo请求携带正确CSRF Token,或在代理视图中排除CSRF校验。

4. 查看Django请求日志

检查Django日志,确认Centrifugo是否发起代理请求,以及请求是否存在404、403等错误,定位具体问题。

5. 浏览器DevTools调试

打开浏览器开发者工具Network标签,查看WebSocket帧数据,确认客户端发送的connect命令格式是否正确,是否携带必要参数(如token、用户信息)。


内容的提问来源于stack exchange,提问作者Ivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 12:38:10