OpenClaw 配置 Nginx 反向代理完整指南将 OpenClaw Gateway 安全地暴露到公网并通过 HTTPS 和登录保护确保访问安全。前言OpenClaw 是一个强大的 AI 助手网关默认情况下它只监听本地回环地址 (127.0.0.1:18789)。如果你想从外部网络访问 Control UI或者为团队提供安全的访问入口配置 Nginx 反向代理是最佳实践。本文将介绍如何✅ 配置 Nginx 反向代理到 OpenClaw✅ 启用 HTTPS (Let’s Encrypt SSL 证书)✅ 添加 Basic Auth 登录保护✅ 配置 OpenClaw 信任代理模式环境准备服务器: CentOS Stream 9 / Ubuntu 20.04 / Debian 10域名: 已解析到服务器 IP本文以www.zhonghongwei.site为例OpenClaw: 已安装并运行在本地模式端口: 80/443 未被占用步骤一安装 Nginx 和 Certbot# CentOS/RHELsudodnfinstall-ynginx certbot python3-certbot-nginx# Ubuntu/Debiansudoaptupdatesudoaptinstall-ynginx certbot python3-certbot-nginx# 启动 Nginxsudosystemctl start nginxsudosystemctlenablenginx步骤二配置 OpenClaw 信任代理模式编辑~/.openclaw/openclaw.json在gateway部分添加以下配置{gateway:{port:18789,mode:local,bind:loopback,trustedProxies:[127.0.0.1],auth:{mode:trusted-proxy,trustedProxy:{userHeader:x-forwarded-user,requiredHeaders:[x-forwarded-proto,x-forwarded-host]}},controlUi:{allowedOrigins:[https://www.zhonghongwei.site]}}}关键参数说明参数说明bind: loopback只监听本地地址禁止外部直接访问trustedProxies信任的代理服务器 IP此处为 Nginxmode: trusted-proxy启用信任代理认证模式userHeaderNginx 传递用户身份的 HeaderallowedOrigins允许的 Control UI 来源域名重启 OpenClaw Gatewayopenclaw gateway restart步骤三配置 Nginx 反向代理创建/etc/nginx/conf.d/openclaw.conf# OpenClaw Nginx 配置 # HTTP 重定向到 HTTPS server { listen 80; server_name www.zhonghongwei.site; return 301 https://$host$request_uri; } # HTTPS 服务 server { listen 443 ssl http2; server_name www.zhonghongwei.site; # SSL 证书路径步骤四会自动配置 ssl_certificate /etc/letsencrypt/live/www.zhonghongwei.site/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.zhonghongwei.site/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # 安全响应头 add_header Strict-Transport-Security max-age31536000; includeSubDomains always; add_header X-Frame-Options SAMEORIGIN always; add_header X-Content-Type-Options nosniff always; add_header X-XSS-Protection 1; modeblock always; # 日志配置 access_log /var/log/nginx/openclaw-access.log; error_log /var/log/nginx/openclaw-error.log; location / { # WebSocket 支持必需 proxy_pass http://127.0.0.1:18789; # 核心这里清空认证头防止冲突 proxy_set_header Authorization ; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; # 转发用户身份信任代理模式必需 proxy_set_header X-Forwarded-User $remote_user; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # 超时设置WebSocket 长连接 proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 86400s; proxy_buffering off; } }测试并重载配置nginx-tnginx-sreload步骤四申请 SSL 证书使用 Let’s Encrypt 免费证书# 申请证书sudocertbot--nginx-dwww.zhonghongwei.site# 测试自动续期sudocertbot renew --dry-runCertbot 会自动生成 SSL 证书修改 Nginx 配置添加证书路径设置自动续期任务步骤五添加登录保护可选但强烈建议为了防止未授权访问建议添加 Basic Auth 登录。1. 创建密码文件# 创建账号密码用户名: adminsudosh-cecho admin:$(openssl passwd -apr1 你的密码) /etc/nginx/.htpasswd# 添加更多用户sudosh-cecho 用户名:$(openssl passwd -apr1 密码) /etc/nginx/.htpasswd2. 启用 Basic Auth在 Nginx 配置中添加两行server { listen 443 ssl http2; server_name www.zhonghongwei.site; # 添加登录保护 auth_basic OpenClaw Login; auth_basic_user_file /etc/nginx/.htpasswd; location / { # ... 原有配置 } }重载 Nginxsudonginx-tsudosystemctl reload nginx验证部署访问https://www.zhonghongwei.site你应该看到 浏览器显示安全锁SSL 生效 弹出登录框Basic Auth 登录后进入 OpenClaw Control UI常见问题1. “origin not allowed” 错误原因:allowedOrigins配置不匹配解决: 在openclaw.json中添加你的访问地址allowedOrigins:[https://www.zhonghongwei.site]2. WebSocket 连接失败 (1008 unauthorized)原因: 缺少必要的转发 Headers解决: 确保 Nginx 配置中包含proxy_set_header X-Forwarded-User $remote_user; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host;3. 证书续期失败检查:sudocertbot certificatessudocertbot renew --dry-run手动续期:sudocertbot renewsudosystemctl reload nginx安全建议防火墙设置: 只开放 80/443 端口禁止外部访问 18789sudofirewall-cmd--permanent--add-servicehttpssudofirewall-cmd--permanent--add-servicehttpsudofirewall-cmd--reload定期更换密码: 建议每 3-6 个月更换一次 Basic Auth 密码访问日志监控: 定期检查异常访问sudotail-f/var/log/nginx/openclaw-access.log限制访问来源: 如需更高安全性可在 Nginx 中添加 IP 白名单总结通过本文的配置你已经完成了✅ OpenClaw 信任代理模式配置✅ Nginx 反向代理 HTTPS✅ Let’s Encrypt SSL 证书✅ Basic Auth 登录保护现在你可以安全地从任何地方访问你的 OpenClaw Control UI 了相关链接:OpenClaw 官方文档Nginx 官方文档Let’s Encrypt配置文件备份:~/.openclaw/openclaw.json/etc/nginx/conf.d/openclaw.conf/etc/nginx/.htpasswd小技巧直接把这个博客内容发给openclaw让它帮你配置。