SSL证书申请与HTTPS配置实战指南:DV/OV/EV选型、Nginx部署与排错
问题背景与根因:HTTPS强制化与证书有效期新规
微信小程序、苹果ATS、主流浏览器均要求业务站点使用HTTPS。没有有效证书时,浏览器会提示不安全,小程序请求可能直接失败。原文中“默认一年有效期”已过时:按CA/B Forum基线要求,公共可信TLS证书最长有效期为398天;Let’s Encrypt免费证书有效期为90天,建议到期前30天自动续期。EV证书也不再在Chrome等主流浏览器地址栏普遍展示公司名称,EV更多用于高保证身份与合规场景。常见失败根因集中在:443端口未放行、证书链不完整、域名与SAN不匹配、DNS TXT验证未生效、私钥与证书不匹配、续期任务未执行。
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
该命令可查看当前证书主题、签发者、起止时间和SAN。若输出中缺少www.example.com,浏览器会出现NET::ERR_CERT_COMMON_NAME_INVALID;若notAfter已过期,会出现certificate has expired。证书部署不是“上传文件”即完成,必须同时满足域名验证、证书链、私钥权限、TLS版本和HTTP跳转五项检查。
申请前准备:域名、DNS、端口与CSR
申请DV证书前准备:待申请域名、域名管理权限、可添加TXT记录、Web服务器操作系统与版本。若使用DNS-01验证,记录名通常为_acme-challenge.example.com,TTL建议600秒。ECS安全组入方向需放行TCP 80和443,系统防火墙也要放行。CSR可让CA生成,也可本地生成:
openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr -subj '/CN=example.com' -addext 'subjectAltName=DNS:example.com,DNS:www.example.com'
openssl req -in example.com.csr -noout -text | grep -A1 'Subject Alternative Name'
chmod 600 example.com.key
参数说明:rsa:2048兼容性好;ec-256证书体积小,但老客户端需评估。-nodes表示私钥不加密,便于Nginx读取,但必须chmod 600并限制属主。CSR中的CN与SAN必须覆盖实际访问域名,通配符*.example.com不覆盖example.com,也不覆盖a.b.example.com。若使用OV/EV,还需准备营业执照、组织名称、电话等资料,审核通常3-5个工作日;DV通常几分钟到数小时。安全组源地址应按业务限制,不要长期对0.0.0.0/0开放管理端口。
完整实操步骤:用acme.sh申请DV证书
以阿里云DNS API为例,安装acme.sh并申请双域名证书:
curl https://get.acme.sh | sh -s email=admin@example.com
source ~/.bashrc
export Ali_Key='你的AccessKeyId'
export Ali_Secret='你的AccessKeySecret'
acme.sh --issue --dns dns_ali -d example.com -d www.example.com --keylength ec-256
acme.sh --install-cert -d example.com
--key-file /etc/nginx/ssl/example.com.key
--fullchain-file /etc/nginx/ssl/example.com.fullchain.pem
--reloadcmd 'systemctl reload nginx'
若没有DNS API,可用手动DNS模式:先执行acme.sh –issue –dns -d example.com –yes-I-know-dns-manual-mode-enough-go-ahead-please,按提示添加TXT记录,再用dig TXT _acme-challenge.example.com +short确认生效,最后执行acme.sh –renew -d example.com –yes-I-know-dns-manual-mode-enough-go-ahead-please。Webroot方式适合已有80站点:certbot certonly –webroot -w /var/www/html -d example.com -d www.example.com。Let’s Encrypt证书有效期90天,acme.sh默认安装cron自动续期;不要只申请不装续期。申请成功后必须安装fullchain,不能只装leaf证书,否则部分Android和Java客户端报unable to get local issuer certificate。
配置示例:Nginx/Apache部署与HTTP 301跳转
Nginx配置如下,80端口强制301到HTTPS,443启用TLS 1.2/1.3并加载完整链:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/nginx/ssl/example.com.fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_session_cache shared:SSL:10m;
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains' always;
location / { root /var/www/html; index index.html; }
}
执行nginx -t && systemctl reload nginx。Apache示例:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/example.com.fullchain.pem
SSLCertificateKeyFile /etc/httpd/ssl/example.com.key
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
</VirtualHost>
ECS安全组放行TCP 443,系统防火墙执行firewall-cmd –add-service=https –permanent && firewall-cmd –reload或ufw allow 443/tcp。HSTS首次上线建议先用max-age=300测试,确认全站HTTPS后再调大;includeSubDomains需确认所有子域均支持HTTPS。301跳转不要写成302,避免搜索引擎反复抓取HTTP。
证书类型与选型对比表
按验证等级分为DV、OV、EV;按覆盖范围分为单域名、多域名SAN、通配符。选型先看业务,不要盲目追求EV。DV适合个人站、API、测试环境;OV适合企业门户、需要组织身份审核的场景;EV适合金融、电商等高保证场景,但需注意主流浏览器已弱化EV地址栏公司名展示。公共可信证书最长398天,Let’s Encrypt 90天,必须自动化续期。
- 代购 SSL 证书 · DigiCert / GeoTrust / RapidSSL 单域名与通配符
- HTTPS证书申请 · SSL证书安装部署 · 证书续费
- Wildcard 通配符 SSL 证书安装部署续费申请
| 类型 | 验证内容 | 典型签发 | 有效期 | 适用场景 |
|---|---|---|---|---|
| DV | 域名控制权(DNS TXT/HTTP文件) | 几分钟至数小时 | ≤398天;LE 90天 | 博客、小程序后端、API、测试 |
| OV | 域名+组织身份 | 3-5个工作日 | ≤398天 | 企业官网、门户 |
| EV | 严格组织身份+法律资料 | 5-10个工作日 | ≤398天 | 金融、电商、合规高保证 |
| 覆盖范围 | 示例 | 注意 |
|---|---|---|
| 单域名 | www.example.com | 不覆盖example.com |
| 多域名SAN | example.com、www.example.com、api.example.com | 数量受套餐限制 |
| 通配符 | *.example.com | 只覆盖一级子域,不覆盖example.com和a.b.example.com |
小程序与苹果ATS场景优先选DV或OV,关键是证书链完整、TLS 1.2以上、域名备案与业务域名配置正确。免费Let’s Encrypt可用,但必须接受90天周期并配置cron或systemd timer自动续期;商业证书省去运维但也要监控到期。
常见错误排查:真实报错与修复命令
1)nginx: [emerg] cannot load certificate … PEM_read_bio_X509_AUX() failed。多为fullchain文件为空、格式错误或权限不足。执行openssl x509 -in /etc/nginx/ssl/example.com.fullchain.pem -noout -subject -dates;确认文件包含leaf和中间证书,权限644、属主root。2)nginx: [emerg] SSL_CTX_use_PrivateKey … key values mismatch。证书与私钥不匹配,用openssl x509 -noout -modulus -in cert.pem | openssl md5与openssl rsa -noout -modulus -in key.pem | openssl md5比对,值必须一致。3)curl: (60) SSL certificate problem: unable to get local issuer certificate。缺少中间证书,用openssl s_client -connect example.com:443 -servername example.com -showcerts检查链,Nginx改为fullchain。4)acme.sh提示Please add the following TXT record但验证失败。用dig TXT _acme-challenge.example.com +short检查,等待TTL 600秒,确认没有多余空格。5)curl: (7) Failed to connect to example.com port 443: Connection timed out。检查安全组、ufw/firewalld和ss -lntp | grep 443。6)浏览器NET::ERR_CERT_COMMON_NAME_INVALID。SAN缺少www或域名写错,重新签发并包含全部域名。7)certificate has expired。执行acme.sh –cron或certbot renew –dry-run检查续期任务。
效果验证与自动续期
部署后做四项验证:HTTP 301、证书链、TLS版本、到期监控。命令如下:
curl -sS -I http://example.com | head -n 5
curl -sS -I https://example.com | head -n 5
echo | openssl s_client -connect example.com:443 -servername example.com -tls1_2 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
openssl s_client -connect example.com:443 -servername example.com -showcerts 2>/dev/null | grep 'BEGIN CERTIFICATE' | wc -l
openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -checkend 2592000 -noout; echo $?
第一条应返回301 Location: https://…;第二条返回200;第三条确认SAN包含所有域名;第四条证书链数量通常≥2;第五条返回0表示30天内不过期,返回1表示即将过期需续期。自动续期检查:crontab -l | grep acme.sh,systemctl list-timers | grep certbot,acme.sh –cron –home ~/.acme.sh。建议增加到期告警:openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -enddate,并接入监控。若使用CDN/WAF,证书需在CDN侧更新,回源也要校验。最后用浏览器开发者工具Security面板确认TLS 1.2/1.3、证书有效、无混合内容。
常见问题
SSL证书有效期到底多长?
公共可信TLS证书最长398天;Let’s Encrypt为90天。原文“一年有效期”已不准确,应配置自动续期并在到期前30天告警。
DV、OV、EV怎么选?
个人站、小程序后端、API优先DV;企业门户可选OV;金融电商高保证可选EV,但主流浏览器已不普遍在地址栏显示公司名,不要只为UI买EV。
通配符证书能覆盖根域名吗?
不能。*.example.com只覆盖一级子域,example.com需单独加入SAN,a.b.example.com也不覆盖。
HTTP如何301跳转到HTTPS?
Nginx用return 301 https://$host$request_uri;Apache用Redirect permanent / https://example.com/。改完执行nginx -t或apachectl configtest再reload。
证书和私钥不匹配怎么办?
用openssl x509 -noout -modulus与openssl rsa -noout -modulus分别计算MD5,必须一致;不一致就重新签发并替换密钥。
小程序提示SSL handshake error怎么排查?
检查证书链是否完整、TLS是否≥1.2、域名是否在业务域名白名单、443是否可达,并用openssl s_client -tls1_2验证。