Nginx开启OCSP Stapling需同时满足五项前提(版本、OCSP URI、网络连通、证书链完整、时间同步)并配置四条核心指令,缺一即静默失效;推荐使用ssl_stapling_file预载响应实现零延迟。

在 Nginx 中开启 OCSP Stapling 不是加一行 ssl_stapling on 就能生效的操作,而是一套必须全部满足、协同工作的机制。缺任何一环,Nginx 都会静默关闭 stapling——不报错、不警告,客户端仍需自行发起 OCSP 查询,导致 TLS 握手多出 150–300ms 延迟。
确认五项硬性前提是否全部达标
任一不满足,stapling 直接失效,且无日志提示:
- Nginx 版本 ≥ 1.3.7(生产环境建议 ≥ 1.11.0);OpenSSL ≥ 1.0.1(推荐 1.1.1+ 或 3.0+)
- 你的证书中必须包含有效的 OCSP URI:运行
openssl x509 -in your.crt -text -noout | grep -A1 "OCSP",输出中应有类似OCSP - URI: http://ocsp.int-x3.letsencrypt.org - 服务器能直连该 OCSP 地址:用
openssl ocsp -url http://ocsp.int-x3.letsencrypt.org -cert your.crt -CAfile fullchain.pem -text测试,看到responseStatus: successful才算通 - 证书链完整:
ssl_certificate必须是域名证书 + 中间证书合并的 PEM 文件(如 Let’s Encrypt 的fullchain.pem) - 系统时间偏差 ≤ ±5 分钟:用
chronyc tracking或ntpdate -q pool.ntp.org检查,超差会导致 OCSP 响应中的thisUpdate/nextUpdate校验失败
在 server 块中配齐四条核心指令
这四行必须同时出现在 HTTPS 的 server { listen 443 ssl; } 块内,不能嵌套在 location 或其他块中:
-
ssl_stapling on;—— 启用装订功能 -
ssl_stapling_verify on;—— 强制校验 OCSP 响应签名、颁发者和有效期,防伪造或过期数据被误用 -
ssl_trusted_certificate /path/to/full-chain-trusted.pem;—— 这不是你的站点证书文件,而是专用于验证 OCSP 响应的信任链(中间 CA 在前 + 根 CA 在后),不能含域名证书,也不能复用ssl_certificate -
resolver 8.8.8.8 1.1.1.1 223.5.5.5 valid=300s;—— Nginx 不读/etc/resolv.conf,必须显式指定至少两个 DNS;valid=300s防止 DNS 缓存过期中断 stapling
强烈建议追加:resolver_timeout 5s;,避免一次 DNS 查询卡住整个 TLS 握手。
验证是否真正生效
不能只看配置重载成功,要确认客户端实际收到了 stapled 响应:
- 终端执行:
openssl s_client -connect example.com:443 -status -servername example.com 2>&1 | grep -A 2 "OCSP response",成功时应显示OCSP response: successful (0x0)和CertStatus: good - 检查 Nginx 错误日志,搜索关键词:
ocsp、no resolver defined、verify failed、timeout - 用 SSL Labs 测试页,查看结果中 “OCSP stapling” 是否显示 Yes
进阶:实现零延迟缓存(推荐生产使用)
原生 stapling 依赖运行时异步拉取,存在首访延迟和被动更新窗口。真正稳定的方案是预载 DER 格式响应文件:
- 用 OpenSSL 离线生成:
openssl ocsp -no_nonce -respout example.com.ocsp.resp -issuer intermediate.crt -cert example.com.crt -url http://ocsp.int-x3.letsencrypt.org/ - 在 server 块中添加:
ssl_stapling_file /var/lib/nginx/ocsp/example.com.ocsp.resp; - 写定时脚本每天刷新响应(注意原子替换 + 双重校验:grep ": good" +
openssl ocsp -verify_other),配合 cron 或 systemd timer 执行
这样 Nginx 启动即加载有效响应,握手全程零延迟,且不受网络抖动或 DNS 故障影响。


















