OpenClaw 网关远程访问指南

OpenClaw 网关远程访问指南

通过云服务器 + SSH 隧道,实现校园内网 WSL2 上 OpenClaw 网关的远程访问

说明:本文中 <网关端口> 指 OpenClaw 网关在本地监听的端口,<隧道端口> 指云服务器上用于 SSH 反向隧道的端口,<服务器IP> 指你的云服务器公网 IP,请按实际环境替换。


一、整体架构

手机 / 笔记本 / 任何设备
  ↓ HTTPS (443)
https://openclaw.example.com
  ↓
云服务器 (CVM)
  ├── Nginx (443) → 反代到 127.0.0.1:<隧道端口>
  └── socat (<网关端口>) → WebSocket 直连
  ↓
SSH 反向隧道(由 WSL2 主动建立)
  ↓
WSL2 (Windows + OpenClaw Gateway, 端口 <网关端口>)

数据流

浏览器加载页面:  HTTPS → Nginx:443 → SSH Tunnel:<隧道端口> → WSL2:<网关端口>
WebSocket 连接:  WSS   → 云服务器:<网关端口> → SSH Tunnel:<网关端口> → WSL2:<网关端口>

二、前置条件

组件 说明
WSL2 Windows 11 + WSL2,运行 OpenClaw Gateway
OpenClaw 已安装并运行在本地网关端口
云服务器 有公网 IP,已备案域名
域名 一个已备案的域名(本文以 example.com 为例)

三、配置步骤

1. 本机 → 云服务器 SSH 隧道

在本机(WSL2)中建立持久化的 SSH 反向隧道:

# 配置云服务器 SSH(一次性的)
cat >> ~/.ssh/config << "EOF"

Host myserver
  HostName <服务器IP>
  User root
  Port 22
  IdentityFile ~/.ssh/<你的密钥文件>
EOF

# 启动隧道(需要保持运行)
nohup bash -c '
while true; do
  ssh -o ConnectTimeout=10 -o ServerAliveInterval=30 \
      -o ServerAliveCountMax=3 \
      -N -R <隧道端口>:localhost:<网关端口> \
      -R <网关端口>:localhost:<网关端口> \
      myserver
  sleep 3
done
' > /tmp/tunnel.log 2>&1 &

原理-R <网关端口>:localhost:<网关端口> 表示云服务器上的该端口收到的流量,通过 SSH 隧道转发到本机 WSL2 的同一端口。隧道断线后 3 秒自动重连。

2. 云服务器安全组

在云服务器控制台开放端口:

入站规则:
  TCP / <隧道端口> / 0.0.0.0/0 / 允许
  TCP / <网关端口> / 0.0.0.0/0 / 允许   (如使用 WebSocket 直连)

如果安全组配置为"全部允许",则跳过此步。

3. Nginx 配置

/etc/nginx/nginx.confhttp{} 块内添加:

# ===== openclaw 子域名 =====
server {
    listen 443 ssl;
    server_name openclaw.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://127.0.0.1:<隧道端口>/;
        proxy_http_version 1.1;
        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_read_timeout 86400;
        proxy_buffering off;
    }
}

# ===== openclaw HTTP 重定向 =====
server {
    listen 80;
    server_name openclaw.example.com;
    return 301 https://$host$request_uri;
}

WebSocket 直连端口(非必须,Nginx 反代已覆盖):

# 使用 socat 将公网 <网关端口> 转发到隧道端
yum install -y socat
nohup socat TCP-LISTEN:<网关端口>,reuseaddr,fork TCP:127.0.0.1:<网关端口> > /tmp/socat.log 2>&1 &

4. SSL 证书(子域名)

certbot --nginx --expand \
  -d example.com \
  -d www.example.com \
  -d openclaw.example.com

5. DNS 解析

在域名服务商处添加 CNAME 记录:

类型 主机记录 记录值
CNAME openclaw example.com

四、设备认证

首次从新设备访问 https://openclaw.example.com 时,OpenClaw 会要求设备认证。

管理员在本机上审批

# 查看待审批的设备
openclaw devices list

# 输出示例:
# Pending (1)
# ┌──────────────────────────────────────┬──────────────────────┐
# │ Request                              │ Status               │
# ├──────────────────────────────────────┼──────────────────────┤
# │ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx │ new pairing          │
# └──────────────────────────────────────┴──────────────────────┘

# 批准设备
openclaw devices approve <requestId>

用户侧操作

打开 https://openclaw.example.com,看到报错后将 requestId 发给管理员即可。


五、运维注意事项

事项 说明
隧道保活 本机重启后需重新启动隧道命令
配置修改 不要直接修改 OpenClaw 网关配置(容易崩)
设备重新认证 浏览器清理缓存后需重新审批
端口安全 即使端口暴露,无 token 也无法访问

六、效果展示

  • 地址: https://openclaw.example.com
  • 功能: 查看/管理所有 OpenClaw 会话、聊天、节点
  • 设备: 手机/笔记本/任何有浏览器的设备
  • 网络: 不限校园网,随时随地可访问

本文档由 AI 助手整理,基于实际部署经验。