跳到主要内容
ssl证书按装

HTTPS/SSL 证书安装配置与部署实战:从 DV 申请到 Nginx/Apache/Tomcat/IIS 加固

HTTPS/SSL 证书安装配置与部署实战:从 DV 申请到 Nginx/Apache/Tomcat/IIS 加固 — HTTPS/SSL 证书安装配置与部署实战:从 DV 申请到 Nginx/Apache/Tomcat/IIS 加固

问题背景与根因:为什么 HTTP 必须升级为 HTTPS

HTTP 明文传输会导致 Cookie、表单、Token 被监听或篡改,运营商劫持、公共 Wi-Fi 注入、混合内容降级都源于此。浏览器对 HTTP 页面标记“不安全”,Google、百度等搜索引擎也把 HTTPS 作为排名正向因素。根因通常不是“没买证书”这么简单,而是证书链不完整、TLS 版本过旧、证书过期、SNI 未配置、负载均衡终止 TLS 后后端仍走 HTTP。需要特别修正两个过时认知:公开信任 SSL 证书最长有效期自 2020 年 9 月起为 398 天,Let’s Encrypt 免费证书有效期为 90 天,必须自动化续期;TLS 1.0/1.1 已被主流浏览器弃用,SHA-1 签名证书已淘汰,生产环境应启用 TLS 1.2 与 TLS 1.3。排查时先看协议与证书链:curl -I http://example.com 确认是否 301 跳转;openssl s_client -connect example.com:443 -servername example.com -showcerts 查看是否返回完整链;openssl x509 -in cert.pem -noout -dates -subject -issuer -ext subjectAltName 检查有效期与 SAN。若只配了站点证书而漏掉中间证书,Android、Java、部分 curl 会报 unable to get local issuer certificate。另一个常见根因是 80 端口被防火墙关闭,导致 HTTP-01 验证失败,需改用 DNS-01 或放行 80。HSTS 不要一上来就 includeSubDomains 加 preload,应先短 max-age 观察,否则回退困难。

证书类型选型与 CSR 生成:DV、OV、EV 怎么选

SSL 证书按验证强度分为 DV、OV、EV。DV 只验证域名控制权,适合个人站、API、APP 后端;OV 需验证企业工商信息,适合政府组织与企业官网;EV 审核最严,但原文“激活绿色地址栏”已过时,现代浏览器已取消 EV 绿色地址栏展示,EV 主要体现为证书策略 OID 与更高审核成本,金融、支付机构可按合规要求选择。所有公开信任证书都受 398 天最长有效期限制,Let’s Encrypt 为 90 天,TrustAsia 等免费 DV 也需关注签发策略与自动续期。选型对比见下表。

类型 验证内容 签发时长 信任等级 适用场景
DV SSL 域名控制权,HTTP-01/DNS-01 分钟级 普通 个人站、APP、测试环境
OV SSL 域名+企业工商信息 1-3 个工作日 强 企业官网、政府组织
EV SSL 严格企业身份与法律文件 3-7 个工作日 最高 金融、支付、强合规

生成 CSR 与私钥可直接执行:openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr -subj '/C=CN/ST=Beijing/L=Beijing/O=Example/CN=example.com' -addext 'subjectAltName=DNS:example.com,DNS:www.example.com'。检查 CSR:openssl req -in example.com.csr -noout -text。私钥权限必须收紧:chmod 600 example.com.key。注意通配符 *.example.com 不覆盖根域 example.com,需要同时加入 SAN;多域名站点也应使用 SAN,而不是靠 CN 兼容。OV/EV 申请时提交的营业执照、组织名称必须与 CSR 中 O 字段一致,否则 CA 会退回。

完整实操:用 Certbot 申请 Let’s Encrypt 证书并自动续期

