首页 教程 分类 Skills下载 关于
ZH EN JA KO
基础配置

OpenClaw远程访问与端口转发配置

· 11 分钟

前言

默认情况下,OpenClaw 仅监听 127.0.0.1,只允许本机访问。但在实际部署中,你通常需要从外部网络访问 Dashboard、接收即时通讯平台的 Webhook 回调,或让移动设备配对连接。本文将系统介绍 OpenClaw 的远程访问方案,从基础的端口配置到生产级的反向代理部署。

基础:开放网络访问

最简单的做法是将监听地址改为 0.0.0.0

{
  "gateway": {
    "host": "0.0.0.0",
    "port": 18789
  }
}

修改后重启网关:

openclaw gateway --port 18789

此时局域网内的其他设备已经可以通过 http://<服务器IP>:18789 访问 OpenClaw。但要接受来自公网的连接,还需要额外的配置。

方案一:路由器端口转发

如果你的 OpenClaw 运行在家庭网络中,需要在路由器上设置端口转发(Port Forwarding):

  1. 登录路由器管理页面
  2. 找到"端口转发"或"虚拟服务器"设置
  3. 添加规则:外部端口 18789 → 内部 IP 192.168.x.x 端口 18789
  4. 保存并应用

动态 DNS

家庭宽带的公网 IP 通常会变化,建议配合 DDNS 服务使用:

# 常见的 DDNS 服务
# Cloudflare DDNS
# DuckDNS
# No-IP

配置 DDNS 后,你可以使用固定域名(如 myserver.duckdns.org)访问 OpenClaw,无需关心 IP 变化。

方案二:Nginx 反向代理

在生产环境中,推荐使用 Nginx 作为反向代理,提供 TLS 终止、负载均衡和额外的安全层。

安装 Nginx

# Ubuntu / Debian
sudo apt install nginx

# CentOS / RHEL
sudo yum install nginx

配置文件

创建 Nginx 配置文件 /etc/nginx/sites-available/openclaw

server {
    listen 80;
    server_name openclaw.yourdomain.com;

    # HTTP 重定向到 HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    server_name openclaw.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/openclaw.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/openclaw.yourdomain.com/privkey.pem;

    # 推荐的 SSL 配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://127.0.0.1:18789;
        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;

        # WebSocket 支持(Dashboard 实时更新和移动端连接需要)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # 超时设置(AI 响应可能较慢)
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
    }

    # 限制请求体大小(匹配 OpenClaw 的 maxBodySize)
    client_max_body_size 20m;
}

启用配置

sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

方案三:Caddy 反向代理

如果你更喜欢简洁的配置,Caddy 是一个优秀的替代方案。它会自动申请和续期 Let's Encrypt 证书。

Caddyfile 配置

openclaw.yourdomain.com {
    reverse_proxy localhost:18789
}

是的,就这么简单。Caddy 会自动处理 HTTPS 证书、HTTP 到 HTTPS 重定向、WebSocket 代理等。

# 启动 Caddy
caddy start

方案四:内网穿透

如果你的网络环境无法进行端口转发(如公司网络、校园网),可以使用内网穿透工具。

使用 Cloudflare Tunnel

# 安装 cloudflared
# 登录 Cloudflare
cloudflared tunnel login

# 创建隧道
cloudflared tunnel create openclaw

# 配置隧道
# ~/.cloudflared/config.yml
tunnel: your-tunnel-id
credentials-file: ~/.cloudflared/your-tunnel-id.json

ingress:
  - hostname: openclaw.yourdomain.com
    service: http://localhost:18789
  - service: http_status:404

# 启动隧道
cloudflared tunnel run openclaw

使用 frp

# frpc.ini (客户端配置)
[common]
server_addr = your-frp-server.com
server_port = 7000
token = your-auth-token

[openclaw]
type = tcp
local_ip = 127.0.0.1
local_port = 18789
remote_port = 18789

HTTPS 证书配置

Let's Encrypt(推荐)

使用 certbot 免费获取 SSL 证书:

# 安装 certbot
sudo apt install certbot python3-certbot-nginx

# 自动配置 Nginx + 证书
sudo certbot --nginx -d openclaw.yourdomain.com

# 证书自动续期
sudo certbot renew --dry-run

OpenClaw 内置 TLS

如果不使用反向代理,OpenClaw 也支持直接配置 TLS:

{
  "security": {
    "tls": {
      "enabled": true,
      "cert": "/etc/letsencrypt/live/yourdomain.com/fullchain.pem",
      "key": "/etc/letsencrypt/live/yourdomain.com/privkey.pem"
    }
  }
}

Webhook 回调地址配置

许多即时通讯平台(如 Telegram、WhatsApp)需要通过 Webhook 向你的服务器推送消息。配置好远程访问后,需要更新 Webhook 基础地址:

{
  "gateway": {
    "webhookBase": "https://openclaw.yourdomain.com"
  }
}

OpenClaw 会自动将 webhookBase 与各频道的 webhookPath 拼接,生成完整的回调 URL 并注册到对应平台。例如 Telegram 的 Webhook 会被设置为 https://openclaw.yourdomain.com/webhook/telegram

防火墙配置

确保防火墙放行相关端口:

# UFW (Ubuntu)
sudo ufw allow 18789/tcp
sudo ufw allow 443/tcp
sudo ufw allow 80/tcp

# firewalld (CentOS)
sudo firewall-cmd --permanent --add-port=18789/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload

# iptables
sudo iptables -A INPUT -p tcp --dport 18789 -j ACCEPT

安全最佳实践

远程访问意味着暴露在公网,务必采取安全措施:

  1. 始终使用 HTTPS:保护传输中的数据,尤其是 API 密钥和对话内容
  2. 设置 Dashboard 密码:防止未授权的管理操作
  3. 配置速率限制:防止滥用和 DDoS 攻击
  4. 使用 IP 白名单:如果访问来源固定,限制允许的 IP 范围
  5. 定期更新:保持 OpenClaw 和反向代理软件的最新版本
  6. 监控日志:定期检查访问日志中的异常请求
{
  "security": {
    "rateLimit": {
      "enabled": true,
      "maxRequests": 60,
      "windowMs": 60000
    },
    "ipWhitelist": ["203.0.113.0/24"]
  }
}

连接验证

配置完成后,可以使用以下方法验证远程访问是否正常:

# 从外部测试 HTTPS 连接
curl -I https://openclaw.yourdomain.com/dashboard

# 测试 WebSocket 连接
wscat -c wss://openclaw.yourdomain.com/ws

# OpenClaw 内置的连接测试
openclaw doctor --remote

总结

远程访问配置是 OpenClaw 投入实际使用的关键一步。对于个人使用,路由器端口转发加 DDNS 即可满足需求;对于生产部署,推荐使用 Nginx 或 Caddy 作为反向代理,配合 Let's Encrypt 证书实现安全的 HTTPS 访问。无论选择哪种方案,都请务必遵循安全最佳实践,保护你的 AI 网关不被滥用。

OpenClaw 是开源免费的个人AI助手,支持 WhatsApp、Telegram、Discord 等多平台接入