插件窝 干货文章 Nginx反向代理SSL配置,加密网站数据传输

Nginx反向代理SSL配置,加密网站数据传输

SSL 配置 header Nginx 1026    来源:    2025-04-16

Nginx反向代理SSL配置指南

基本SSL配置

要为Nginx配置SSL反向代理,您需要以下基本配置:

server {
    listen 443 ssl;
    server_name yourdomain.com;

    # SSL证书配置
    ssl_certificate /path/to/your/certificate.crt;
    ssl_certificate_key /path/to/your/private.key;

    # SSL协议配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';

    # 反向代理配置
    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

高级安全配置

1. 启用HSTS (HTTP Strict Transport Security)

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

2. OCSP Stapling配置

ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/trusted_ca_cert.crt;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;

3. 安全头部配置

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
add_header Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval'";

完整示例配置

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    # SSL证书配置
    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    # SSL优化配置
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_session_tickets off;

    # 协议和密码套件
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';
    ssl_prefer_server_ciphers on;

    # 安全头部
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
    add_header X-XSS-Protection "1; mode=block";

    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    # 反向代理配置
    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    # 静态文件缓存
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
    }
}

# HTTP重定向到HTTPS
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

证书获取方式

  1. 自签名证书(仅用于测试):

    openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    -keyout /etc/ssl/private/nginx-selfsigned.key \
    -out /etc/ssl/certs/nginx-selfsigned.crt
    
  2. Let's Encrypt免费证书(推荐生产使用):

    sudo apt install certbot python3-certbot-nginx
    sudo certbot --nginx -d example.com -d www.example.com
    

配置验证与测试

  1. 测试Nginx配置语法:

    sudo nginx -t
    
  2. 重新加载Nginx配置:

    sudo systemctl reload nginx
    
  3. 使用SSL Labs测试SSL配置: https://www.ssllabs.com/ssltest/

  4. 检查HTTP重定向:

    curl -I http://example.com
    

常见问题解决

  1. 证书链不完整

    • 确保中间证书已包含在证书文件中
    • 可以使用cat intermediate.crt >> your_certificate.crt合并证书
  2. SSL协议或密码套件问题

    • 使用openssl s_client -connect example.com:443测试连接
    • 确保没有使用不安全的协议如SSLv3
  3. OCSP Stapling验证失败

    • 检查解析器配置
    • 确保证书链完整
  4. 性能问题

    • 启用SSL会话缓存
    • 考虑使用TLS 1.3以获得更好性能

通过以上配置,您可以建立一个安全、高效的Nginx反向代理SSL配置,确保网站数据传输的安全性。