公网 HTTPS 证书获取 / 更新 / 部署手册
生成日期:2026-07-30
适用环境:演示公网机 182.92.106.252 / 域名 ruoyioffice.com
关联配置:../vben/nginx/ruoyi-office.conf、locations-ruoyi-office.inc(见 Nginx 配置说明)
1. 架构与目录约定
| 项 | 路径 / 说明 |
|---|---|
| 公网机 | 182.92.106.252(nginx Docker 容器名 nginx) |
| 域名 | 主域名 ruoyioffice.com;www 可选(当前 DNS 无 A 记录时勿加入证书 SAN) |
| 策略(首期) | HTTP + HTTPS 双开,不强制 301 |
| 证书颁发 | Let’s Encrypt(certbot webroot,不停机) |
| 宿主机证书 | /data/nginx/ssl/ruoyioffice.com/ → 链到 /etc/letsencrypt/live/ruoyioffice.com/ |
| 容器内证书 | /etc/nginx/ssl/ruoyioffice.com/{fullchain,privkey}.pem |
| ACME webroot | 宿主机 /data/nginx/html/acme/ → 容器 /usr/share/nginx/html/acme/ |
| 站点 conf | /data/nginx/conf/conf.d/ruoyi-office.conf |
| 公共 location | /data/nginx/conf/conf.d/locations-ruoyi-office.inc(80/443 共用) |
| 续期脚本 | 本目录 renew-and-reload.sh(拷到公网机 /data/nginx/bin/) |
Browser
├─ https://ruoyioffice.com/* → nginx :80
└─ https://ruoyioffice.com/* → nginx :443 (ssl_certificate from /data/nginx/ssl)
Let’s Encrypt HTTP-01 → /.well-known/acme-challenge/ on :80 (webroot)
certbot renew → 写 live/ → reload nginx 容器2. 首次签发步骤
2.1 前置检查
# DNS
getent hosts ruoyioffice.com www.ruoyioffice.com
# 应解析到 182.92.106.252
# 端口(安全组需放行 80/443)
ss -lntp | grep -E ':80|:443'2.2 目录
mkdir -p /data/nginx/ssl /data/nginx/html/acme/.well-known/acme-challenge /data/nginx/bin2.3 nginx 容器增加 ssl 挂载
原挂载含 conf / html / logs。需额外:
/data/nginx/ssl -> /etc/nginx/ssl示例(保留原有 bind,按实际 docker inspect nginx 调整):
docker stop nginx
docker rm nginx
docker run -d --name nginx --restart unless-stopped \
-p 80:80 -p 443:443 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
-v /data/nginx/html:/usr/share/nginx/html:ro \
-v /data/nginx/logs:/var/log/nginx \
-v /data/nginx/ssl:/etc/nginx/ssl:ro \
nginx:latest若线上还有
network_mode/ 其它 volume,以 inspect 为准合并,勿丢挂载。
2.4 配置 ACME + server_name(仅 80 先生效)
- 备份:
cp -a /data/nginx/conf/conf.d/ruoyi-office.conf /data/nginx/conf/conf.d/ruoyi-office.conf.bak.https-$(date +%Y%m%d%H%M%S) server_name含:ruoyioffice.com www.ruoyioffice.com 182.92.106.252 _;- 在业务 location 之前增加:
location ^~ /.well-known/acme-challenge/ {
root /usr/share/nginx/html/acme;
default_type text/plain;
}docker exec nginx nginx -t && docker exec nginx nginx -s reload
2.5 安装 certbot 并签发
CentOS 7:
yum install -y epel-release
yum install -y certbot签发(运维邮箱可改)。仅在 www 已解析到本机时再加 -d www.ruoyioffice.com,否则 Let’s Encrypt 校验失败:
# 当前线上:仅 apex(www 尚无 A 记录)
certbot certonly --webroot -w /data/nginx/html/acme \
-d ruoyioffice.com \
--email admin@ruoyioffice.com --agree-tos --non-interactive --no-eff-email
# www 就绪后扩容 SAN:
# certbot certonly --webroot -w /data/nginx/html/acme \
# -d ruoyioffice.com -d www.ruoyioffice.com \
# --expand --email admin@ruoyioffice.com --agree-tos --non-interactive --no-eff-email挂到 nginx 使用目录:
mkdir -p /data/nginx/ssl/ruoyioffice.com
# 推荐:拷贝实文件(容器只读挂载时无法跟随 /etc/letsencrypt/live 内相对软链)
cp -L /etc/letsencrypt/live/ruoyioffice.com/fullchain.pem /data/nginx/ssl/ruoyioffice.com/
cp -L /etc/letsencrypt/live/ruoyioffice.com/privkey.pem /data/nginx/ssl/ruoyioffice.com/
chmod 644 /data/nginx/ssl/ruoyioffice.com/fullchain.pem
chmod 600 /data/nginx/ssl/ruoyioffice.com/privkey.pem
docker exec nginx ls -la /etc/nginx/ssl/ruoyioffice.com/续期脚本
renew-and-reload.sh会在续期后再次cp -L同步并 reload。
2.6 启用 443
ruoyioffice.conf 中增加 listen 443 ssl http2 的 server,与 80 共用 locations-ruoyi-office.inc,证书路径:
ssl_certificate /etc/nginx/ssl/ruoyioffice.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/ruoyioffice.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;首期不做 return 301 https://...。
docker exec nginx nginx -t && docker exec nginx nginx -s reload3. nginx / Docker 挂载变更清单
| 变更 | 说明 |
|---|---|
宿主机 /data/nginx/ssl | 证书(或软链) |
容器 -v .../ssl:/etc/nginx/ssl:ro | 必加 |
locations-ruoyi-office.inc | 业务 location 单一来源 |
ruoyi-office.conf | 仅两个 server(80/443)+ include |
| ACME location | 仅需在 80(443 也可 include 同一文件,无害) |
4. 续期与 cron
将本仓库脚本拷到公网机:
# 从文档仓或运维机上传
install -m 755 renew-and-reload.sh /data/nginx/bin/renew-and-reload.shcron(每天 03:17):
17 3 * * * /data/nginx/bin/renew-and-reload.sh >> /var/log/letsencrypt-renew-nginx.log 2>&1脚本行为:certbot renew → 若有更新则同步 pem 到 /data/nginx/ssl/... → docker exec nginx nginx -s reload。
手动试跑: /data/nginx/bin/renew-and-reload.sh
5. 验证清单
curl -sI https://ruoyioffice.com/web | head -5
curl -sIk https://ruoyioffice.com/web | head -10
curl -sIk https://ruoyioffice.com/weather-api/health
echo | openssl s_client -servername ruoyioffice.com -connect ruoyioffice.com:443 2>/dev/null | openssl x509 -noout -dates -subject期望:HTTP/HTTPS 均 200;证书 CN/SAN 含 ruoyioffice.com;HTTP 访问未变成强制跳转。
6. 回滚
# 恢复 conf 备份
cp -a /data/nginx/conf/conf.d/ruoyi-office.conf.bak.https-XXXX \
/data/nginx/conf/conf.d/ruoyi-office.conf
docker exec nginx nginx -t && docker exec nginx nginx -s reload业务即可回到纯 HTTP。证书目录可保留不影响。
7. 常见问题
| 现象 | 处理 |
|---|---|
| challenge 404 | 检查 ACME root 与 certbot -w 是否同为 .../html/acme;nginx -t/reload |
| certbot DNS 失败 | getent hosts;补全 www A 记录 |
| 容器内证书 File not found | live 软链出容器解析失败 → 用脚本拷贝实文件到 /data/nginx/ssl/ |
| 443 握手失败 | 安全组、容器 -p 443:443、conf 是否 listen 443 ssl |
| HTTPS 页 OnlyOffice 白屏 | 混合内容:http://…:18081 被拦 → 见 OnlyOffice 文档 与 Nginx 配置说明 中 locations-ruoyi-office.inc 的 /onlyoffice 备选反代 |
| 天气运营台 Cookie | 双开阶段保持 ADMIN_COOKIE_SECURE=false;全站强制 HTTPS 后再改 true |
8. 关联系统副作用
| 组件 | 首期双开 | 日后强制 HTTPS |
|---|---|---|
现有 http:// 入口 | 不变 | 环境变量改为 https:// |
| 天气运营台 | ADMIN_COOKIE_SECURE=false | 改为 true |
OnlyOffice :18081 | HTTP 页可用 | 须 HTTPS 反代 |
| 前端硬编码 http | 仍可用 | 逐步替换 |
9. 变更记录
| 日期 | 操作 | 备注 |
|---|---|---|
| 2026-07-30 | 初版方案落地 | LE 签发 ruoyioffice.com(无 www);ssl 挂载 + 443 双开;cron 17 3 * * *;文档与续期脚本入库 |
