HTTPS 证书配置

Let's Encrypt 免费证书 —— 10分钟搞定 HTTPS

为什么要上 HTTPS

如果你的 OpenClaw 要对外提供访问,HTTPS 不是可选项,是必须的。原因很简单:HTTP 是明文传输,你的 API Key、对话内容在网络上裸奔,中间人随便截获。

好消息是现在搞 HTTPS 完全免费 —— Let's Encrypt 提供免费证书,Certbot 工具自动申请和续期,10 分钟就能搞定。

四步搞定 HTTPS

1

安装 Certbot

Certbot 是 Let's Encrypt 官方推荐的证书管理工具,一条命令安装。它会自动帮你申请证书、配置 Nginx、设置续期。

2

申请证书

运行 certbot 命令,选择你的域名,它会自动验证域名所有权并签发证书。整个过程不到一分钟。

3

配置 Nginx SSL

Certbot 会自动修改你的 Nginx 配置,加上 SSL 相关参数。但建议检查一下,确保配置是最优的。

4

设置自动续期

Let's Encrypt 证书有效期 90 天,但 Certbot 装好后会自动配置 cron/systemd timer,到期前自动续期。你只需要确认一下自动续期在正常工作就行。

安装 Certbot

安装 Certbot
# Ubuntu / Debian
sudo apt update
sudo apt install -y certbot python3-certbot-nginx

# 确认安装成功
certbot --version

申请证书

确保你的域名已经解析到服务器 IP,Nginx 也在正常运行,然后一条命令搞定:

申请 Let's Encrypt 证书
# 自动申请并配置 Nginx
sudo certbot --nginx -d openclaw.example.com

# 如果有多个域名
sudo certbot --nginx -d openclaw.example.com -d www.openclaw.example.com

# 按提示输入邮箱(用于证书到期提醒)
# 同意服务条款
# 选择是否将 HTTP 重定向到 HTTPS(建议选是)

Nginx SSL 配置

Certbot 会自动改你的 Nginx 配置,但下面是一份优化过的完整版,你可以对照检查:

nginx SSL 完整配置
server {
    listen 80;
    server_name openclaw.example.com;
    # HTTP 自动跳转 HTTPS
    return 301 https://$host$request_uri;
}

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

    # SSL 证书路径(Certbot 自动生成)
    ssl_certificate /etc/letsencrypt/live/openclaw.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/openclaw.example.com/privkey.pem;

    # SSL 安全参数
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # HSTS(强制浏览器用 HTTPS)
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    # 反向代理到 OpenClaw
    location / {
        proxy_pass http://127.0.0.1:3000;
        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;

        # WebSocket
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 300s;
    }
}

自动续期

Certbot 装好后一般会自动配置定时任务,但还是确认一下比较稳:

确认自动续期
# 测试续期流程(不会真的续期,只是模拟)
sudo certbot renew --dry-run

# 看到 "Congratulations, all simulated renewals succeeded" 就说明没问题

# 查看 Certbot 的定时任务
sudo systemctl list-timers | grep certbot

# 或者查看 crontab
cat /etc/cron.d/certbot

# 如果没有自动任务,手动加一个
# sudo crontab -e
# 0 3 * * * certbot renew --quiet --post-hook "nginx -s reload"
⚠️ 证书 90 天过期,自动续期一定要配好。很多人装完 HTTPS 以为万事大吉,结果三个月后证书过期,网站直接打不开。跑一遍 certbot renew --dry-run 确认自动续期能正常工作,别等用户投诉了才发现。

验证 HTTPS

配好后检查一下证书是否正常:

验证证书
# 用 curl 测试
curl -vI https://openclaw.example.com 2>&1 | grep "SSL certificate"

# 查看证书详情
echo | openssl s_client -connect openclaw.example.com:443 2>/dev/null | openssl x509 -noout -dates

# 也可以用在线工具检测
# https://www.ssllabs.com/ssltest/
这篇教程对你有帮助吗?