Nginx OCSP Stapling 缓存失效需从权限、路径、校验、更新四环节主动验证:确认缓存目录及文件属主/权限匹配worker进程(如www-data),路径准确无误;用openssl ocsp -respin校验响应文件有效性及有效期;检查cron更新脚本是否真正执行并原子替换;reload后查error_log中stapling相关错误,必要时启用debug日志定位。

排查 Nginx OCSP Stapling 响应缓存目录损坏或写入失败,关键不是等错误发生后再翻日志,而是从权限、路径、校验、更新链四个环节主动验证。问题常表现为 ssl_stapling 静默失效、error_log 中反复出现 ngx_ssl_stapling_cert_t 相关警告,或客户端 TLS 握手延迟明显回升。
检查缓存目录权限与归属
Nginx worker 进程必须能读取 ssl_stapling_file 指向的文件,且该文件所属用户/组需与 worker_processes 运行身份一致(通常是 www-data 或 nginx):
- 确认目录存在且路径拼写准确:
ls -ld /var/lib/nginx/ocsp,避免末尾多斜杠或大小写错误 - 检查属主和权限:
sudo ls -l /var/lib/nginx/ocsp/example.com.ocsp.resp,应为-rw-r--r-- 1 www-data www-data类似格式 - 若用 systemd 启动,注意
ProtectSystem=strict或ReadOnlyDirectories可能封锁写入路径,临时注释后测试
验证 OCSP 响应文件有效性
缓存文件即使存在,也可能已过期、签名无效或格式错误,Nginx 会静默跳过使用:
- 用 OpenSSL 手动校验:
openssl ocsp -respin /var/lib/nginx/ocsp/example.com.ocsp.resp -text -CAfile /etc/nginx/ssl/trusted-ocsp-ca.pem - 成功输出需含
Response verify OK和cert status: good;若报unable to get certificate from response或response is not valid,说明文件已损坏或过期 - 检查有效期:
openssl ocsp -respin example.com.ocsp.resp -text | grep -E "(Produced At|Next Update)",确认当前时间在Next Update范围内(Let’s Encrypt 通常为 7 天)
确认定时更新脚本是否真正执行并替换成功
多数故障源于更新脚本“看似运行”,实则未生效:
- 查看 cron 日志:
grep update-ocsp /var/log/syslog或journalctl -u cron | grep update-ocsp,确认任务有触发记录 - 检查脚本中是否用了原子替换(如
mv tmp.new example.com.ocsp.resp),避免写入中途被中断导致损坏 - 在脚本末尾加一行日志:
echo "$(date): updated $(stat -c '%y' /var/lib/nginx/ocsp/example.com.ocsp.resp)" >> /var/log/ocsp-update.log,比单纯看 cron 时间戳更可靠
观察 Nginx 启动与重载时的行为
损坏的响应文件会在 Nginx 加载配置时暴露问题:
- 执行
nginx -t不会报错,但nginx -s reload后立即查error_log,搜索stapling或ocsp,常见提示如:could not load OCSP response file、OCSP response has expired - 若日志无明确提示,可临时加
error_log /var/log/nginx/error.log debug;(仅调试),重启后搜索stapling_file相关 debug 行,看是否加载失败 - 对比启用
ssl_stapling_file前后nginx -V 2>&1 | grep -o 'with-openssl'输出,确保 OpenSSL 版本 ≥ 1.0.1(推荐 1.1.1+)


















