首页主机资讯nginx ssl centos安全设置指南

nginx ssl centos安全设置指南

时间2025-12-06 01:52:04发布访客分类主机资讯浏览1189
导读:Nginx SSL CentOS 安全设置指南 一 环境准备与基线加固 更新系统与安装组件:执行 sudo yum update -y,安装 EPEL 与 Nginx(sudo yum install -y epel-release ng...

Nginx SSL CentOS 安全设置指南

一 环境准备与基线加固

  • 更新系统与安装组件:执行 sudo yum update -y,安装 EPEL 与 Nginx(sudo yum install -y epel-release nginx),启动并设为开机自启(sudo systemctl start nginx & & sudo systemctl enable nginx)。
  • 防火墙仅放行必要端口:sudo firewall-cmd --permanent --zone=public --add-service=http;sudo firewall-cmd --permanent --zone=public --add-service=https;sudo firewall-cmd --reload。
  • 系统层防护:部署 Fail2ban 防暴力(sudo yum install -y fail2ban;sudo systemctl enable --now fail2ban),并按需配置 SSH 加固(禁用 root 远程登录、仅密钥登录)。
  • Nginx 基线:隐藏版本信息(在 http 块设置 server_tokens off; ),便于降低信息泄露面。

二 证书获取与部署

  • 证书类型选择:
    • DV(域名验证):适合个人/测试,验证快,通常有效期1年
    • OV(组织验证):适合企业官网,验证企业主体,通常1–2年
    • EV(扩展验证):适合金融/电商等高信誉场景,通常1–2年
  • 免费证书(Let’s Encrypt 与 Certbot):
    • 安装:sudo yum install -y certbot python3-certbot-nginx;
    • 自动获取并配置 Nginx:sudo certbot --nginx -d example.com -d www.example.com;
    • 生产可用“手动 DNS 挑战”方式:sudo certbot certonly --manual -d example.com --preferred-challenges dns。
  • 商业证书流程:生成 CSR(openssl req -new -newkey rsa:2048 -nodes -keyout /etc/ssl/private/example.com.key -out /etc/ssl/certs/example.com.csr),提交 CA 获取域名证书与中间证书;部署时建议使用合并链文件 fullchain.pem(cat domain.crt chain.crt > fullchain.pem),证书与私钥文件权限建议设为仅 root 可读(如 600/644)。

三 推荐的 Nginx SSL 配置

  • 建议将以下片段放入 /etc/nginx/conf.d/ssl.conf 或站点配置中,按需调整域名与路径:
server {
    
    listen 443 ssl http2;
    
    listen [::]:443 ssl http2;
    
    server_name example.com www.example.com;
    

    # 证书链与私钥(Let’s Encrypt 常用 fullchain.pem)
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    

    # 协议与套件:仅启用 TLS 1.2/1.3,优先 ECDHE,禁用不安全套件
    ssl_protocols TLSv1.2 TLSv1.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_prefer_server_ciphers on;
    

    # 会话恢复与性能
    ssl_session_cache shared:SSL:10m;
    
    ssl_session_timeout 1d;
    
    ssl_session_tickets on;
      # 如版本较老可关闭并配置 ticket.key

    # DH 参数(2048 位或更高)
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    

    # OCSP Stapling
    ssl_stapling on;
    
    ssl_stapling_verify on;
    
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    
    resolver_timeout 5s;
    

    # 安全响应头
    add_header Strict-Transport-Security "max-age=63072000;
     includeSubDomains;
     preload" always;
    
    add_header X-Frame-Options DENY always;
    
    add_header X-Content-Type-Options nosniff always;
    
    add_header X-XSS-Protection "1;
     mode=block" always;
    
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    
    add_header Permissions-Policy "accelerometer=(), camera=(), microphone=()" always;
    
    add_header Content-Security-Policy "default-src 'self';
     script-src 'self';
     object-src 'none';
    " always;
    

    # 可选:TLS 1.3 0-RTT(仅在充分评估重放风险后启用)
    # ssl_early_data on;


    location / {
    
        root /var/www/html;
    
        index index.html;

    }

}


# HTTP 强制跳转 HTTPS
server {
    
    listen 80;
    
    server_name example.com www.example.com;
    
    return 301 https://$host$request_uri;

}
    
  • 生成 DH 参数(建议 2048 位或更高):sudo openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048。
  • 自签名证书注意:OCSP Stapling 对自签名证书不适用,Nginx 会给出警告并自动禁用该功能。

四 验证与运维

  • 配置语法与生效:sudo nginx -t & & sudo systemctl reload nginx;访问测试:curl -I https://example.com。
  • 证书与链验证:openssl s_client -connect example.com:443 -servername example.com -status;确保链完整且 stapling 正常。
  • 在线评测与监控:使用 SSL Labs 测试评级(目标 A+),并设置证书过期预警(建议提前30天);持续监控握手错误率与异常流量。
  • 续期与回滚:Let’s Encrypt 默认每90天续期,建议配置定时任务与自动重载;变更前备份配置与证书,回滚时快速恢复。

五 进阶与常见问题

  • 性能与安全平衡:启用 HTTP/2(需 Nginx ≥ 1.9.5、OpenSSL ≥ 1.0.2),合理设置 ssl_buffer_size(如 1400 字节),在明确重放风险后再启用 TLS 1.3 的 0-RTT。
  • 0-RTT 风险提示:可能引入重放攻击风险,仅在特定、低风险场景启用,并配合业务层幂等等保护。
  • 自签名证书限制:浏览器会告警且不支持 OCSP Stapling,仅适合测试或受控内网。
  • 安全头策略:HSTS 的 preload 一旦提交难以撤销,务必在充分测试后启用;必要时先使用不带 preload 的 HSTS。

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


若转载请注明出处: nginx ssl centos安全设置指南
本文地址: https://pptw.com/jishu/765413.html
nginx在centos上ssl优化技巧 nginx在centos上ssl安装步骤

游客 回复需填写必要信息