让 OpenClaw 在局域网内通过 HTTPS 正常访问的完整配置

  1. Gateway 绑定gateway.bind 改为 "lan"controlUi.allowedOrigins 添加 IP 域名
  2. SSL 证书:生成自签名证书(CN=IP,有效期 365 天)
  3. Nginx 配置 :HTTPS 反向代理 + proxy_set_header Host localhost 绕过配对验证
  4. 防火墙:UFW 放行 80/443 及局域网 18789(192.168.0.0/16)
  5. 设备配对 :首次访问运行 openclaw devices approve <id>

📋 OpenClaw 局域网 HTTPS 访问配置总结

一、修改 Gateway 绑定地址

bash 复制代码
# 编辑配置文件
nano ~/.openclaw/openclaw.json

# 找到 gateway.bind,从 "loopback" 改为 "lan"
"gateway": {
  "bind": "lan",  # ← 允许局域网访问
  "mode": "local",
  "port": 18789
}

二、修改 Control UI 允许的域名

在同一个文件中,添加所有可能的访问域名:

json 复制代码
"gateway": {
  ...
  "controlUi": {
    "allowedOrigins": [
      "http://localhost:18789",
      "http://127.0.0.1:18789",
      "https://localhost:18789",
      "https://127.0.0.1:18789",
      "http://YOUR_IP:18789",
      "https://YOUR_IP:18789",
      "http://YOUR_IP",
      "https://YOUR_IP"
    ]
  }
}

💡 将 YOUR_IP 替换为实际 IP,如 192.168.123.123

三、生成 SSL 证书(自签名)

bash 复制代码
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /etc/ssl/private/openclaw.key \
  -out /etc/ssl/certs/openclaw.crt \
  -subj "/CN=YOUR_IP" \
  -addext "subjectAltName=DNS:localhost,DNS:YOUR_IP,IP:YOUR_IP"

四、配置 Nginx HTTPS 反向代理

bash 复制代码
nano /etc/nginx/sites-available/openclaw-https

粘贴以下配置(将 YOUR_IP 替换为实际 IP):

nginx 复制代码
server {
    listen 443 ssl;
    server_name YOUR_IP localhost;
    
    # SSL 证书配置
    ssl_certificate /etc/ssl/certs/openclaw.crt;
    ssl_certificate_key /etc/ssl/private/openclaw.key;
    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    
    location / {
        proxy_pass http://YOUR_IP:18789/;
        proxy_http_version 1.1;
        
        # 🔑 关键:强制本地化,避免配对要求
        proxy_set_header Host localhost;
        proxy_set_header Origin https://localhost:18789;
        
        # WebSocket 支持
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        
        proxy_set_header X-Forwarded-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 https;
        
        proxy_read_timeout 86400;
        proxy_send_timeout 86400;
        proxy_buffering off;
    }
    
    location /app/ {
        try_files $uri $uri/ /index.html;
        add_header Cache-Control "public, max-age=31536000";
    }
}

# HTTP 自动跳转 HTTPS
server {
    listen 80;
    server_name YOUR_IP localhost;
    return 301 https://$host$request_uri;
}

启用配置:

bash 复制代码
ln -sf /etc/nginx/sites-available/openclaw-https /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx

五、配置 UFW 防火墙

bash 复制代码
# 重置并启用防火墙
ufw --force reset

# 允许必要端口
ufw allow 22/tcp          # SSH(所有来源)
ufw allow 80/tcp          # HTTP 重定向
ufw allow 443/tcp         # HTTPS(所有来源)
ufw allow from 192.168.0.0/16 to any port 18789  # Gateway(仅局域网)

# 启用防火墙
ufw --force enable

六、重启 OpenClaw Gateway

bash 复制代码
pkill -f openclaw-gateway
sleep 2
nohup /root/.nvm/versions/node/v25.8.1/bin/node \
  /root/.nvm/versions/node/v25.8.1/lib/node_modules/openclaw/bin/gateway.js > /tmp/openclaw-gw.log 2>&1 &

七、首次访问配对(如需要)

浏览器访问 https://YOUR_IP/chat?session=main,如果遇到 "pairing required":

bash 复制代码
# 查看配对请求列表
openclaw devices list

# 批准设备(替换为实际设备 ID)
openclaw devices approve <Request ID>

✅ 验证清单

检查项 命令 预期结果
Gateway 运行 `ps aux grep openclaw-gateway`
Gateway 端口 `ss -ltnp grep 18789`
Nginx 运行 systemctl status nginx active (running)
防火墙状态 ufw status 包含 22/80/443/18789
HTTPS 访问 curl -k https://localhost/ 返回 HTML

📞 访问方式

局域网内任意设备:

ini 复制代码
https://YOUR_IP/chat?session=main

首次浏览器访问会提示自签名证书警告,选择"继续访问"即可。


🔒 安全建议

  1. Token 认证 : Gateway 已有 token 保护(查看 openclaw.json
  2. 限制 QQBot 访问 : 将 "allowFrom": ["*"] 改为具体账号
  3. SSH 加固: 考虑限制 SSH 为特定 IP 或禁用密码登录
  4. 定期更新 SSL 证书: 自签名证书一年后需重新生成

这样配置后,局域网内的任何设备都可以直接通过 HTTPS 访问 Control UI 了!

相关推荐
程序员鱼皮20 小时前
又一个新项目开源,让 AI 帮你盯全网热点!
javascript·ai·程序员·编程·ai编程
loonggg1 天前
一个被99%程序员忽略的效率杀手:你每天盯着看的那块屏幕
程序员
程序员cxuan1 天前
为什么 Claude 要求实名认证?
人工智能·后端·程序员
得物技术1 天前
生成式召回在得物的落地技术分享与思考
算法·性能优化·程序员
JarvanMo1 天前
别拦我!我要在手机上继续写代码
程序员
SimonKing1 天前
AI大模型中转平台,无需科学上网就可以使用国外模型
java·后端·程序员
程序员cxuan1 天前
10 个贼爽的 workflow 工作流
后端·程序员·代码规范
舒一笑2 天前
一文讲透 Temporal:为什么大厂都在用它做 AI 与分布式系统的“流程大脑”?
后端·程序员·llm
程序员鱼皮2 天前
别再说 AI 编程就是 Vibe Coding 了!6 种主流模式一次讲清
ai·程序员·编程·ai编程·vibe coding
SimonKing2 天前
OpenCode 20 个斜杠命令,90% 的人只用过 3 个
java·后端·程序员