在反向代理後面運行 OpenClaw 可以取得 HTTPS、自訂網域,以及在同一台伺服器上運行多個服務的能力。以下是如何正確設定 Caddy、Nginx 或 Traefik。
為什麼要使用反向代理?
- HTTPS 自動 TLS:透過 Let's Encrypt 取得免費 SSL 憑證
- 自訂網域:透過
openclaw.yourdomain.com存取 OpenClaw - 安全性:隱藏 gateway 連接埠,不對外暴露
- 多服務:在同一台伺服器上同時運行 OpenClaw 和其他應用程式
Trusted Proxies 設定
這是最重要的一步。如果不設定,OpenClaw 無法分辨真正的本地連線和偽造的連線。
openclaw config set gateway.trustedProxies '["127.0.0.1"]'
如果跳過這一步,你會看到:
WARN gateway.trusted_proxies_missing
Reverse proxy headers are not trusted.
而且 OpenClaw 的本地用戶端檢查將無法透過代理正常運作。
trustedProxies 的作用
當請求透過反向代理進來時,原始用戶端 IP 會遺失。代理會透過 X-Forwarded-For header 將它加回來。但 OpenClaw 只信任 trustedProxies 清單中 IP 發送的這些 header。
如果不設定:
- OpenClaw 會認為所有請求都來自
127.0.0.1(代理本身) - 它無法執行每個用戶端的存取控制
- 安全稽核會標記
trusted_proxies_missing
Caddy 設定
Caddy 是最簡單的選項——自動 HTTPS,設定最少。
Caddyfile
openclaw.yourdomain.com {
reverse_proxy localhost:3001
}
就這樣。Caddy 會自動:
- 取得 Let's Encrypt 憑證
- 處理 HTTPS 終止
- 轉發請求到 OpenClaw
OpenClaw 的 Caddy 設定
openclaw config set gateway.mode local
openclaw config set gateway.trustedProxies '["127.0.0.1"]'
openclaw restart
重新載入 Caddy
caddy reload --adapter caddyfile --config /etc/caddy/Caddyfile
重要: 重新載入時務必加上 --adapter caddyfile。沒有它,Caddy 可能會靜默失敗。
Nginx 設定
Nginx 設定檔
server {
listen 443 ssl;
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;
location / {
proxy_pass http://127.0.0.1:3001;
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_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 80;
server_name openclaw.yourdomain.com;
return 301 https://$server_name$request_uri;
}
重點:
- WebSocket 支援:
Upgrade和Connectionheader 是 OpenClaw 即時連線所必需的 - 轉發 header:
X-Real-IP和X-Forwarded-For將真實的用戶端 IP 傳遞給 OpenClaw - HTTPS 重導向:強制所有流量走 HTTPS
取得 SSL 憑證(Certbot)
sudo certbot --nginx -d openclaw.yourdomain.com
OpenClaw 的 Nginx 設定
openclaw config set gateway.mode local
openclaw config set gateway.trustedProxies '["127.0.0.1"]'
openclaw restart
Traefik 設定
docker-compose.yml
services:
traefik:
image: traefik:v3.0
command:
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.le.acme.httpchallenge.entrypoint=web"
- "--certificatesresolvers.le.acme.email=you@example.com"
- "--certificatesresolvers.le.acme.storage=/letsencrypt/acme.json"
ports:
- "80:80"
- "443:443"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "letsencrypt:/letsencrypt"
openclaw:
image: openclaw-stack
labels:
- "traefik.http.routers.openclaw.rule=Host(`openclaw.yourdomain.com`)"
- "traefik.http.routers.openclaw.tls.certresolver=le"
- "traefik.http.services.openclaw.loadbalancer.server.port=3001"
environment:
- OPENCLAW_GATEWAY_TOKEN=your-stable-token
DNS 設定
將你的網域指向伺服器 IP:
Type: A
Name: openclaw(或你選擇的子網域)
Value: YOUR_SERVER_IP
TTL: 300
Cloudflare 使用者: 如果使用 Cloudflare 免費方案搭配萬用子網域,請將 DNS 記錄設為 DNS only(灰色雲朵),而不是 Proxied(橙色雲朵)。Cloudflare 免費方案不支援代理的萬用 DNS 記錄。
安全稽核
設定好代理後,執行 OpenClaw 的安全稽核:
openclaw security audit
你應該會看到:
OK gateway.trusted_proxies — reverse proxy headers are trusted
進行更深入的分析:
openclaw security audit --deep
這會檢查:
- Trusted proxy 設定
- 攻擊面(開放的連接埠、啟用的工具)
- Webhook 安全性
- 瀏覽器控制設定
疑難排解
"trusted_proxies_missing" 警告
openclaw config set gateway.trustedProxies '["127.0.0.1"]'
openclaw restart
儀表板可載入但無法連線
WebSocket 連線需要特殊的代理設定。請確保你的代理有傳遞 Upgrade 和 Connection header。
憑證錯誤
- 在請求憑證之前,DNS 必須先指向你的伺服器
- Port 80 必須開放,用於 HTTP-01 challenge(Let's Encrypt)
- 在初次憑證簽發期間,Cloudflare proxy 必須停用
502 Bad Gateway
OpenClaw 未在運行,或不在預期的連接埠上:
openclaw status
openclaw config get gateway.port
預先設定的代理
ClawTank 自動處理反向代理、TLS 憑證和子網域路由。每個實例都有自己的 HTTPS 子網域——不需要任何代理設定。
