本文记录了一次真实的智能运维实战——借助 QClaw AI 助手,从零开始为一个生产环境网站部署免费 SSL 证书,并解决部署后出现的 Mixed Content 问题。全程无需手动敲命令,AI 自动完成所有操作。


背景

今天收到了腾讯云的QClaw的内测邀请码,正好最近开发了一个基于区块链的教育评价系统平台,还没有进行ssl的部署,心血来潮用龙虾试一下。

网站运行在阿里云 Ubuntu 22.04 服务器上,使用宝塔面板管理,Nginx 作为 Web 服务器。网站包含两个前端入口:

  • 📱 手机端http://yourdomain.com/mobile
  • 💻 电脑端http://yourdomain.com/admin
  • 🔌 API 服务http://api.yourdomain.com(Node.js,反向代理到本地 3000 端口)

目标:全站升级 HTTPS,部署免费 SSL 证书。


第一步:环境侦察

QClaw 首先通过 DNS 解析确认域名指向的服务器 IP,然后 SSH 登录服务器,自动检测环境:

nginx -v        # nginx/1.26.3(宝塔自带版本)
certbot --version  # 未安装

发现关键信息:

  • 服务器上跑着两套 Nginx:系统自带的 + 宝塔自带的(/www/server/nginx
  • 宝塔 Nginx 占用了 80 端口
  • 宝塔已为域名创建了 /www/server/panel/vhost/nginx/yourdomain.com.conf,并预留了 SSL 配置区

第二步:安装 Certbot

apt-get install -y certbot python3-certbot-nginx

⚠️ 踩坑提示:直接用 certbot --nginx 会失败,因为 Certbot 会尝试重启系统 Nginx,但 80 端口已被宝塔 Nginx 占用。正确做法是用 --webroot 模式,让宝塔 Nginx 来处理验证请求。


第三步:验证域名所有权

先测试 .well-known 路径是否可以从外网访问:

mkdir -p /www/wwwroot/.well-known/acme-challenge
echo "test" > /www/wwwroot/.well-known/acme-challenge/test.txt
curl http://yourdomain.com/.well-known/acme-challenge/test.txt
# 返回:test ✅

确认可访问后,申请主域名证书:

certbot certonly --webroot \
  -w /www/wwwroot \
  -d yourdomain.com \
  --non-interactive --agree-tos \
  --email admin@yourdomain.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/yourdomain.com/privkey.pem
This certificate expires on 2026-06-14.

💡 www.yourdomain.com 申请失败,因为该子域名没有配置 DNS 解析,跳过即可。


第四步:配置主域名 HTTPS

QClaw 自动生成并写入 Nginx 配置,核心结构如下:

# HTTP → HTTPS 强制跳转
server {
    listen 80;
    server_name yourdomain.com;
    return 301 https://$host$request_uri;
}

# HTTPS 主配置
server {
    listen 443 ssl;
    http2 on;
    server_name yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:10m;

    # 手机端
    location ^~ /mobile/ {
        alias /www/wwwroot/web/mobile/dist/;
        try_files $uri $uri/ /mobile/index.html;
    }

    # 电脑端
    location ^~ /admin/ {
        alias /www/wwwroot/web/admin/dist/;
        try_files $uri $uri/ /admin/index.html;
    }
}

重载 Nginx:

/www/server/nginx/sbin/nginx -t   # 配置检查
/www/server/nginx/sbin/nginx -s reload

验证结果:

https://yourdomain.com  →  HTTP/2 200 ✅
http://yourdomain.com   →  301 → https:// ✅

第五步:发现新问题——Mixed Content

HTTPS 部署完成后,用户反馈登录页验证码加载失败,浏览器控制台报错:

Mixed Content: The page at 'https://yourdomain.com/admin/login' was loaded 
over HTTPS, but requested an insecure XMLHttpRequest endpoint 
'http://api.yourdomain.com/system/auth/captcha'. 
This request has been blocked.

原因分析:

页面:https://yourdomain.com/admin/   ← HTTPS ✅
API: http://api.yourdomain.com/...   ← HTTP ❌ 被浏览器安全策略拦截

浏览器的安全策略规定:HTTPS 页面不允许请求 HTTP 资源


第六步:给 API 域名也部署 SSL

QClaw 自动完成以下操作:

1. 申请 api 子域名证书

certbot certonly --webroot \
  -w /www/wwwroot/education-server-master \
  -d api.yourdomain.com \
  --non-interactive --agree-tos \
  --email admin@yourdomain.com

2. 更新 API 的 Nginx 配置

server {
    listen 80;
    server_name api.yourdomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    http2 on;
    server_name api.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/api.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.yourdomain.com/privkey.pem;

    # 反向代理到本地 Node.js 服务
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

验证:

https://api.yourdomain.com/system/auth/captcha  →  HTTP/2 200 ✅
http://api.yourdomain.com  →  301 → https:// ✅

第七步:修复前端硬编码的 HTTP 地址

服务端 HTTPS 配好了,但浏览器还是报错。QClaw 进一步排查,发现前端打包的 JS 文件里 API 地址是硬编码的 http://

grep -r "http://api.yourdomain.com" /www/wwwroot/web/ -l
# 找到 admin 端 10 个文件、mobile 端 8 个文件

一条命令批量替换所有文件:

find /www/wwwroot/web/ -name "*.js" \
  | xargs grep -l "http://api.yourdomain.com" \
  | xargs sed -i 's|http://api\.yourdomain\.com|https://api.yourdomain.com|g'

验证替换结果:

grep -r "http://api.yourdomain.com" /www/wwwroot/web/ -l | wc -l
# 0 ✅ 全部替换完毕

最终结果

项目 状态
https://yourdomain.com ✅ HTTPS 正常,HTTP/2
https://api.yourdomain.com ✅ HTTPS 正常,HTTP/2
HTTP → HTTPS 自动跳转 ✅ 301 重定向
证书自动续期 ✅ Certbot 定时任务已配置
Mixed Content 错误 ✅ 已消除
手机端 /mobile ✅ 正常访问
电脑端 /admin ✅ 正常访问,验证码恢复
证书有效期 2026-06-14(90天自动续期)

经验总结

1. 宝塔环境下不能直接用 certbot --nginx
宝塔有自己的 Nginx,系统 Certbot 不认识它的配置路径,必须用 --webroot 模式手动申请证书,再手动写配置。

2. 部署 HTTPS 不只是主域名的事
只要前端有跨域 API 请求,所有涉及的域名都需要同步升级 HTTPS,否则会触发 Mixed Content 拦截。

3. 前端打包文件里可能有硬编码的 HTTP 地址
这是最容易被忽略的坑。服务端配好了 HTTPS,但前端 JS 里的 API 地址还是 http://,浏览器照样拦截。解决方法:grep 找出来,sed 批量替换。

4. QClaw 智能运维的价值
整个过程从环境侦察、证书申请、Nginx 配置、问题排查到批量修复,全部由 QClaw 自动完成,无需人工逐条敲命令,大幅降低了运维门槛。


🦞 龙虾说:SSL 不是终点,是起点。HTTPS 只是安全的第一步,后续还可以配置 HSTS、CSP、WAF 等进一步加固。有了 QClaw 智能运维,这些都不是难事。

Logo

小龙虾开发者社区是 CSDN 旗下专注 OpenClaw 生态的官方阵地,聚焦技能开发、插件实践与部署教程,为开发者提供可直接落地的方案、工具与交流平台,助力高效构建与落地 AI 应用

更多推荐