Created At :
win-acme 生成证书
使用以下路径,Nginx 路径请换成你实际安装的位置:
1 2 3 4
| win-acme:D:\devops\win-acme Nginx: D:\devops\nginx 证书目录:D:\devops\nginx\ssl 域名: iot-agent.cn、*.iot-agent.cn
|
1. 先补齐腾讯云插件
我检查了你目前的目录,尚未发现腾讯云插件。需要从 win-acme 发布页 下载:
- x64 pluggable 主程序包。
- 同版本的 Tencent DNS 验证插件包。
解压到 D:\devops\win-acme,保留已有的 settings.json。腾讯云插件需要 pluggable 版本,小体积 trimmed
版本不能加载该插件。官方要求
用 管理员 PowerShell运行:
1 2 3 4 5 6 7 8
| Set-Location D:\devops\win-acme
# 解除下载文件的锁定 Get-ChildItem -File -Recurse | Unblock-File
# 检查腾讯云参数是否已加载 .\wacs.exe --help --verbose | Select-String 'tencent|tencentapiid|tencentapikey'
|
应能看到腾讯云相关参数,再继续。
2. 配置 ECDSA P-256
对应 Linux 文档里的 --keylength ec-256。
打开配置:
1
| notepad D:\devops\win-acme\settings.json
|
找到 Csr 下的 Ec,修改这两个值,其他配置保留:
1 2 3 4
| "Ec": { "CurveName": "secp256r1", "SignatureAlgorithm": "SHA256withECDSA" }
|
仅使用 --csr ec 时,win-acme 默认曲线是 P-384;这里显式配置成与你文档一致的
P-256。配置说明
3. 创建证书目录和自动重载脚本
1 2
| New-Item -ItemType Directory -Force ` -Path D:\devops\nginx\ssl | Out-Null
|
创建 Nginx 重载脚本,供首次签发和以后续期调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @' @echo off cd /d D:\devops\nginx if errorlevel 1 exit /b 1
rem 首次部署时,如果 Nginx 尚未启动,暂不重载 if not exist logs\nginx.pid exit /b 0
nginx.exe -t if errorlevel 1 exit /b 1
nginx.exe -s reload exit /b %errorlevel% '@ | Set-Content ` -LiteralPath D:\devops\win-acme\Scripts\reload-nginx.cmd ` -Encoding ASCII
|
win-acme 支持签发或续期成功后执行 .cmd、.bat、.ps1
等部署脚本。官方说明
4. 申请根域名与通配符证书
对应 Linux 的:
1 2 3 4 5
| acme.sh --issue \ -d iot-agent.cn \ -d '*.iot-agent.cn' \ --dns dns_tencent \ --keylength ec-256
|
Windows PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| Set-Location D:\devops\win-acme
$TencentSecretId = Read-Host '请输入腾讯云 SecretId'
# 隐藏输入,不把密钥字面值写入 PowerShell 命令历史 $SecretCredential = Get-Credential ` -UserName $TencentSecretId ` -Message '请在密码栏填写腾讯云 SecretKey'
$CertificateArgs = @( '--source', 'manual' '--host', 'iot-agent.cn,*.iot-agent.cn' '--friendlyname', 'iot-agent-cn-wildcard'
'--baseuri', 'https://acme-v02.api.letsencrypt.org/' '--emailaddress', '替换成你的邮箱' '--accepttos'
'--validationmode', 'dns-01' '--validation', 'tencent' '--tencentapiid', $TencentSecretId '--tencentapikey', $SecretCredential.GetNetworkCredential().Password
'--csr', 'ec'
'--store', 'pemfiles' '--pemfilespath', 'D:\devops\nginx\ssl' '--pemfilesname', 'iot-agent'
'--installation', 'script' '--script', 'D:\devops\win-acme\Scripts\reload-nginx.cmd' )
& .\wacs.exe @CertificateArgs $IssueExitCode = $LASTEXITCODE
Remove-Variable CertificateArgs, SecretCredential, TencentSecretId
if ($IssueExitCode -ne 0) { throw '证书申请或部署失败,请检查 win-acme 输出' }
|
这里 SecretId 对应 --tencentapiid,SecretKey 对应 --tencentapikey;win-acme 会调用 DNS API
完成验证。腾讯云参数说明
成功后检查文件:
1
| Get-ChildItem D:\devops\nginx\ssl
|
主要会生成:
1 2 3 4
| iot-agent-crt.pem 站点证书 iot-agent-key.pem 私钥 iot-agent-chain.pem 站点证书 + 完整证书链 iot-agent-chain-only.pem 中间证书链
|
Nginx 使用 iot-agent-chain.pem 和
iot-agent-key.pem。PEM 文件说明
5. 配置 Windows Nginx
打开配置:
1
| notepad D:\devops\nginx\conf\nginx.conf
|
在已有的 http { ... } 内加入或修改对应的 server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| server { listen 443 ssl;
server_name iot-agent.cn *.iot-agent.cn;
ssl_certificate D:/devops/nginx/ssl/iot-agent-chain.pem; ssl_certificate_key D:/devops/nginx/ssl/iot-agent-key.pem;
location / { proxy_pass http://127.0.0.1:8080; 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; } }
|
将 8080 改成实际应用端口。Windows Nginx 配置中的路径使用 /。Nginx 官方说明
检查配置:
1 2 3
| Set-Location D:\devops\nginx
.\nginx.exe -t
|
检查通过后, 尚未启动时执行:
1 2 3 4
| Start-Process ` -FilePath D:\devops\nginx\nginx.exe ` -WorkingDirectory D:\devops\nginx ` -WindowStyle Hidden
|
已经启动时执行:
6. 配置和检查自动续期
对应 Linux 的 cron:
1 2 3 4 5 6 7 8 9 10 11 12
| Set-Location D:\devops\win-acme
# 创建或更新自动续期计划任务 .\wacs.exe --setuptaskscheduler
# 查看已保存的证书续期配置 .\wacs.exe --list
# 查看 Windows 计划任务 Get-ScheduledTask | Where-Object TaskName -Like '*win-acme*' | Select-Object TaskName, State
|
正常检查到期续期:
需要手动强制验证整条续期、证书导出和 Nginx 重载流程时执行:
1 2 3
| .\wacs.exe --renew ` --friendlyname 'iot-agent-cn-wildcard' ` --force
|
强制续期只用于必要的验证,不要反复执行。正常情况下,由 Windows 计划任务检查续期,成功后调用前面的脚本重载
Nginx。续期机制