首页主机资讯Ubuntu Nginx SSL如何优化

Ubuntu Nginx SSL如何优化

时间2026-01-20 15:58:04发布访客分类主机资讯浏览839
导读:Ubuntu 上 Nginx SSL/TLS 优化实操指南 一 证书与基础配置 使用 Let’s Encrypt 获取并自动配置证书(Certbot 会自动修改 Nginx 配置并开启 HTTPS): 安装:sudo apt updat...

Ubuntu 上 Nginx SSL/TLS 优化实操指南

一 证书与基础配置

  • 使用 Let’s Encrypt 获取并自动配置证书(Certbot 会自动修改 Nginx 配置并开启 HTTPS):
    • 安装:sudo apt update & & sudo apt install certbot python3-certbot-nginx
    • 签发:sudo certbot --nginx -d example.com [-d www.example.com]
    • 续期:sudo certbot renew --dry-run;生产建议定时任务(如每月):0 3 * * * /usr/bin/certbot renew --quiet
  • 强制 HTTP→HTTPS 跳转,并在 443 启用 HTTP/2
    • 示例:
      server {
          
        listen 80;
          
        server_name example.com;
          
        return 301 https://$host$request_uri;
      
      }
      
      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;
      
        # 其他 SSL 参数见下文
      }
          
      
  • 证书路径说明:Let’s Encrypt 的 fullchain.pem 已包含中间证书,通常无需额外链文件;如使用其他 CA,请确保证书链完整。

二 安全与性能关键参数

  • 协议与套件
    • 仅启用现代协议:ssl_protocols TLSv1.2 TLSv1.3;优先服务器套件:ssl_prefer_server_ciphers on
    • 推荐套件(兼顾安全与前向保密,TLS 1.3 使用内建套件):
      ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:
                   ECDHE-RSA-AES128-GCM-SHA256:
                   ECDHE-ECDSA-AES256-GCM-SHA384:
                   ECDHE-RSA-AES256-GCM-SHA384:
                   ECDHE-ECDSA-CHACHA20-POLY1305:
                   ECDHE-RSA-CHACHA20-POLY1305';
          
      
  • 会话复用
    • 共享会话缓存与会话票据(减少完整握手次数):
      ssl_session_cache shared:SSL:10m;
          
      ssl_session_timeout 1d;
          
      ssl_session_tickets on;
          
      
  • OCSP Stapling(提升握手性能并减轻 CA 查询)
    ssl_stapling on;
        
    ssl_stapling_verify on;
        
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        
    resolver 1.1.1.1 8.8.8.8 valid=300s;
        
    resolver_timeout 5s;
        
    
  • 安全响应头与信息泄露防护
    add_header Strict-Transport-Security "max-age=63072000;
         includeSubDomains;
         preload" always;
        
    add_header X-Frame-Options DENY;
        
    add_header X-Content-Type-Options nosniff;
        
    add_header X-XSS-Protection "1;
         mode=block";
        
    server_tokens off;
        
    
  • 密钥交换与 DH 参数
    • 生成更强的 DH 参数(仅一次):sudo openssl dhparam -out /etc/nginx/dhparam.pem 2048
    • 配置:ssl_dhparam /etc/nginx/dhparam.pem;
    • 椭圆曲线:ssl_ecdh_curve secp384r1

三 进阶与场景化优化

  • TLS 1.3 0-RTT(谨慎启用)
    • 优点:减少往返,提高首包速度;风险:重放攻击。建议仅对幂等等安全场景开启:ssl_early_data on; 并在应用层做好重放防护。
  • 反向代理与 WSS(WebSocket Secure)
    • 关键要点:proxy_http_version 1.1;Upgrade/Connection 头;长连接超时。
      location /wss/ {
          
        proxy_pass http://127.0.0.1:8080;
          
        proxy_http_version 1.1;
          
        proxy_set_header Upgrade $http_upgrade;
          
        proxy_set_header Connection "Upgrade";
          
        proxy_set_header Host $host;
          
        proxy_read_timeout 86400s;
          
        proxy_send_timeout 86400s;
      
      }
          
      
  • 硬件加速(可选)
    • 具备 PKCS#11/SSL 加速引擎 时可使用:ssl_engine on; (需按引擎文档配置设备与参数)。

四 验证与运维

  • 配置语法检查与热加载
    • 检查:sudo nginx -t
    • 生效:sudo systemctl reload nginx(避免重启造成短暂中断)
  • 在线评测与连通性
    • 安全与性能评分:Qualys SSL Labs Server Test(目标 A/A+
    • 握手与链验证:
      • openssl s_client -connect example.com:443 -servername example.com -tls1_3
      • 查看证书链:openssl s_client -connect example.com:443 -servername example.com -showcerts
  • 常见问题速查
    • 证书不受信任/链不完整:确保使用 fullchain.pem,或正确合并中间证书。
    • 握手失败/协议不匹配:确认仅启用 TLSv1.2/1.3,套件与客户端兼容。
    • 页面混合内容:全站资源改为 HTTPS 加载,避免浏览器告警与性能损失。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu Nginx SSL如何优化
本文地址: https://pptw.com/jishu/787348.html
Debian Golang编译如何进行代码优化 Debian Golang编译如何生成可执行文件

游客 回复需填写必要信息