温馨提示×

Debian怎么优化Nginx SSL

小樊
40
2026-08-18 05:34:43
栏目: 云计算

下面给你一套在 Debian 上优化 Nginx SSL 的实用方案,从 协议、证书、加密套件、性能、安全、HTTP/2、HSTS 等方面系统说明,适合生产环境。


一、系统 & 软件准备

1. 使用官方 Nginx(推荐)

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 支持更好


二、SSL 证书(推荐 Let’s Encrypt)

安装 Certbot

sudo apt install certbot python3-certbot-nginx

申请证书

sudo certbot --nginx -d example.com -d www.example.com

自动配置 SSL,并支持自动续期:

sudo systemctl status certbot.timer

三、Nginx SSL 核心优化配置

示例:/etc/nginx/conf.d/ssl.conf

ssl_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;

说明

  • TLS 1.3:更快、更安全
  • 关闭 ssl_session_tickets:避免会话票据安全问题
  • 开启 OCSP Stapling:减少证书验证延迟

四、HTTP/2 与性能优化

启用 HTTP/2(必须配合 SSL)

listen 443 ssl http2;
listen [::]:443 ssl http2;

开启 Gzip / Brotli(可选)

gzip on;
gzip_types text/plain text/css application/json application/javascript;

Brotli(性能更好,但需编译):

apt install nginx-module-brotli

五、安全增强(强烈推荐)

1. HSTS(强制 HTTPS)

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

2. 关闭服务器信息泄露

server_tokens off;

3. 防止点击劫持

add_header X-Frame-Options SAMEORIGIN;

4. 防止 MIME 嗅探

add_header X-Content-Type-Options nosniff;

六、SSL 证书文件权限(非常重要)

chmod 600 /etc/letsencrypt/live/example.com/privkey.pem
chown root:root /etc/letsencrypt/live/example.com/privkey.pem

七、调优系统参数(Debian)

1. 文件描述符

ulimit -n 65535

写入:

/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

2. TCP 优化

/etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
sysctl -p

八、测试 SSL 安全性

在线测试

  • https://www.ssllabs.com/ssltest/
  • 目标:A+

本地测试

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 示例

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;
    }
}

如果你愿意,我可以:

  • 按你的 Debian 版本(11 / 12)定制
  • 针对高并发 / 低延迟场景优化
  • 帮你做 A+ SSL Labs 配置
  • 排查现有 Nginx SSL 配置问题

直接贴你的 nginx -T 或配置文件即可。

0