跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客GitHub 精选镜像工具UI配色美学隐私政策关于联系
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
Shell / BashSaaSAI

OpenClaw 配置 Nginx 反向代理及 HTTPS 安全接入指南

OpenClaw Gateway 默认监听本地回环地址,通过配置 Nginx 反向代理可将其安全暴露至公网。方案涵盖安装 Nginx 与 Certbot、设置信任代理模式、申请 Let's Encrypt SSL 证书、启用 Basic Auth 登录保护以及 WebSocket 支持。最终实现通过 HTTPS 加密通道安全访问 Control UI,并包含防火墙设置与日志监控建议。

女王发布于 2026/3/15更新于 2026/6/1419 浏览
OpenClaw 配置 Nginx 反向代理及 HTTPS 安全接入指南

OpenClaw 配置 Nginx 反向代理及 HTTPS 安全接入指南

将 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(本文以 your-domain.com 为例)
  • OpenClaw: 已安装并运行在本地模式
  • 端口: 80/443 未被占用

步骤一:安装 Nginx 和 Certbot

# CentOS/RHEL
sudo dnf install -y nginx certbot python3-certbot-nginx
# Ubuntu/Debian
sudo apt update && sudo apt install -y nginx certbot python3-certbot-nginx
# 启动 Nginx
sudo systemctl start nginx
sudo systemctl enable nginx

步骤二:配置 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://your-domain.com"]}}}

关键参数说明:

参数说明
bind: "loopback"只监听本地地址,禁止外部直接访问
trustedProxies信任的代理服务器 IP,此处为 Nginx
mode: "trusted-proxy"启用信任代理认证模式
userHeaderNginx 传递用户身份的 Header
allowedOrigins允许的 Control UI 来源域名

重启 OpenClaw Gateway:

openclaw gateway restart

步骤三:配置 Nginx 反向代理

创建 /etc/nginx/conf.d/openclaw.conf:

# OpenClaw Nginx 配置
# HTTP 重定向到 HTTPS
server {
    listen 80;
    server_name your-domain.com;
    return 301 https://$host$request_uri;
}

