让 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 了!

相关推荐
爱勇宝15 小时前
深扒 Anthropic 1680 位工程师简历:应届生几乎没机会,AI 公司最缺的不是博士
前端·后端·程序员
AskHarries15 小时前
工具失败时怎么办:重试、回滚、人工确认和风险提示
后端·程序员
陈随易1 天前
VSCode的Copilot扩展支持接入DeepSeek,Kimi了!
前端·后端·程序员
七十二時4462 天前
什么是VibeCoding
程序员
AskHarries2 天前
Canvas / Artifact:把结果变成可查看、可交互的产物
程序员
爱勇宝2 天前
鸿蒙生态的下半场:开发者不只要能开发,还要能赚钱
android·前端·程序员
程序员cxuan2 天前
DeepSeek 杀入多模态,识图功能正式上线!
人工智能·后端·程序员
文心快码BaiduComate3 天前
Comate 搭载GLM-5.2:百万上下文,稳定支撑长程任务
前端·程序员·开源
程序员cxuan3 天前
分享一下我最近常用的 10 个 Codex 小技巧。
人工智能·后端·程序员
Moonbit3 天前
MoonBit ×CCF开源创新大赛 倒计时24天!快来提交你的作品
程序员·编程语言