参考资料

  1. 检测Nginx配置文件 nginx -t
  2. nginx -g详细说明以及案例
  3. nginx 配置ssl证书
  4. Nginx进程配置指令详解
  5. 如何设置nginx高级安全配置
  6. Nginx教程,一看就懂,一学就会
  7. Nginx负载均衡策略详解
  8. 如何通过响应头防御XSS?
  1. 获取SSL证书

  • 从CA机构购买或使用Let's Encrypt免费证书

  • 证书通常包含:证书文件(.crt)和私钥文件(.key)

  1. 配置Nginx
    在nginx.conf或站点配置文件中添加:

示例配置:

server {
    listen 443 ssl;
    server_name example.com;
    
    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/private.key;
    
    # 安全协议配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384';
    ssl_prefer_server_ciphers on;
    
    # 其他配置...
}
  1. 强制HTTPS重定向(可选)

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}
  1. 验证配置并重启

nginx -t  # 测试配置
systemctl restart nginx
  1. 安全增强建议

  • 启用HSTS头:add_header Strict-Transport-Security "max-age=63072000"

  • 定期更新SSL证书

  • 禁用旧版TLS协议(TLS 1.0/1.1)