Nginx 和 Apache 的最佳 TLS 配置
协议
| 协议 | 建议 |
|---|---|
| SSL 2.0 / 3.0 | 禁用,已不安全 |
| TLS 1.0 / 1.1 | 禁用,已过时且被现代浏览器拒绝 |
| TLS 1.2 | 启用,兼容性广 |
| TLS 1.3 | 启用,默认更强、更快更简单 |
Nginx TLS 配置
server {
listen 443 ssl;
http2 on;
server_name example.com;
ssl_certificate /etc/ssl/example.com/fullchain.pem;
ssl_certificate_key /etc/ssl/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
add_header Strict-Transport-Security "max-age=31536000" always;
}
这里故意省略了装订相关的行。Let's Encrypt 已于 2025 年 5 月 7 日从其证书中移除 OCSP 地址(Let's Encrypt 的 OCSP 公告),所以没有东西可装订。如果你的 CA 仍列出 OCSP 响应器,就加上 ssl_stapling on;, ssl_stapling_verify on; 以及一行 resolver 。指令 http2 on; 需要较新的 Nginx。在旧版本上,把 http2 放在 ssl 的 listen 行之后。
Apache TLS 配置
SSLEngine on
SSLCertificateFile /etc/ssl/example.com/cert.pem
SSLCertificateKeyFile /etc/ssl/example.com/privkey.pem
SSLCertificateChainFile /etc/ssl/example.com/chain.pem
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder off
Header always set Strict-Transport-Security "max-age=31536000"
如果你的 CA 仍列出 OCSP 响应器,可以加上 SSLUseStapling on,这还需要在主服务器配置中加一行 SSLStaplingCache 。Let's Encrypt 证书不需要它。
加密套件
TLS 1.3 会自己选择安全的套件。对 TLS 1.2,优先使用 ECDHE 密钥交换配合 AES-GCM 或 ChaCha20-Poly1305,避免 RC4、3DES 和静态 RSA 密钥交换。使用仍在维护的配置生成器,或你发行版当前的默认值,这比照抄旧博客片段要好。
证书与证书链
- 发送叶子证书和中间证书,不要发送根证书。
- RSA 2048 或 ECDSA P-256 是标准选择。
- 在剩余有效期约三分之一时自动续期。参见 Certbot 自动化 以及即将到来的 47 天有效期路线图.
需要证书?在这里免费签发: TryFreeSSL.com.
测试
- 运行 TLS Checker 查看服务器接受哪些协议版本和加密套件。
- 运行 SSL Checker 检查证书和证书链。
- 运行 Security Headers 检查 HSTS 等项目。
- 用以下命令测试
openssl s_client -connect example.com:443 -tls1_2以确认某个特定版本。
延伸阅读: 在 HAProxy 上配置 SSL.
常见问题
应该禁用 TLS 1.2 吗?
公开站点暂时不要。一些客户端仍需要它。在受控环境中只开 TLS 1.3 是合理的。
需要 HSTS preload 吗?
不需要。它是可选的且难以撤销,所以只在所有子域名都是 HTTPS 时使用。
OCSP 装订有什么作用?
服务器把最新的吊销状态附加到握手过程中,客户端就不必去查询 CA。它只对列出了 OCSP 响应器的证书有效。Let's Encrypt 于 2025 年 5 月 7 日移除了 OCSP 地址,并于 2025 年 8 月 6 日关闭了其响应器(Let's Encrypt 的 OCSP 公告)。参见 什么是 OCSP 装订.
严格设置会让老设备出问题吗?
非常旧的系统可能失败。在放弃真实用户仍在依赖的东西之前,先看你的分析数据。
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.