OCSP Stapling(OCSP装订)是一种优化SSL/TLS握手过程的机制,它可以显著提高HTTPS连接的性能和隐私性。以下是在Nginx反向代理中配置和优化OCSP Stapling的详细指南。
OCSP (Online Certificate Status Protocol) Stapling允许服务器在TLS握手期间提供证书吊销状态信息,而不需要客户端单独查询OCSP服务器。这带来以下优势:
在Nginx中启用OCSP Stapling的基本配置:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/private.key;
# 启用OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
# 指定DNS解析器用于OCSP验证
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# 信任链文件
ssl_trusted_certificate /path/to/trusted_ca_cert.pem;
}
ssl_stapling_file /path/to/ocsp_response.der;
你可以使用openssl
命令预获取OCSP响应并保存到文件:
openssl ocsp -issuer /path/to/ca.crt -cert /path/to/site.crt \
-url http://ocsp.example.com -respout /path/to/ocsp_response.der
然后设置cron任务定期更新此文件。
对于负载均衡环境,可以使用共享内存区域缓存OCSP响应:
ssl_stapling_cache shared:ocsp_cache:10m;
ssl_stapling_responder_timeout 5s;
ssl_stapling_error_timeout 2s;
http {
# 共享OCSP缓存
ssl_stapling_cache shared:ocsp_cache:10m;
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_trusted_certificate /etc/nginx/ssl/ca-bundle.crt;
# SSL优化配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384...';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
# OCSP Stapling配置
ssl_stapling on;
ssl_stapling_verify on;
ssl_stapling_responder_timeout 5s;
ssl_stapling_error_timeout 2s;
# 使用Google DNS解析OCSP
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# 其他配置...
}
}
openssl s_client -connect example.com:443 -status -servername example.com -tlsextdebug < /dev/null 2>&1 | grep -i "OCSP response"
如果看到"OCSP Response Status: successful"则表示配置成功。
可以使用以下在线工具验证: - SSL Labs SSL Test - Qualys SSL Server Test
错误日志:
OCSP_basic_verify() failed (SSL: error:27069065:OCSP routines:OCSP_basic_verify:certificate verify error) while requesting certificate status
解决方案:
- 确保ssl_trusted_certificate
包含完整的证书链
- 确保证书链顺序正确(服务器证书在前,中间证书在后)
定期更新OCSP响应,可以设置cron任务:
0 3 * * * openssl ocsp -issuer /path/to/ca.crt -cert /path/to/site.crt -url http://ocsp.example.com -respout /path/to/ocsp_response.der && nginx -s reload
如果OCSP服务器经常不可达,可以考虑: - 使用备用解析器 - 增加超时时间 - 考虑使用OCSP Stapling预加载
通过合理配置OCSP Stapling,可以显著提升HTTPS连接的性能和用户体验,同时增强安全性。