首页 资讯 下载 教程 Skills 社群

Nginx 反向代理部署 OpenClaw 教程

为什么需要反向代理

在生产环境中,OpenClaw 通常部署在内网服务器上。通过反向代理可以实现:

  • HTTPS/TLS 加密
  • 精细控制暴露的路径
  • 隐藏内部服务架构
  • 负载均衡和访问日志

信任代理配置

使用反向代理后,OpenClaw 收到的请求来源 IP 会变成代理服务器的地址。需要配置 trustedProxies 让 OpenClaw 正确识别真实客户端 IP:

{
  gateway: {
    // 信任的代理 IP 列表
    trustedProxies: [
      "127.0.0.1",
      "10.0.0.0/8",
      "172.16.0.0/12"
    ]
  }
}

安全警告:如果不配置 trustedProxies,攻击者可能通过伪造 X-Forwarded-For 头来绕过基于 IP 的认证。特别是当代理和 OpenClaw 运行在同一台机器上时,localhost 来源的请求可能被错误地授予特权访问。

Nginx 反向代理配置

基础 HTTP 代理

server {
    listen 443 ssl;
    server_name openclaw.example.com;

    ssl_certificate /etc/ssl/certs/openclaw.pem;
    ssl_certificate_key /etc/ssl/private/openclaw.key;

    # 仅暴露 Webhook 路径到公网
    location /hooks {
        proxy_pass http://127.0.0.1:8080;
        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;
    }

    # Google Chat Webhook
    location /googlechat {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # 拒绝所有其他请求
    location / {
        return 403;
    }
}

WebSocket 代理配置

OpenClaw 的 WebSocket 连接需要特殊的代理配置:

# WebSocket 代理(仅限内网访问)
location /ws {
    proxy_pass http://127.0.0.1:8080;
    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;

    # WebSocket 超时设置
    proxy_read_timeout 86400s;
    proxy_send_timeout 86400s;
}

控制面板访问限制

管理面板应仅限内网或 VPN 访问,不要暴露到公网:

# 控制面板 - 仅允许内网访问
location /dashboard {
    allow 10.0.0.0/8;
    allow 172.16.0.0/12;
    allow 192.168.0.0/16;
    deny all;

    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

其他反向代理方案

Caddy(自动 HTTPS)

openclaw.example.com {
    reverse_proxy /hooks localhost:8080
    reverse_proxy /googlechat localhost:8080

    @internal {
        remote_ip 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
    }
    reverse_proxy @internal /dashboard localhost:8080
}

Cloudflare Tunnel

无需开放公网端口,适合没有固定 IP 的环境:

# 安装 cloudflared
cloudflared tunnel create openclaw-tunnel

# 配置路由
cloudflared tunnel route dns openclaw-tunnel openclaw.example.com

# 启动隧道
cloudflared tunnel --url http://localhost:8080 run openclaw-tunnel

Tailscale Funnel

在 Tailscale 网络中快速暴露服务:

# 将 OpenClaw 网关暴露到 Tailscale 网络
tailscale funnel 8080

# 仅特定路径
tailscale funnel --set-path=/hooks 8080

安全最佳实践

1. 仅暴露必要路径(/hooks、/googlechat 等)到公网
2. 控制面板和 WebSocket 仅限内网或 VPN 访问
3. 始终配置 trustedProxies 以正确检测客户端 IP
4. 启用 HTTPS,使用有效的 TLS 证书
5. 配置合理的请求速率限制
6. 定期检查 Nginx 访问日志中的异常请求

常见问题排查

  • 502 Bad Gateway:确认 OpenClaw 网关正在运行,端口号一致
  • WebSocket 连接断开:检查 proxy_read_timeout 是否设置足够长
  • 客户端 IP 显示为 127.0.0.1:配置 trustedProxies 并确保 Nginx 传递了正确的头
  • HTTPS 证书问题:使用 Let's Encrypt 自动续期,或考虑 Cloudflare Tunnel