# HTTPS 服务
server {
    listen 443 ssl http2;
    server_name your-domain.com;
    # SSL 证书路径(步骤四会自动配置)
    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
    # 安全响应头
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" 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 -t
nginx -s reload

步骤四:申请 SSL 证书

使用 Let's Encrypt 免费证书:

# 申请证书
sudo certbot --nginx -d your-domain.com
# 测试自动续期
sudo certbot renew --dry-run

Certbot 会自动:

  • 生成 SSL 证书
  • 修改 Nginx 配置添加证书路径
  • 设置自动续期任务

步骤五:添加登录保护(可选但强烈建议)

为了防止未授权访问,建议添加 Basic Auth 登录。

1. 创建密码文件
# 创建账号密码(用户名:admin)
sudo sh -c 'echo "admin:$(openssl passwd -apr1 你的密码)" > /etc/nginx/.htpasswd'
# 添加更多用户
sudo sh -c 'echo "用户名:$(openssl passwd -apr1 密码)" >> /etc/nginx/.htpasswd'
2. 启用 Basic Auth

在 Nginx 配置中添加两行:

server {
    listen 443 ssl http2;
    server_name your-domain.com;
    # 添加登录保护
    auth_basic "OpenClaw Login";
    auth_basic_user_file /etc/nginx/.htpasswd;
    location / {
        # ... 原有配置
    }
}

重载 Nginx:

sudo nginx -t && sudo systemctl reload nginx

验证部署

访问 https://your-domain.com,你应该看到:

  1. 🔒 浏览器显示安全锁(SSL 生效)
  2. 📝 弹出登录框(Basic Auth)
  3. 🤖 登录后进入 OpenClaw Control UI

常见问题

1. "origin not allowed" 错误

原因: allowedOrigins 配置不匹配
解决: 在 openclaw.json 中添加你的访问地址:

"allowedOrigins":["https://your-domain.com"]
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. 证书续期失败

检查:

sudo certbot certificates
sudo certbot renew --dry-run

手动续期:

sudo certbot renew
sudo systemctl reload nginx

安全建议

  1. 定期更换密码: 建议每 3-6 个月更换一次 Basic Auth 密码
  2. 限制访问来源: 如需更高安全性,可在 Nginx 中添加 IP 白名单

访问日志监控: 定期检查异常访问

sudo tail -f /var/log/nginx/openclaw-access.log

防火墙设置: 只开放 80/443 端口,禁止外部访问 18789

sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

总结

通过本文的配置,你已经完成了:

  • ✅ OpenClaw 信任代理模式配置
  • ✅ Nginx 反向代理 + HTTPS
  • ✅ Let's Encrypt SSL 证书
  • ✅ Basic Auth 登录保护

现在你可以安全地从任何地方访问你的 OpenClaw Control UI 了!

目录

  1. OpenClaw 配置 Nginx 反向代理及 HTTPS 安全接入指南
  2. 前言
  3. 环境准备
  4. 步骤一:安装 Nginx 和 Certbot
  5. CentOS/RHEL
  6. Ubuntu/Debian
  7. 启动 Nginx
  8. 步骤二:配置 OpenClaw 信任代理模式
  9. 步骤三:配置 Nginx 反向代理
  10. OpenClaw Nginx 配置
  11. HTTP 重定向到 HTTPS
  12. HTTPS 服务
  13. 步骤四:申请 SSL 证书
  14. 申请证书
  15. 测试自动续期
  16. 步骤五:添加登录保护(可选但强烈建议)
  17. 1. 创建密码文件
  18. 创建账号密码(用户名:admin)
  19. 添加更多用户
  20. 2. 启用 Basic Auth
  21. 验证部署
  22. 常见问题
  23. 1. "origin not allowed" 错误
  24. 2. WebSocket 连接失败 (1008 unauthorized)
  25. 3. 证书续期失败
  26. 安全建议
  27. 总结
  • 免费图片AI生成工具免费生成了解详情
  • Magick API 一键接入全球大模型注册送1000万token查看
  • 免费图片视频在线生成30秒,将你的创意变成现实开始设计
  • X/Twitter免费视频下载器免登陆无限额度免费视频解析下载了解详情
  • 100+免费在线小游戏爽一把
极客日志微信公众号二维码

微信扫一扫,关注极客日志

微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

更多推荐文章

查看全部
  • TD3 算法详解:双延迟深度确定性策略梯度
  • Dify 平台的 Webhook 机制配置与使用场景
  • 可解释性:走向透明与可信的人工智能
  • 基于 SpringBoot+Vue 的动漫视频分享与交流平台设计与实现
  • OpenClaw 配置本地 llama.cpp 后端指南
  • ToDesk 内置 ToClaw AI:零代码实现科技新闻日报自动化实战
  • Python 循环结构详解:for-in 与 while 用法
  • Java 音视频场景面试实战:内存模型、Spring Boot 与 Kafka
  • 机器人通讯架构选型:CAN/FD、RS485、EtherCAT 对比分析
  • OpenCore 安装指南:在 PC 上运行 macOS 的完整教程
  • RTK 免像控验证:无人机摄影测量精度对比与代码实现
  • 融合语言模型的多模态触觉传感器 SuperTac 助力机器人触觉感知
  • 强化学习基础与智能决策系统开发
  • 网络安全技术入门:渗透测试、二进制逆向与安全研发
  • 基于 AnythingtoRealCharacters2511 的 ACG 真人化素材自动化方案
  • 利用 OpenVINO 部署 Qwen2.5 模型实战指南
  • 家庭 AI 助手实战:QQ 机器人接入 OpenClaw
  • 算法实战:位运算解两数之和、唯一数字及缺失数字
  • VS Code Remote WSL 中 Copilot 代理设置与网络问题排查
  • Git 分支管理基础与策略

相关免费在线工具

  • RSA密钥对生成器

    生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online

  • Mermaid 预览与可视化编辑

    基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online

  • 随机西班牙地址生成器

    随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online

  • Markdown转HTML

    将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online