前言
默认情况下,OpenClaw 网关运行在 HTTP 协议上。如果你需要通过公网访问 Dashboard,或者对接某些要求 HTTPS 的聊天平台(如 Telegram Webhook),就必须配置 HTTPS。本教程覆盖三种主流方案:Let's Encrypt 免费证书、Cloudflare SSL 以及 Nginx 反向代理。
方案对比
| 方案 | 难度 | 费用 | 适合场景 |
|---|---|---|---|
| Let's Encrypt + Certbot | 中等 | 免费 | 自有服务器、自定义域名 |
| Cloudflare SSL | 简单 | 免费 | 域名托管在 Cloudflare |
| Nginx 反向代理 + SSL | 中等 | 免费 | 需要高级代理功能 |
前提条件
- 一台有公网 IP 的服务器
- 一个已注册的域名(如
ai.example.com) - 域名 DNS 已指向服务器 IP
- OpenClaw 已安装并正常运行在 18789 端口
确认 OpenClaw 正在运行:
openclaw doctor
curl http://localhost:18789/health
方案一:Let's Encrypt + Certbot
1.1 安装 Certbot
# Ubuntu/Debian
sudo apt update
sudo apt install certbot
# CentOS/RHEL
sudo dnf install certbot
# macOS
brew install certbot
1.2 获取证书
确保 80 端口未被占用(Certbot 需要临时使用):
# 检查 80 端口
sudo lsof -i :80
# 如果被占用,临时停止(如 nginx)
sudo systemctl stop nginx
申请证书:
sudo certbot certonly --standalone \
-d ai.example.com \
--email [email protected] \
--agree-tos \
--no-eff-email
成功后,证书文件位于:
/etc/letsencrypt/live/ai.example.com/fullchain.pem # 证书链
/etc/letsencrypt/live/ai.example.com/privkey.pem # 私钥
1.3 配置 OpenClaw 使用证书
编辑 OpenClaw 配置文件:
{
server: {
port: 18789,
host: "0.0.0.0",
ssl: {
enabled: true,
cert: "/etc/letsencrypt/live/ai.example.com/fullchain.pem",
key: "/etc/letsencrypt/live/ai.example.com/privkey.pem",
}
}
}
重启 OpenClaw:
openclaw restart
验证 HTTPS 是否生效:
curl https://ai.example.com:18789/health
1.4 设置自动续期
Let's Encrypt 证书有效期 90 天,需要自动续期:
# 测试续期是否正常
sudo certbot renew --dry-run
# 添加 cron 任务自动续期
sudo crontab -e
添加以下内容:
# 每天凌晨 3 点检查并续期证书
0 3 * * * certbot renew --quiet --post-hook "openclaw restart"
--post-hook 确保续期后自动重启 OpenClaw 加载新证书。
方案二:Cloudflare SSL
如果你的域名托管在 Cloudflare,这是最简单的方案。
2.1 配置 DNS
在 Cloudflare Dashboard 中添加 DNS 记录:
类型:A
名称:ai(对应 ai.example.com)
内容:你的服务器 IP
代理状态:已代理(橙色云朵)
2.2 设置 SSL 模式
在 Cloudflare Dashboard → SSL/TLS 中:
SSL/TLS 加密模式:Full(严格)
各模式说明:
| 模式 | 说明 | 推荐 |
|---|---|---|
| Off | 不加密 | 不推荐 |
| Flexible | 浏览器→Cloudflare 加密,Cloudflare→服务器不加密 | 不推荐 |
| Full | 全程加密,但不验证服务器证书 | 一般 |
| Full (Strict) | 全程加密,验证服务器证书 | 推荐 |
2.3 获取 Cloudflare Origin 证书
对于 Full (Strict) 模式,需要在服务器上安装 Origin 证书:
- 进入 Cloudflare Dashboard → SSL/TLS → Origin Server
- 点击 Create Certificate
- 保持默认设置,点击 Create
- 复制证书和私钥
保存到服务器:
# 保存证书
sudo mkdir -p /etc/cloudflare
sudo nano /etc/cloudflare/origin-cert.pem
# 粘贴证书内容
sudo nano /etc/cloudflare/origin-key.pem
# 粘贴私钥内容
# 设置权限
sudo chmod 600 /etc/cloudflare/origin-key.pem
2.4 配置 OpenClaw
{
server: {
port: 18789,
host: "0.0.0.0",
ssl: {
enabled: true,
cert: "/etc/cloudflare/origin-cert.pem",
key: "/etc/cloudflare/origin-key.pem",
}
}
}
openclaw restart
Cloudflare Origin 证书有效期长达 15 年,无需频繁续期。
2.5 配置 Cloudflare 安全选项
在 Cloudflare Dashboard 中建议开启:
- Always Use HTTPS:自动将 HTTP 重定向到 HTTPS
- HSTS:启用 HTTP 严格传输安全
- Minimum TLS Version:设置为 TLS 1.2
- Automatic HTTPS Rewrites:自动修复混合内容
方案三:Nginx 反向代理
这是最灵活的方案,适合需要负载均衡、缓存等高级功能的场景。
3.1 安装 Nginx
# Ubuntu/Debian
sudo apt update
sudo apt install nginx
# CentOS/RHEL
sudo dnf install nginx
3.2 获取 SSL 证书
使用 Certbot 的 Nginx 插件一键获取:
sudo apt install python3-certbot-nginx
sudo certbot --nginx -d ai.example.com
或手动获取证书(同方案一的 1.2 步骤)。
3.3 配置 Nginx
创建 Nginx 配置文件:
sudo nano /etc/nginx/sites-available/openclaw
写入以下内容:
# HTTP 重定向到 HTTPS
server {
listen 80;
server_name ai.example.com;
return 301 https://$server_name$request_uri;
}
# HTTPS 主配置
server {
listen 443 ssl http2;
server_name ai.example.com;
# SSL 证书
ssl_certificate /etc/letsencrypt/live/ai.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ai.example.com/privkey.pem;
# SSL 安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# 其他安全头
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
# 反向代理到 OpenClaw
location / {
proxy_pass http://127.0.0.1:18789;
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;
# 超时设置(AI 回复可能需要较长时间)
proxy_read_timeout 120s;
proxy_send_timeout 120s;
}
# Dashboard 静态资源缓存
location /dashboard/assets/ {
proxy_pass http://127.0.0.1:18789;
expires 7d;
add_header Cache-Control "public, immutable";
}
}
3.4 启用配置
# 创建符号链接
sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
# 测试配置语法
sudo nginx -t
# 重载 Nginx
sudo systemctl reload nginx
3.5 配置 OpenClaw 信任代理
使用 Nginx 反向代理时,OpenClaw 不需要自己处理 SSL:
{
server: {
port: 18789,
host: "127.0.0.1", // 只监听本地,由 Nginx 代理
trustProxy: true, // 信任代理传递的 X-Forwarded-For
ssl: {
enabled: false, // Nginx 处理 SSL,OpenClaw 不需要
}
}
}
3.6 自动续期(Nginx 方案)
sudo crontab -e
0 3 * * * certbot renew --quiet --post-hook "systemctl reload nginx"
自定义域名配置
DNS 配置示例
| 记录类型 | 名称 | 值 | 说明 |
|---|---|---|---|
| A | ai | 203.0.113.1 | 指向服务器 IP |
| AAAA | ai | 2001:db8::1 | IPv6 地址(可选) |
| CNAME | www.ai | ai.example.com | www 跳转(可选) |
多域名支持
如果需要多个域名指向同一个 OpenClaw 实例:
server {
listen 443 ssl http2;
server_name ai.example.com bot.example.com assistant.example.com;
# 证书需要覆盖所有域名
ssl_certificate /etc/letsencrypt/live/ai.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ai.example.com/privkey.pem;
# ... 其余配置同上
}
申请多域名证书:
sudo certbot certonly --standalone \
-d ai.example.com \
-d bot.example.com \
-d assistant.example.com
验证和测试
测试 HTTPS 连接
# 基本连接测试
curl -I https://ai.example.com
# 查看证书信息
openssl s_client -connect ai.example.com:443 -servername ai.example.com </dev/null 2>/dev/null | openssl x509 -noout -dates
# 使用 SSL Labs 在线检测(浏览器访问)
# https://www.ssllabs.com/ssltest/analyze.html?d=ai.example.com
检查 HSTS
curl -I https://ai.example.com | grep -i strict
# 应该看到: Strict-Transport-Security: max-age=31536000; includeSubDomains
常见问题
证书续期失败
# 查看 Certbot 日志
sudo cat /var/log/letsencrypt/letsencrypt.log
# 手动续期并查看详细输出
sudo certbot renew --verbose
常见原因:80 端口被占用、DNS 解析有问题、防火墙阻断。
混合内容警告
浏览器显示"部分内容不安全",通常是页面中引用了 HTTP 资源。在 Cloudflare 中启用 Automatic HTTPS Rewrites 可以自动修复。
WebSocket 连接失败
确保 Nginx 配置中包含 WebSocket 支持:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
小结
为 OpenClaw 配置 HTTPS 是生产环境中必不可少的步骤。对于大多数用户,推荐使用 Cloudflare SSL(最简单)或 Nginx + Let's Encrypt(最灵活)。无论选择哪种方案,都请确保开启 HSTS、配置 HTTP 到 HTTPS 重定向,并设置证书自动续期。安全无小事,HTTPS 是保护你和用户数据的第一道防线。