以 Ubuntu/Debian 为例:apt update && apt install -y certbot python3-certbot-nginx;CentOS/RHEL 可用 dnf install -y certbot python3-certbot-nginx。Nginx 站点已存在时,一条命令完成申请、配置与跳转:certbot --nginx -d example.com -d www.example.com --agree-tos -m admin@example.com --redirect。若使用 Webroot,先确保 80 端口可达:certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com。通配符证书必须用 DNS-01:certbot certonly --manual --preferred-challenges dns -d '*.example.com' -d example.com,按提示添加 TXT 记录;生产环境建议用 DNS 插件自动写入。证书文件位于 /etc/letsencrypt/live/example.com/fullchain.pem 与 privkey.pem,Nginx/Apache 应引用 fullchain 而不是 cert.pem,否则链不完整。自动续期:systemctl enable --now certbot.timer,或 cron 0 3 * * * certbot renew --quiet --deploy-hook 'systemctl reload nginx'。Let’s Encrypt 有效期 90 天,剩余 30 天进入续期窗口,必须执行 certbot renew --dry-run 验证。若报 Problem binding to port 80: Could not bind to IPv4 or IPv6,说明 80 被占用,可停掉占用服务或改用 DNS-01。CAA 记录要允许签发机构,例如 example.com. IN CAA 0 issue 'letsencrypt.org',否则 CA 拒绝签发。

Nginx 配置示例:HTTPS、HTTP/2、HSTS 与反向代理

以下配置可直接落地,注意 Nginx 1.25.1 起推荐 http2 on;,旧版本使用 listen 443 ssl http2;。80 端口强制跳转,443 启用 TLS 1.2/1.3、OCSP Stapling、HSTS 与反向代理头。

server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
http2 on;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
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:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains' always;
add_header X-Content-Type-Options nosniff always;
location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Real-IP $remote_addr; }
}

执行 nginx -t && systemctl reload nginx。若后端也走 HTTPS,负载均衡到后端需加 proxy_pass https://backend;、proxy_ssl_verify on;、proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;、proxy_ssl_server_name on;,否则会报证书校验失败。HSTS 建议先设 max-age=300 观察,再逐步提升到 31536000。若站点有子域未全量 HTTPS,先不要 includeSubDomains。OCSP Stapling 需要 resolver 或系统 DNS 可用,且 chain.pem 正确,否则 stapling 状态为失败但不影响主链路。

Apache、Tomcat、IIS 配置示例

Apache 先启用模块:a2enmod ssl headers && systemctl restart apache2。VirtualHost 示例:

<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
Header always set Strict-Transport-Security 'max-age=31536000; includeSubDomains'
</VirtualHost>

Tomcat 需把 PEM 转 PKCS12:openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out example.com.p12 -name tomcat -CAfile chain.pem -caname root。server.xml 的 Connector 关键参数:port='443' protocol='org.apache.coyote.http11.Http11NioProtocol' SSLEnabled='true' scheme='https' secure='true' keystoreFile='/opt/tomcat/conf/example.com.p12' keystoreType='PKCS12' keystorePass='***' SSLProtocol='TLSv1.2,TLSv1.3'。IIS 用 PowerShell 导入 PFX:Import-PfxCertificate -FilePath 'C:/certs/example.com.pfx' -CertStoreLocation 'Cert:/LocalMachine/My',再用 netsh http add sslcert hostnameport=example.com:443 certhash=THUMBPRINT appid={GUID} certstorename=MY 绑定 SNI。若报 0x80070520,多为私钥权限或绑定冲突,用 certutil -store My 检查证书与私钥。Tomcat 报 PKIX path building failed 时,优先修复服务端 fullchain,而不是盲目改 JVM truststore。

证书链、TLS 版本与密码套件加固

证书链是 HTTPS 部署最容易忽略的环节。服务端必须发送“站点证书+中间证书”,根证书由客户端内置信任库提供。验证命令:openssl s_client -connect example.com:443 -servername example.com -showcerts,观察输出是否包含中间 CA;openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt fullchain.pem 应返回 OK。若链缺失,Java、Android 7 以下、部分 curl 会直接失败。TLS 加固原则:禁用 SSLv3、TLS 1.0、TLS 1.1;RSA 密钥至少 2048 位,优先 ECDSA P-256;TLS 1.3 密码套件由协议固定,不需要在 Nginx 中枚举。Nginx 推荐 Mozilla intermediate 套件:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384。开启 ssl_session_tickets off 可减少前向安全风险;OCSP Stapling 需正确配置 ssl_trusted_certificate。CAA 记录可限制签发机构,降低误签发风险。检查工具:nmap --script ssl-enum-ciphers -p 443 example.com、testssl.sh example.com、SSL Labs 在线评分。目标为 TLS 1.2/1.3 可用、无弱套件、证书链完整、HSTS 生效。若使用 CDN 或负载均衡,证书可能部署在边缘节点,回源也要校验证书,避免“边缘 HTTPS、回源 HTTP”被内网嗅探。

常见错误排查:真实报错与修复命令

