SSL证书配置示例:Nginx 1.26 与 Apache 2.4 的 7 步参数清单(2026 版)
SSL 证书配置示例的核心是让服务器在 443 端口发送叶证书加中间证书的 PEM 链,并在 Nginx 1.26.1 中设置 ssl_protocols TLSv1.2 TLSv1.3、Mozilla intermediate 套件、OCSP stapling 与 HSTS,最后用 openssl s_client -connect 域名:443 -servername 域名 看到 Verify return code: 0 (ok)。 Nginx 1.26.1 最小可用片段:ssl_certificate 指向 fullchain.pem,ssl_certificate_key 指向 600 权限私钥,ssl_session_cache shared:SSL:10m,ssl_session_timeout 1d,ssl_stapling on 且配置 resolver 8.8.8.8 valid=300s。 Apache 2.4.58 用 SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1 与 SSLHonorCipherOrder off;Caddy 2.8.4 默认自动申请和续期,HAProxy 2.9 需把证书、私钥、链拼成一个 PEM 并用 ssl-min-ver TLSv1.2。 DV 证书年费约 0-300 元、OV 约 800-3000 元、DV 通配符约 300-1500 元;单站点配置工时 0.5-2 小时,验证命令 2-5 分钟可完成。
为什么直接复制 SSL 证书配置示例会失败?先看 6 个根因
把一份网上的 SSL 证书配置示例原样贴进 nginx.conf 或 httpd-ssl.conf,结果服务起不来、或者起来了浏览器仍报错,绝大多数情况不是模板写错,而是运行环境与模板的假设不一致。下面 6 个根因按排查顺序排列,每条都附可直接复制的验证命令。
1. 文件格式与编码不匹配——为什么 nginx -t 报 PEM_read_bio_X509_AUX failed?
Nginx 1.26.1 与 Apache 2.4.58 的 mod_ssl 都只读取 PEM 文本(Base64 + -----BEGIN CERTIFICATE----- 边界)。先用 file 判类型:
file example.com.crt
# 期望输出:example.com.crt: ASCII text
# 若输出 "...: data",说明是 DER 二进制,必须先转换
openssl x509 -inform DER -in example.com.crt -out example.com.pem
openssl x509 -in example.com.pem -noout -subject -dates # 验证可解析
# 从 p12 导出时注意私钥单独拆出并 chmod 600
openssl pkcs12 -in all.p12 -nodes -out full.pem
2. 证书链不完整——为什么 curl 报 unable to get local issuer certificate?
据 RFC 5280 第 6 节的证书路径验证规则,服务器应发送叶证书与其签发中间 CA,根证书由客户端本地信任库提供、不必发送。看服务器实际发了什么:
openssl s_client -connect example.com:443 -servername example.com -showcerts /dev/null
| grep -E '^s*[is]:'
# i: 行代表中间 CA,至少要出现 1 个;只有 s: 行即为链不完整
Nginx 的 ssl_certificate 应指向 fullchain(叶证书在前、中间 CA 在后);Apache 自 2.4.8 起 SSLCertificateChainFile 已废弃,中间证书直接追加进 SSLCertificateFile 指向的文件。
3. 服务器默认协议不同——为什么 nmap 仍扫出 TLSv1.0: enabled?
Nginx 官方文档中 ssl_protocols 的默认值已是 TLSv1.2 TLSv1.3;而 Apache 2.4.58 的 SSLProtocol 默认仍是 all -SSLv3,all 含 TLSv1.0/1.1,从旧模板抄来的这一行会原样放开这两个协议。
nmap --script ssl-enum-ciphers -p 443 example.com # 检查 TLSv1.0/1.1 是否 enabled
# Nginx
ssl_protocols TLSv1.2 TLSv1.3;
# Apache:用白名单写法,避免以后版本新增协议被意外放开
SSLProtocol -all +TLSv1.2 +TLSv1.3
4. 密码套件与 TLS 1.3 的关系被误解——为什么写了 ssl_ciphers 还出现 TLS_AES_256_GCM_SHA384?
Nginx 的 ssl_ciphers 与 Apache 的 SSLCipherSuite 都只控制 TLS 1.2 及以下;TLS 1.3 套件由 OpenSSL 3.0 的默认策略决定,要改得动 openssl.cnf 里的 Ciphersuites。据 Mozilla SSL Configuration Generator 的 intermediate 档,TLS 1.2 部分应给完整列表而非一行:
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:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off; # TLS 1.3 时代交给客户端排序更优
5. SNI 与多站点——为什么同一 IP 上第二个域名报 NET::ERR_CERT_COMMON_NAME_INVALID?
域名通过 ClientHello 的 SNI 扩展传递,客户端不带或服务端未匹配到 server_name 时会回落到该 IP 的默认站点证书。
openssl s_client -connect 1.2.3.4:443 /dev/null | openssl x509 -noout -subject
openssl s_client -connect 1.2.3.4:443 -servername www.example.com /dev/null | openssl x509 -noout -subject
Nginx 侧必须在 443 的 server 块显式写 server_name example.com www.example.com;,只写 listen 443 ssl 会命中第一个 server 块或 default_server。
6. HSTS 与 OCSP stapling 有前置条件
HSTS 只对“已通过 HTTPS 收到过该响应头”的客户端生效,首次访问不带;max-age=31536000; includeSubDomains 下发后无法撤回,建议先用短 max-age 灰度。OCSP stapling 需同时满足:证书内含 OCSP URL、配置了 resolver、以及指向完整链的 ssl_trusted_certificate。
openssl x509 -in fullchain.pem -noout -ocsp_uri # 无输出则证书不支持 stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 valid=300s; # 缺此行常见日志:ssl_stapling ignored, issuer certificate not found
ssl_trusted_certificate /etc/nginx/ssl/chain.pem;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
把上述差异集中对比,能解释为什么同一份 SSL 证书配置示例在两个 Web 服务器上表现完全不同:
| 项目 | Nginx 1.26.1 | Apache 2.4.58 | 建议值 |
|---|---|---|---|
| 协议指令与默认值 | ssl_protocols,默认 TLSv1.2 TLSv1.3 |
SSLProtocol,默认 all -SSLv3(含 TLSv1/1.1) |
TLSv1.2 TLSv1.3 / -all +TLSv1.2 +TLSv1.3 |
| 证书链写法 | ssl_certificate 指向 fullchain |
2.4.8 起并入 SSLCertificateFile |
叶证书在前、中间 CA 在后,不发送根证书 |
| TLS 1.3 套件控制 | 均不受 ssl_ciphers/SSLCipherSuite 影响,由 OpenSSL 3.0 默认策略决定 |
改 openssl.cnf 的 Ciphersuites |
|
| OCSP stapling | ssl_stapling on + resolver |
SSLUseStapling on + SSLStaplingCache |
两者都需可信链文件,缺一即 no response sent |
把一份下载包里的三个文件变成线上可用的 HTTPS,本质是一条”合并 → 选模板 → 查语法 → 平滑重载 → 挂续期钩子 → 留基线”的固定流水线。下面每一步都给可复制命令,照着敲一遍约 15 分钟。
步骤 1:合并 PEM 并收紧权限(3-5 分钟)。CA 下载包通常含 example.com.crt(站点证书)、ca_bundle.crt(中间证书)和 example.com.key。顺序不能反——据 RFC 5280 的证书链构建规则,服务器必须把叶子证书放在最前面,否则部分客户端会报 unable to get local issuer certificate。
# 站点证书在前,中间证书在后,合并为 fullchain
cat example.com.crt ca_bundle.crt > example.com.fullchain.pem
chmod 600 example.com.key # 私钥仅属主可读
chmod 644 example.com.fullchain.pem # 证书链可公开读取
# 确认主体与到期时间(notAfter 是唯一可靠的到期依据)
openssl x509 -in example.com.fullchain.pem -noout -subject -dates
# 输出示例:subject=CN = example.com
# notAfter=Jun 12 03:14:07 2026 GMT
步骤 2:用 Mozilla 生成器选模板,而不是手抄 cipher 串。打开 Mozilla SSL Configuration Generator,Server 选 Nginx 1.26.1 / Apache 2.4.58 / HAProxy 2.9,OpenSSL 版本选 3.0,配置档选 intermediate,会直接给出 ssl_protocols TLSv1.2 TLSv1.3; 与对应的 ssl_ciphers 列表。不要选 old 档,除非你必须兼容 Android 4.4 这类只支持 TLS 1.0/弱套件的终端;据 RFC 8446,TLS 1.3 的套件协商已与密钥交换解耦,1.2+1.3 的组合足以覆盖 2026 年主流浏览器。
步骤 3:改配置后先做语法检查,别直接 reload。三款服务器的成功输出各不相同,认准关键字:
| 服务器 | 检查命令 | 成功输出关键字 | 平滑重载命令 |
|---|---|---|---|
| Nginx 1.26 | nginx -t |
syntax is ok / test is successful | systemctl reload nginx |
| Apache 2.4.58 | apachectl configtest |
Syntax OK | systemctl reload httpd |
| HAProxy 2.9 | haproxy -c -f /etc/haproxy/haproxy.cfg |
Configuration file is valid | systemctl reload haproxy |
nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
报错时先看它给出的行号与文件名,90% 的问题出在证书路径拼写或漏写分号。
步骤 4:灰度 reload 并回看日志(10-30 秒)。reload 不会断开已建立连接,但新 worker 会重新加载证书。重载后立刻确认没有致命错误:
systemctl reload nginx
journalctl -u nginx -n 20 --no-pager | grep -i emerg # 应无输出
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates
第二条命令用真实握手验证线上证书就是你刚部署的那张,而不是旧文件缓存。
步骤 5:把续期与 reload 绑成钩子。Let’s Encrypt 证书有效期 90 天(据 Let’s Encrypt 官方文档),acme.sh 默认在第 60 天续期;Caddy 2.8.4 内置自动续期,无需钩子。Nginx 侧用 --install-cert 一次性写入续期后动作:
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' # 续期成功后自动生效
步骤 6:记录基线与成本。保存证书指纹和时间戳,下次排查”证书是否真的换了”时有据可查:
openssl x509 -in example.com.fullchain.pem -noout -fingerprint -sha256
date -u # 输出示例:Tue Jun 16 02:11:04 UTC 2026
预算参考:DV 证书年费约 0-300 元,OV 约 800-3000 元,DV 通配符约 300-1500 元;按上述流程单站点部署工时约 0.5-2 小时,自动化钩子配好后每年只需核对一次 notAfter。
Nginx 1.26.1 的 SSL 证书配置示例怎么写?关键参数逐行说明
先给结论:Nginx 1.26.1(2024 年 6 月进入 stable 分支)里,一份能直接上线的 443 server 块至少包含以下六行关键指令。据 Nginx 官方文档 ngx_http_ssl_module 章节,ssl_certificate 必须指向包含站点证书和中间证书的 fullchain 文件,只放站点证书会导致部分 Android 与 Java 客户端报 unable to get local issuer certificate。
# /etc/nginx/conf.d/example.com.conf —— Nginx 1.26.1 适用
server {
listen 443 ssl; # 1.25.1 起不再把 http2 写进 listen
http2 on; # 新写法,替代 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;
}
为什么必须改成 http2 on;? 旧的 listen 443 ssl http2; 在 1.26.1 中仍可解析,但每次 reload 会打印:nginx: [warn] the "listen ... http2" directive is deprecated, use the "http2" directive instead。若反向照抄 1.24 的配置并混用两种写法,会出现 [emerg] invalid parameter "http2" 之类报错。用 nginx -v 确认版本后再选写法。
协议与加密套件建议对齐 Mozilla SSL Configuration Generator 的 Intermediate 档(面向 TLS 1.2/1.3 双栈、兼容主流客户端):
ssl_protocols TLSv1.2 TLSv1.3;
# 仅保留 ECDHE + AEAD 套件,前向保密 + 抗篡改
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; # 默认 off;让客户端按其偏好排序
Mozilla 明确建议 ssl_prefer_server_ciphers off:现代客户端(Chrome/Firefox/iOS)的套件排序已优先考虑 ChaCha20 与硬件 AES 加速,服务端强行指定顺序反而降低移动端性能。注意 TLS 1.3 的套件由 OpenSSL 内置的 ssl_conf 控制,ssl_ciphers 对其无效,不要指望在这里关掉 TLS 1.3 的某个套件。
| 参数 | 默认值 | 本示例取值 | 作用与取舍 |
|---|---|---|---|
ssl_protocols |
TLSv1.2 TLSv1.3(1.23.4+) | TLSv1.2 TLSv1.3 | 显式声明,避免继承旧 include 文件里的 TLSv1/1.1 |
ssl_prefer_server_ciphers |
off | off | 交由客户端排序,移动端更省电 |
ssl_session_cache |
none | shared:SSL:10m | 官方示例:1MB≈4000 会话,10m≈4 万 |
ssl_session_tickets |
on | off | 关闭后无 ticket 密钥轮换风险,但完整握手比例上升 |
ssl_stapling |
off | on | 省去客户端额外拉取 OCSP 的 RTT |
# 会话复用:减少 TLS 握手开销
ssl_session_cache shared:SSL:10m; # 10MB 共享内存,跨 worker 复用
ssl_session_timeout 1d; # 默认 5m,配合 cache 可放宽到 1 天
ssl_session_tickets off; # 关闭 ticket,代价是首访更常走完整握手
# OCSP stapling:证书必须含 OCSP URL,且需可用的 DNS resolver
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 1.1.1.1 valid=300s;
resolver_timeout 5s;
验证 stapling 是否真的生效(看到 OCSP Response Status: successful (0x0) 即成功;若显示 no response sent,多为 resolver 不通或证书缺 OCSP URL):
openssl s_client -connect example.com:443 -servername example.com -status </dev/null 2>&1 | grep -A2 "OCSP Response"
# HSTS:首次上线先用 300 秒观察 24 小时,再阶梯上调到一年
add_header Strict-Transport-Security "max-age=300" always;
# 稳定后改为: "max-age=31536000; includeSubDomains"
# 依据 RFC 6797,includeSubDomains 会覆盖全部子域,先确认子域均支持 HTTPS
证书路径与权限是最容易翻车的一步。 目录 750、私钥 600 属主 root、fullchain 644 是通行做法,但很多人只改文件不改目录,结果 worker 进程读不到:
sudo install -d -m 750 -o root -g root /etc/nginx/ssl
sudo chown root:root /etc/nginx/ssl/example.com.key
sudo chmod 600 /etc/nginx/ssl/example.com.key
sudo chmod 644 /etc/nginx/ssl/example.com.fullchain.pem
namei -l /etc/nginx/ssl/example.com.key # 逐级打印每层目录权限
# namei 输出示例:只要任意一级是 d--------- 就会失败
f: /etc/nginx/ssl/example.com.key
drwxr-xr-x root root /
drwxr-xr-x root root etc
drwxr-xr-x root root nginx
drwxr-x--- root root ssl
-rw------- root root example.com.key
若某级目录变成 700 且属主不是 root,reload 时会出现 nginx: [emerg] cannot load certificate key "/etc/nginx/ssl/example.com.key": BIO_new_file() failed (SSL: error:8000000D:system library::Permission denied)。改完权限务必 sudo nginx -t && sudo systemctl reload nginx,再用 openssl s_client -connect example.com:443 -servername example.com 检查 Protocol : TLSv1.3 与 Verify return code: 0 (ok),才算这份 SSL 证书配置示例真正跑通。
Apache 2.4.58、Caddy 2.8.4 与 HAProxy 2.9 的配置示例有什么不同?
一句话答案:Apache 把证书和私钥拆成两个文件、靠 SSLCertificateFile 指向 fullchain;Caddy 默认根本不写证书路径,交给内置 ACME 自动签发;HAProxy 则强制把「叶证书 → 中间链 → 私钥」拼进同一个 PEM。下面这份 SSL 证书配置示例按三台服务器的真实可运行配置展开,并附参数默认值与校验命令。
Apache 2.4.58:证书分文件,链合并进 fullchain
# /etc/httpd/conf.d/example-ssl.conf —— Apache 2.4.58 (RHEL/Rocky 路径)
LoadModule ssl_module modules/mod_ssl.so
LoadModule headers_module modules/mod_headers.so
<VirtualHost *:443>
ServerName example.com
SSLEngine on
# 2.4.8 起 SSLCertificateFile 可直接指向"叶证书+中间链"拼接的 fullchain,
# SSLCertificateChainFile 已标记 deprecated,无需再写
SSLCertificateFile /etc/httpd/ssl/example.com.fullchain.pem
SSLCertificateKeyFile /etc/httpd/ssl/example.com.key
# SSLProtocol 默认值为 all(含 TLSv1/1.1),必须显式减掉
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
# 仅对 TLS 1.2 及以下生效,TLS 1.3 套件由 OpenSSL 决定
SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256
SSLHonorCipherOrder off
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</VirtualHost>
据 Apache HTTP Server 2.4 官方 mod_ssl 文档,SSLCertificateChainFile 自 2.4.8 起被弃用,因为 SSLCertificateFile 允许在叶证书之后追加中间证书。若仍按老教程分开写,新版会忽略链文件并在 apachectl configtest 时给出 AH01905: SSL certificate chain file ... is deprecated 类告警。
Caddy 2.8.4:能自动就别手写
# /etc/caddy/Caddyfile —— 方式 A:使用已有证书
example.com {
tls /etc/caddy/example.com.fullchain.pem /etc/caddy/example.com.key
header Strict-Transport-Security "max-age=31536000; includeSubDomains"
encode gzip
}
# 方式 B:自动 HTTPS(推荐),不写 tls 指令
example.com {
encode gzip
# Caddy 通过 ACME 申请 90 天证书并自动续期,默认监听 :443
}
Caddy 默认最低 TLS 版本为 TLS 1.2(可在 tls 块用 protocols tls1.3 收紧),且 TLS 1.3 套件由 Go 标准库固定,Caddyfile 中没有等价于 ssl_ciphers 的旋钮——这也是它比 Nginx/Apache 少一半配置行的原因。
HAProxy 2.9:单一 PEM,顺序和权限都不能错
# 拼接顺序固定:叶证书 → 中间证书 → 私钥
cat example.com.crt intermediate.crt example.com.key > /etc/haproxy/certs/example.com.pem
chmod 600 /etc/haproxy/certs/example.com.pem # 权限过宽 haproxy 会拒绝启动
chown haproxy:haproxy /etc/haproxy/certs/example.com.pem
# /etc/haproxy/haproxy.cfg
frontend fe_https
bind :443 ssl crt /etc/haproxy/certs/example.com.pem alpn h2,http/1.1 ssl-min-ver TLSv1.2
default_backend be_web
backend be_web
server web1 127.0.0.1:8080 check
HAProxy 2.5 起 ssl-min-ver 的默认值已提升为 TLSv1.2,显式写出只是为了审计可读。若顺序拼错(例如私钥放最前),启动会直接失败:
[ALERT] (2311) : config : parsing [/etc/haproxy/haproxy.cfg:12] : ‘bind’ in section ‘frontend’ : unable to load SSL private key from PEM file ‘/etc/haproxy/certs/example.com.pem’.
三种服务器横向对比
| 维度 | Apache 2.4.58 | Caddy 2.8.4 | HAProxy 2.9 |
|---|---|---|---|
| 证书载体 | 证书、私钥分文件 | 证书、私钥分文件 | 叶+链+私钥拼成单一 PEM |
| 证书链写法 | SSLCertificateFile 指向 fullchain(2.4.8+) |
tls 直接给 fullchain.pem |
顺序敏感,拼在 crt 文件内 |
| 自动签发/续期 | 无,需 certbot 等外部工具 | 内置 ACME,默认开启 | 无 |
| 最小 TLS 版本默认 | all(含 TLSv1/1.1,须手动关) |
TLS 1.2 | TLS 1.2(2.5 起) |
| TLS 1.3 套件控制 | SSLOpenSSLConfCmd Ciphersuites ... |
不可配(Go 标准库固定) | ciphersuites 关键字 |
| 配置校验命令 | apachectl configtest |
caddy validate --config |
haproxy -c -f |
多域名 SNI 怎么写?Apache 复制多个 <VirtualHost *:443>,每个 ServerName 配各自证书;Caddy 在同一个 Caddyfile 里并列多个站点块即可;HAProxy 用 crt-list,每行「PEM 路径 + SNI 名」:
- HTTPS配置 · SSL证书安装服务(各种服务器)
- APP/小程序 HTTPS · SSL网站加密证书 · CA证书配置
- NGINX / APACHE / TOMCAT / IIS 证书配置 · SSL证书安装
# /etc/haproxy/certs.list
/etc/haproxy/certs/example.com.pem example.com
/etc/haproxy/certs/api.example.com.pem api.example.com
# 绑定:bind :443 ssl crt-list /etc/haproxy/certs.list alpn h2,http/1.1
# 验证任意一台都必须带 -servername,否则只会拿到默认证书
openssl s_client -connect example.com:443 -servername example.com -alpn h2 </dev/null 2>/dev/null
| openssl x509 -noout -subject -issuer -dates
为什么改了 SSLCipherSuite 却看不到效果?因为它只管 TLS 1.2 及以下。据 OpenSSL 3.0 文档,TLS 1.3 套件由独立参数 Ciphersuites 控制,默认值为 TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256。要改可在 Nginx 用 ssl_conf_command Ciphersuites ...(需 OpenSSL 1.1.1+、nginx 1.19.4+),Apache 用 SSLOpenSSLConfCmd Ciphersuites ...;Mozilla SSL Configuration Generator 也建议大多数站点保持默认,因为这三个套件都已通过 AEAD 认证。
reload 前先跑哪四条命令?四者都要在 systemctl reload 之前执行,只有输出 Syntax OK / Valid configuration / Configuration file is valid 才继续:
nginx -t # nginx: configuration file /etc/nginx/nginx.conf test is successful
apachectl configtest # Syntax OK
caddy validate --config /etc/caddy/Caddyfile # Valid configuration
haproxy -c -f /etc/haproxy/haproxy.cfg # Configuration file is valid
典型失败输出例如 nginx: [emerg] cannot load certificate "/etc/nginx/ssl/example.com.fullchain.pem": BIO_new_file() failed (SSL: error:80000002:system library::No such file or directory),多为 SELinux 上下文或路径拼写问题;而 caddy validate 报 unrecognized directive 时,几乎都是 Caddyfile 缩进或站点块括号写错。上述 SSL 证书配置示例覆盖了三类部署形态,若后续要接 OCSP Stapling、HSTS preload 或 TLS 1.3 0-RTT,可在这些基线配置上逐项追加。
TLS 参数与方案怎么选?一张对比表看协议、套件、OCSP 与工时
选型不是“哪个服务器软件更好”,而是四件事必须一次定死:协议下限、套件集合、OCSP stapling 开关、HSTS 头。任何一项单独配置都可能被浏览器判定为“不安全”或直接降级。下面这张表是 2026 年仍可直接抄用的示例值。
| 方案 | 推荐版本 | 协议默认/建议 | 套件配置项 | OCSP stapling | HSTS 写法 | 部署工时 | 适用场景 |
|---|---|---|---|---|---|---|---|
| Nginx | 1.26.1 | 默认 TLSv1 TLSv1.1 TLSv1.2;建议 TLSv1.2 TLSv1.3 |
ssl_ciphers = Mozilla intermediate 列表 |
ssl_stapling on + resolver 8.8.8.8 valid=300s |
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always |
0.5–1 小时 | 高流量站点、需要精细调参 |
| Apache | 2.4.58 | 默认 all -SSLv3;建议 all -SSLv3 -TLSv1 -TLSv1.1 |
SSLCipherSuite 同上 |
SSLUseStapling on + SSLStaplingCache shmcb:/run/httpd/stapling(128000) |
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" |
1–2 小时 | 传统企业应用、.htaccess 生态 |
| Caddy | 2.8.4 | 默认 TLSv1.2/1.3 | 默认套件,无需手写 | 自动 | header Strict-Transport-Security "max-age=31536000; includeSubDomains" |
0.2–0.5 小时 | 新站点、想省续期运维 |
| HAProxy | 2.9 | ssl-min-ver TLSv1.2,可加 ssl-max-ver TLSv1.3 |
ssl-default-bind-ciphers |
需显式配置 | http-response set-header Strict-Transport-Security ... |
1–2 小时 | 四层/七层负载均衡入口 |
Nginx 的最小可用片段(放在 server 块或 http 块,nginx 1.26.1 + OpenSSL 3.x):
# /etc/nginx/conf.d/tls.conf
ssl_protocols TLSv1.2 TLSv1.3; # 默认值含 TLSv1/TLSv1.1,1.23.4 起已被官方标记 deprecated
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; # 默认 off,让客户端选更利于 TLS1.3 与移动端
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; # 缺这行 stapling 会静默失效
resolver 8.8.8.8 1.1.1.1 valid=300s; # 无 resolver 时 stapling 直接不工作
resolver_timeout 5s;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Apache 2.4.58 + mod_ssl 的等价写法:
# /etc/httpd/conf.d/ssl.conf
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite 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
SSLHonorCipherOrder off
SSLUseStapling on
SSLStaplingCache "shmcb:/run/httpd/stapling(128000)" # 必须全局唯一,否则启动报 AH01929
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
三个容易踩的坑。其一,ssl_ciphers 管不到 TLS 1.3——RFC 8446 把 TLS1.3 套件从 cipher suite 中拆出,nginx 需 ssl_conf_command Ciphersuites ...(1.19.4+)才能覆盖,OpenSSL 默认三个套件已足够安全。其二,OCSP stapling 失败往往无声:日志出现 ssl_stapling" ignored, issuer certificate not found for certificate 就说明缺 ssl_trusted_certificate 全链。更要紧的是,据 Let’s Encrypt 公告《Ending OCSP Support in 2025》,其证书已分阶段移除 OCSP URL 并关闭响应器,因此对 LE 证书 stapling 基本只剩兼容意义,商业 CA 证书仍有效;想验证可用 openssl s_client -connect example.com:443 -status 看是否返回 OCSP Response Status: successful。其三,HSTS 的 max-age 单位是秒,常见值 31536000(1 年),提交 preload 列表要求 ≥ 6 个月且带 includeSubDomains,一旦下发无法回滚,先在测试域验证。
成本怎么估。DV 单域名 0–300 元/年,OV 800–3000 元/年,DV 通配符 300–1500 元/年;Let’s Encrypt 为 0 元但需 90 天续期(acme.sh / certbot 自动化后近乎零人力);Cloudflare 边缘证书免费,但源站仍须自备证书,否则回源链路仍是明文或自签。按上表工时 0.5–2 小时、时薪 200–500 元估算,单站点一次性投入约 100–1000 元,这也是为什么 Caddy 的“自动 stapling + 自动续期”在新项目里性价比最高。
配置后出现报错怎么办?6 条真实错误信息与排查命令
问:reload 之后 nginx 直接 [emerg] 退出,是证书买错了吗? 大多数情况下证书本身没问题,出错的是文件格式、路径或拼接顺序。下面 6 条报错覆盖了 PEM 解析、证书链不全、密钥不匹配、Apache 权限与 SELinux、浏览器不信任、HAProxy 拼接顺序这六类高频故障;每一条都配了可直接复制的验证命令和期望输出,也是上一节 SSL证书配置示例 落地后最该先跑的检查清单。
1)nginx: [emerg] PEM_read_bio_X509_AUX failed (SSL: error:0480006C:PEM routines::no start line)
OpenSSL 3.x(Nginx 1.26 官方包默认链接 3.0/3.2)读取文件首行时找不到 PEM 头,说明 ssl_certificate 指到了私钥,或用了 IIS/Windows 导出的 DER 二进制。
# 首行必须是 PEM 文本头
head -n 1 /etc/nginx/ssl/example.com.fullchain.pem
# 期望:-----BEGIN CERTIFICATE-----
# 解析主体;DER 文件此处会直接报 unable to load certificate
openssl x509 -in /etc/nginx/ssl/example.com.fullchain.pem -noout -subject
# 期望:subject=CN = example.com
# 确认是 DER 后再转换回 PEM
openssl x509 -inform DER -in example.com.der -out example.com.pem
2)curl: (60) SSL certificate problem: unable to get local issuer certificate
叶证书发了、中间证书没发。据 Let’s Encrypt 文档,fullchain.pem 必须是“叶证书 + 中间证书”,顺序不可颠倒。
# 数服务端实际下发的证书条数,返回 0 即只发了叶证书
openssl s_client -connect example.com:443 -servername example.com -showcerts /dev/null | grep -c '^ i:'
# 期望:>= 1(通常 1~2)
# 修复:拼接中间证书,再本地验证
cat example.com.crt ca_bundle.crt > example.com.fullchain.pem
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt example.com.fullchain.pem
# 期望:example.com.fullchain.pem: OK
3)nginx: [emerg] SSL_CTX_use_PrivateKey(…) failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch)
证书与私钥不是一对,常见于续签时只替换了 crt。RSA 比对模数、ECC 比对公钥:
openssl x509 -noout -modulus -in cert.pem | openssl md5
openssl rsa -noout -modulus -in key.pem | openssl md5
# 两行 MD5 必须完全一致
# ECC 证书无 modulus,改用公钥比对
openssl x509 -noout -pubkey -in cert.pem | openssl md5
openssl pkey -pubout -in key.pem | openssl md5
4)AH02572: Failed to configure at least one certificate and key for example.com:443
Apache 2.4 下多为路径写错、权限 640 但属主不是 root(httpd 以 root 启动、降权前读取),或 SELinux 上下文为 user_home_t。
apachectl -t -D DUMP_VHOSTS # 打印实际生效的 vhost 与证书路径
ls -lZ /etc/httpd/ssl # 期望上下文 system_u:object_r:cert_t:s0
chown root:root /etc/httpd/ssl/* && chmod 640 /etc/httpd/ssl/*
restorecon -Rv /etc/httpd/ssl # 修正 SELinux 标签
tail -n 20 /var/log/httpd/ssl_error_log
5)NET::ERR_CERT_AUTHORITY_INVALID
浏览器不信任签发 CA:自签证书、内部 CA 未导入客户端信任库,或误把根证书塞进下发链(根证书不应下发,会放大握手体积并干扰部分客户端的链构建)。
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt /etc/nginx/ssl/example.com.fullchain.pem
# 打印链中所有证书,检查是否混入自签根
openssl crl2pkcs7 -nocrl -certfile /etc/nginx/ssl/example.com.fullchain.pem | openssl pkcs7 -print_certs -noout
6)[ALERT] config : ‘bind :443’ : unable to load SSL private key from PEM file(HAProxy 2.9)
HAProxy 要求单文件内按“证书 → 中间证书 → 私钥”排列,且文件权限过宽会被直接拒绝。
cat example.com.crt chain.pem example.com.key > example.com.pem
chmod 600 example.com.pem
haproxy -c -f /etc/haproxy/haproxy.cfg
# 期望:Configuration file is valid
| 报错关键字 | 根因 | 验证命令 | 期望输出 |
|---|---|---|---|
| no start line | 文件非 PEM / 指向私钥 | openssl x509 -in X -noout -subject |
subject=CN = example.com |
| unable to get local issuer | 缺中间证书 | openssl verify -CAfile … fullchain.pem |
…: OK |
| key values mismatch | 证书与私钥不配对 | -modulus | openssl md5 |
两个 MD5 相同 |
| AH02572 | 路径 / 属主 / SELinux | apachectl -t -D DUMP_VHOSTS |
无 Syntax Error |
| ERR_CERT_AUTHORITY_INVALID | CA 不被信任 / 链含根证书 | openssl verify -CAfile … |
OK 且链仅 1~2 张 |
| unable to load SSL private key | 拼接顺序或权限错误 | haproxy -c -f … |
Configuration file is valid |
据 OpenSSL 官方文档,openssl verify 默认使用系统信任库,验证链时务必显式传 -CAfile,否则结果会随宿主机 ca-certificates 版本漂移;Mozilla SSL Configuration Generator 生成的片段同样假设 fullchain 已正确拼接。排错顺序建议固定为:先用 openssl x509 确认单文件可解析,再用 openssl verify 确认链完整,最后才 reload 服务。
怎么验证 SSL 证书配置已生效?6 个命令与预期输出
验证 SSL 证书配置是否生效,要分三层看:握手层(协议版本与密码套件)、证书层(证书链、有效期、OCSP)、服务层(HTTP 响应与端口监听)。下面 6 条命令覆盖这三层,每条都给出预期输出和失败时的排查方向。执行前请把 example.com 换成你自己的域名。
1)强制 TLS 1.3 握手,看协议、套件与链验证码
# -servername 传 SNI,否则多域名服务器可能返回默认站点证书
# -tls1_3 限定只走 TLS 1.3;</dev/null 避免交互挂起
openssl s_client -connect example.com:443 -servername example.com -tls1_3 </dev/null
预期输出:Protocol : TLSv1.3、Cipher : TLS_AES_256_GCM_SHA384、Verify return code: 0 (ok)。若看到 Verify return code: 20 (unable to get local issuer certificate),说明服务器只发了站点证书、没发中间证书,回到链配置:Nginx 的 ssl_certificate 应指向 fullchain.pem 而非 cert.pem(据 Let’s Encrypt 文档,fullchain.pem 才是完整链)。若报 EE certificate key too weak,是 OpenSSL 3.x 默认安全级别 2 拒绝了小于 2048 位的 RSA 密钥。
2)看 HTTP 状态码与 HSTS 头
curl -I https://example.com
# 预期首行 HTTP/2 200 或 HTTP/1.1 200
# 预期含 strict-transport-security: max-age=31536000; includeSubDomains
缺 HSTS 头就去查 add_header Strict-Transport-Security(Nginx)或 Header always set(Apache 2.4,需启用 mod_headers)。若只返回 HTTP/1.1,注意 Nginx 自 1.25.1 起废弃了 listen 443 ssl http2 里的 http2 参数,必须改成 listen 443 ssl; 加独立指令 http2 on;,否则会打印 [warn] the "listen ... http2" directive is deprecated。
3)单独验证 TLS 1.2 握手
curl -I --tlsv1.2 --tls-max 1.2 https://example.com # 预期仍返回 200
4)枚举协议与密码套件
nmap --script ssl-enum-ciphers -p 443 example.com
预期只出现 TLSv1.2: 与 TLSv1.3: 两个段落,且没有 TLSv1.0: enabled。出现 RC4、3DES 等弱套件时检查 ssl_ciphers:Apache 2.4 默认 SSLProtocol all -SSLv3,仍包含 TLS 1.0/1.1,必须显式写 SSLProtocol -all +TLSv1.2 +TLSv1.3;Nginx 自 1.23.4 起 ssl_protocols 默认值才是 TLSv1.2 TLSv1.3,老版本请照 Mozilla SSL Configuration Generator 的 intermediate 配置显式覆盖。
5)用 testssl.sh 做整体评级
testssl.sh --severity HIGH https://example.com # 3.2 版,扫 443 端口约 2–5 分钟
预期出现 No critical/high findings 以及 OCSP stapling: offered。若 stapling 显示 not offered,Nginx 需配 ssl_stapling on; ssl_stapling_verify on; 并用 ssl_trusted_certificate 指向链文件。
6)查有效期,并在 reload 后确认进程状态
openssl s_client -connect example.com:443 -servername example.com -status </dev/null
| openssl x509 -noout -dates
# 预期 notBefore 早于当前时间,notAfter 在 60–90 天内
systemctl reload nginx
ss -ltnp | grep :443 # 预期处于 LISTEN
journalctl -u nginx -n 20 --no-pager # 预期无 [emerg]
curl -s -o /dev/null -w '%{http_code} %{time_total}' https://example.com
# 预期 200 与 0.1–0.5 秒(含握手的总时间)
Let’s Encrypt 证书 notAfter 距签发约 90 天,因此剩余 60–90 天是正常态;若只剩几天,说明 certbot 的续期定时器没跑起来。
| 命令 | 验证目标 | 通过判据 | 失败先查 |
|---|---|---|---|
openssl s_client -tls1_3 |
协议/套件/证书链 | Verify return code: 0 (ok) | fullchain.pem、密钥长度 |
curl -I |
HTTP 与 HSTS 头 | 200 + max-age=31536000 | add_header、http2 on |
curl --tls-max 1.2 |
TLS 1.2 兼容性 | 200 | ssl_protocols / SSLProtocol |
nmap ssl-enum-ciphers |
协议与弱套件 | 仅 1.2/1.3,无 RC4/3DES | ssl_ciphers、SSLHonorCipherOrder |
testssl.sh |
整体评级、OCSP | No critical/high findings | stapling 三项指令 |
openssl x509 -dates |
有效期与续期 | 剩余 60–90 天 | certbot timer |
通常不要。服务器发送叶证书和中间 CA 即可,根证书由客户端内置信任库提供;据 RFC 5280,根证书放在链里会增加握手字节且部分客户端会忽略。用 openssl s_client -showcerts 看到叶证书和 1-2 张中间证书即可。
新站点用 Caddy 2.8.4 只写域名即可自动申请和续期 90 天证书,配置量约 3 行;Nginx 1.26.1 需要自己跑 acme.sh 或 certbot 并写 ssl_certificate 路径。已有 Nginx 生态、需要精细控制套件和缓存时选 Nginx,追求少运维时选 Caddy。
先确认服务器是否发送中间证书:openssl s_client -connect 域名:443 -servername 域名 -showcerts 看 i: 条目。若缺少,把 CA 提供的 ca_bundle.crt 与叶证书拼成 fullchain 并让 ssl_certificate 指向它;再用 openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt fullchain.pem 验证返回 OK。
一般站点抄 Mozilla SSL Configuration Generator 的 intermediate 档,兼容 TLS 1.2/1.3 和主流浏览器;要兼容 Android 4.4 等旧客户端才用 old 档。TLS 1.3 套件不受 ssl_ciphers 控制,由 OpenSSL 3.0 默认策略决定。
首次上线建议 max-age=300 观察 24 小时,确认所有子域 HTTPS 正常后再调到 31536000(1 年);加 includeSubDomains 前要确认没有仅 HTTP 的子域,否则子域会被浏览器强制 HTTPS 而不可访问。
多域名 SAN 证书在一张证书里列多个 DNS: 名称,Nginx 仍用一个 server_name 列表;通配符 *.example.com 只覆盖一级子域,不覆盖 example.com 本身和 a.b.example.com。价格上 DV 通配符约 300-1500 元/年,多域名 DV 按域名数增加,通常每增加 1 个 SAN 加 50-200 元/年。
不一定是。先检查 Nginx 是否配置 resolver 8.8.8.8 valid=300s,Apache 是否启用 SSLUseStapling on 和 SSLStaplingCache;再用 openssl x509 -in fullchain.pem -noout -ocsp_uri 看证书是否带 OCSP URL。若都没有,换支持 OCSP 的 CA 或忽略该指标。
本文由 sslgood.com 安全团队基于生产环境实战经验撰写,关键结论均经过技术复核。