Cloudflare Agent Tunnel
Give each OpenClaw agent a permanent, secure HTTPS URL via Cloudflare Tunnel — no SSL certs, no nginx, no open ports.
How It Works
CODEBLOCK0
- - Cloudflare handles TLS — no cert management on the server
- The local port never needs to be open to the internet
- Each agent gets its own
cloudflared process + systemd service
✅ Preferred Method — Named Tunnel (Permanent, Free Cloudflare Account)
Always use this method. Gives a permanent URL tied to your domain. Requires a free Cloudflare account — takes 2 minutes to set up.
Step 1: Install cloudflared
CODEBLOCK1
Step 2: Authenticate — give the user this URL
Run on the VPS:
CODEBLOCK2
This prints a Cloudflare auth URL. Give that URL to the user — they open it in their browser, log into their Cloudflare account, and click Authorize. This saves /root/.cloudflared/cert.pem on the VPS.
Poll for completion:
CODEBLOCK3
Step 3: Create the tunnel
CODEBLOCK4
Step 4: Write tunnel config
CODEBLOCK5
Step 5: Route DNS
CODEBLOCK6
The domain must use Cloudflare nameservers. If it doesn't yet, the user transfers DNS management to Cloudflare (free, takes ~5 min).
Step 6: Install as systemd service
CODEBLOCK7
Step 7: Update OpenClaw allowedOrigins
CODEBLOCK8
Then: INLINECODE2
Step 8: Lock down the port
Block direct public access — all traffic must go through the tunnel:
ufw deny 18789
ufw reload
Quick Tunnel (Fallback Only — Temporary)
⚠️ Use only as a temporary fallback when no domain is available. The URL is random and resets every time the service restarts. Switch to a named tunnel as soon as a domain is ready.
CODEBLOCK10
Read the assigned URL:
grep -o 'https://[a-z0-9-]*\.trycloudflare\.com' /var/log/cloudflared-openclaw.log | tail -1
Multi-Agent Setup (One VPS, Multiple Agents)
Each agent = one OpenClaw gateway port + one named tunnel + one systemd service.
CODEBLOCK12
Critical: Do NOT use cloudflared service install for multiple agents — it only supports one tunnel and overwrites the system service. Always write individual systemd service files per agent.
Custom Domains
Key facts:
- - Domain must use Cloudflare nameservers (transfer at your registrar — free)
- Cloudflare issues and auto-renews TLS certs
- CNAME records created automatically via INLINECODE4
- Free Cloudflare plan: unlimited tunnels, unlimited bandwidth
See references/custom-domains.md for a full walkthrough.
Managing Tunnels
CODEBLOCK13
Cloudflare Agent Tunnel
通过Cloudflare Tunnel为每个OpenClaw代理分配一个永久、安全的HTTPS URL — 无需SSL证书、无需nginx、无需开放端口。
工作原理
用户 → https://koda.yourdomain.com
↓ (Cloudflare边缘节点 — 在此处终止TLS)
Cloudflare Tunnel (加密)
↓
VPS上的cloudflared进程
↓
http://localhost:18789 (OpenClaw网关)
- - Cloudflare处理TLS — 服务器上无需管理证书
- 本地端口无需向互联网开放
- 每个代理拥有独立的cloudflared进程 + systemd服务
✅ 推荐方法 — 命名隧道(永久,免费Cloudflare账户)
始终使用此方法。 提供绑定到您域名的永久URL。需要免费Cloudflare账户 — 设置只需2分钟。
步骤1:安装cloudflared
bash
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main \
| tee /etc/apt/sources.list.d/cloudflared.list
apt-get update -qq && apt-get install -y cloudflared
步骤2:身份验证 — 向用户提供此URL
在VPS上运行:
bash
cloudflared tunnel login
这将打印一个Cloudflare身份验证URL。将该URL提供给用户 — 他们在浏览器中打开,登录Cloudflare账户,点击授权。这将在VPS上保存/root/.cloudflared/cert.pem。
轮询完成状态:
bash
等待cert.pem出现(用户已授权)
until [ -f /root/.cloudflared/cert.pem ]; do sleep 3; done && echo 已授权!
步骤3:创建隧道
bash
cloudflared tunnel create openclaw-koda
输出一个UUID — 记下它
TUNNEL_UUID=$(cloudflared tunnel list --output json | python3 -c \
import json,sys; t=[x for x in json.load(sys.stdin) if x[name]==openclaw-koda]; print(t[0][id]))
步骤4:编写隧道配置
bash
mkdir -p /etc/cloudflared
cat > /etc/cloudflared/openclaw-koda.yml << EOF
tunnel: ${TUNNEL_UUID}
credentials-file: /root/.cloudflared/${TUNNEL_UUID}.json
ingress:
- hostname: koda.yourdomain.com
service: http://localhost:18789
- service: http_status:404
EOF
步骤5:路由DNS
bash
cloudflared tunnel route dns openclaw-koda koda.yourdomain.com
自动创建CNAME:koda.yourdomain.com → .cfargotunnel.com
域名必须使用Cloudflare名称服务器。如果尚未使用,用户需将DNS管理转移到Cloudflare(免费,约需5分钟)。
步骤6:安装为systemd服务
bash
cat > /etc/systemd/system/cloudflared-koda.service << EOF
[Unit]
Description=Cloudflare Tunnel — openclaw-koda
After=network.target openclaw.service
[Service]
Type=simple
User=root
ExecStart=/usr/bin/cloudflared tunnel --no-autoupdate --config /etc/cloudflared/openclaw-koda.yml run
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable cloudflared-koda
systemctl start cloudflared-koda
systemctl is-active cloudflared-koda
步骤7:更新OpenClaw的allowedOrigins
json
gateway: {
controlUi: {
allowedOrigins: [
http://localhost:18789,
https://koda.yourdomain.com
]
}
}
然后:systemctl restart openclaw-koda
步骤8:锁定端口
阻止直接公共访问 — 所有流量必须通过隧道:
bash
ufw deny 18789
ufw reload
快速隧道(仅作为备用方案 — 临时使用)
⚠️ 仅在无域名可用时作为临时备用方案使用。 URL是随机的,每次服务重启都会重置。一旦域名就绪,请立即切换到命名隧道。
bash
启动快速隧道 — 打印随机https://*.trycloudflare.com URL
cloudflared tunnel --url http://localhost:18789 --no-autoupdate
或作为systemd服务(URL记录到/var/log/cloudflared-openclaw.log)
ExecStart=/usr/bin/cloudflared tunnel --no-autoupdate --url http://localhost:18789
读取分配的URL:
bash
grep -o https://[a-z0-9-]*\.trycloudflare\.com /var/log/cloudflared-openclaw.log | tail -1
多代理设置(一台VPS,多个代理)
每个代理 = 一个OpenClaw网关端口 + 一个命名隧道 + 一个systemd服务。
端口 18789 → openclaw-koda.service + cloudflared-koda.service → koda.yourdomain.com
端口 18790 → openclaw-alex.service + cloudflared-alex.service → alex.yourdomain.com
端口 18791 → openclaw-jordan.service + cloudflared-jordan.service → jordan.yourdomain.com
关键: 不要对多个代理使用cloudflared service install — 它只支持一个隧道并会覆盖系统服务。始终为每个代理编写独立的systemd服务文件。
自定义域名
关键事实:
- - 域名必须使用Cloudflare名称服务器(在您的域名注册商处转移 — 免费)
- Cloudflare颁发并自动续期TLS证书
- 通过cloudflared tunnel route dns自动创建CNAME记录
- 免费Cloudflare计划:无限隧道,无限带宽
完整指南请参见references/custom-domains.md。
管理隧道
bash
状态
systemctl list-units cloudflared-* --no-pager
日志
journalctl -u cloudflared-koda -f
列出命名隧道
cloudflared tunnel list
删除隧道
cloudflared tunnel delete openclaw-koda
systemctl disable cloudflared-koda && rm /etc/systemd/system/cloudflared-koda.service