排查先看三处日志:journalctl -u nginx、tail -f /var/log/letsencrypt/letsencrypt.log、浏览器开发者工具 Security 面板。常见报错与修复如下表。

真实报错 根因 修复
curl: (60) SSL certificate problem: unable to get local issuer certificate 服务端缺中间证书 Nginx/Apache 改用 fullchain.pem,重载服务
nginx: [emerg] no 'ssl_certificate' is defined for the 'listen ... ssl' directive 443 server 块缺证书指令 补 ssl_certificate 与 ssl_certificate_key
NET::ERR_CERT_DATE_INVALID 证书过期或系统时间错误 openssl x509 -enddate -noout -in cert.pem,执行 certbot renew --force-renewal
SSL_ERROR_BAD_CERT_DOMAIN SAN 不含当前域名 重新签发,加入根域、www、通配符需求
PKIX path building failed Java 客户端不信任服务端链 修复服务端 fullchain,或导入中间 CA 到 truststore
0x80070520 IIS 证书绑定失败 certutil -store My 检查私钥权限,重新 netsh 绑定 SNI
Problem binding to port 80: Could not bind to IPv4 or IPv6 80 端口被占用 停占用服务或改用 DNS-01 验证
AH02572: Failed to configure at least one certificate and key Apache 路径错误或权限不足 检查 fullchain/privkey 路径与 chmod 600

若浏览器报 ERR_SSL_PROTOCOL_ERROR,检查是否把 HTTPS 配到了 80 端口、是否启用了已废弃协议、是否被防火墙 TLS 代理干扰。若续期失败但站点仍可访问,通常是旧证书未过期,需在剩余 30 天内修复。HSTS 开启后回退要先把 max-age 改为 0 并观察,不能直接删头。

效果验证:从命令行到在线评分

部署后按顺序验证。第一,跳转:curl -I http://example.com 应返回 301 到 https;curl -I https://example.com 应返回 200 且含 strict-transport-security。第二,协议:openssl s_client -connect example.com:443 -servername example.com -tls1_2 2>/dev/null | grep 'Protocol',再用 -tls1_3 验证;若 TLS 1.0/1.1 仍可握手,说明 ssl_protocols 未生效。第三,证书:openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -dates -subject -issuer -ext subjectAltName,确认 SAN 覆盖所有域名,到期时间大于 30 天。第四,链:openssl s_client -connect example.com:443 -servername example.com -showcerts 2>/dev/null | openssl x509 -noout -issuer,issuer 应为中间 CA 而非站点自身。第五,在线评分:SSL Labs 目标 A 或 A+,HSTS preload 需满足 max-age 至少 31536000、includeSubDomains、preload 且所有子域 HTTPS。第六,自动化监控:certbot renew --dry-run 加入巡检;用 echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -enddate 提取到期日,接入 Prometheus blackbox_exporter 或 Zabbix。最后检查混合内容:grep -R 'http://' /var/www/html,把静态资源改为相对协议或 https。日志中 495/496 表示客户端证书错误,可辅助定位 TLS 握手问题。

常见问题

DV 只验证域名控制权,分钟级签发,适合个人站和 API;OV 验证企业工商信息,EV 审核更严但浏览器已取消绿色地址栏。三者加密强度相同,差别在身份可信度与合规场景。

公开信任 SSL 证书最长有效期为 398 天;Let’s Encrypt 免费证书为 90 天,剩余 30 天进入续期窗口。必须用 certbot.timer 或 cron 自动续期,并定期执行 dry-run。

服务端只发送了站点证书,缺少中间 CA。Nginx/Apache 应引用 fullchain.pem 而不是 cert.pem,Tomcat 的 PKCS12 需包含 CA 链,IIS 导入 PFX 时也要包含中间证书。

若 TLS 在负载均衡终止,边缘需要证书;回源若走 HTTPS,后端也要配置证书或受信 CA。回源走 HTTP 时需确保内网可信,并设置 X-Forwarded-Proto,避免应用生成 http 链接。

先把 max-age 改为 0 并保留 includeSubDomains 观察,待浏览器缓存过期后再删除头。不要直接删 HSTS 头,否则已缓存用户仍会强制 HTTPS。preload 移除需向浏览器列表提交。

内容审核

长期从事 SSL/TLS 证书部署与 HTTPS 运维,负责本站证书选型、安装配置与故障排查内容的编写与审核。