下面给你一套在 Debian 上优化 Nginx SSL 的实用方案,从 协议、证书、加密套件、性能、安全、HTTP/2、HSTS 等方面系统说明,适合生产环境。
Debian 自带 Nginx 版本可能较旧,建议用官方源:
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
echo "deb https://nginx.org/packages/mainline/debian $(lsb_release -cs) nginx" | \
sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update
sudo apt install nginx
✅ mainline 版本对 TLS 1.3 支持更好
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
自动配置 SSL,并支持自动续期:
sudo systemctl status certbot.timer
/etc/nginx/conf.d/ssl.confssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:
ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:
ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 1.1.1.1 valid=300s;
resolver_timeout 5s;
✅ 说明
listen 443 ssl http2;
listen [::]:443 ssl http2;
gzip on;
gzip_types text/plain text/css application/json application/javascript;
Brotli(性能更好,但需编译):
apt install nginx-module-brotli
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
server_tokens off;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
chmod 600 /etc/letsencrypt/live/example.com/privkey.pem
chown root:root /etc/letsencrypt/live/example.com/privkey.pem
ulimit -n 65535
写入:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
/etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
sysctl -p
openssl s_client -connect example.com:443 -tls1_3
| 问题 | 解决 |
|---|---|
| SSL Labs 评分 B | 启用 TLS 1.3、关闭弱加密 |
| OCSP 失败 | 配置 resolver |
| Certbot 自动续期失败 | certbot renew --dry-run |
| HTTPS 访问慢 | 开启 HTTP/2 + session cache |
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/nginx/conf.d/ssl.conf;
add_header Strict-Transport-Security "max-age=31536000" always;
location / {
proxy_pass http://localhost:8080;
}
}
如果你愿意,我可以:
直接贴你的 nginx -T 或配置文件即可。