跳转至

2026-03-06 - OpenClaw WebSocket 1006 错误排查记录

问题描述

用户报告网页端 https://openclaw.ccchao.top/ 显示 disconnected (1006): no reason,WebSocket 连接失败。

排查过程

1. 初步诊断

  • Gateway 状态:正常运行,但绑定在 127.0.0.1:18789(仅本地)
  • Nginx Proxy Manager (NPM) 配置:转发地址为 127.0.0.1:18789
  • 问题 1: NPM 在 Docker 容器内,无法通过 127.0.0.1 访问宿主机 Gateway

2. Gateway 配置修复

// ~/.openclaw/openclaw.json
"gateway": {
  "port": 18789,
  "mode": "public",
  "bind": "0.0.0.0"  // 或 "lan"
}
重启 Gateway:pkill -f openclaw-gateway && openclaw gateway start

3. NPM 配置修复

修改 /opt/npm/data/nginx/proxy_host/3.conf: - 转发地址从 127.0.0.1 改为 172.93.221.71(宿主机 IP) - WebSocket 配置:

location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";  // 关键!不是 $http_connection
    proxy_http_version 1.1;
    include conf.d/include/proxy.conf;
}
- 删除 server 块级别的重复 proxy_set_header 配置 - 重启 NPM 容器:docker restart $(docker ps | grep npm | awk '{print $1}')

4. 根本原因发现

查看 Gateway 日志 (/tmp/openclaw/openclaw-2026-03-06.log):

"subsystem":"gateway/ws"
"reason":"not-paired"
"code":1008 reason=pairing required
真正原因:设备未配对,不是 Nginx 配置问题!

5. 关键发现

  • 手机可以正常访问
  • 电脑浏览器无法访问
  • 说明配置已正确,问题在**特定浏览器的设备配对状态**

解决方案

  1. 使用令牌化 URL(推荐):

    https://openclaw.ccchao.top/?token=bed416e8f783848d4ad4de7b830b2636255facbaf9f03900b4ba88607f420214
    

  2. 清除浏览器缓存 + Cookie

  3. Ctrl+Shift+Delete → 清除全部时间数据
  4. 完全关闭浏览器重开

  5. 使用无痕模式Ctrl+Shift+N

经验教训

  1. WebSocket 1006/1008 错误:先查 Gateway 日志,不要只盯着 Nginx 配置
  2. Docker 网络:容器内 127.0.0.1 是容器自己的 localhost,不是宿主机
  3. 设备配对:OpenClaw Gateway 默认要求设备配对,跨设备访问需要 token 或配对
  4. 配置验证:修改后一定要 nginx -t 测试并重载/重启容器