插件窝 干货文章 nginx如何配置ssl

nginx如何配置ssl

yourdomain SSL 配置 Nginx 177    来源:    2025-04-27

Nginx SSL 配置指南

配置 Nginx 使用 SSL/TLS 加密可以保护您的网站数据传输安全。以下是详细的 SSL 配置步骤:

1. 获取 SSL 证书

首先,您需要获取 SSL 证书,可以选择:

  • 商业证书:从证书颁发机构(CA)如 DigiCert、GlobalSign 等购买
  • 免费证书:使用 Let's Encrypt (推荐)

使用 Let's Encrypt 获取免费证书

# 安装 certbot
sudo apt-get install certbot python3-certbot-nginx

# 获取证书 (替换 yourdomain.com)
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

2. 基本 SSL 配置

在 Nginx 配置文件中(通常在 /etc/nginx/sites-available/yourdomain.com),添加以下 SSL 配置:

server {
    listen 443 ssl;
    server_name yourdomain.com www.yourdomain.com;

    # 证书路径
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    # SSL 协议配置
    ssl_protocols TLSv1.2 TLSv1.3;

    # SSL 加密套件
    ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';

    # 其他配置...

    location / {
        # 您的网站配置
    }
}

3. 强化 SSL 安全性

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

# DH 参数 (增强安全性)
ssl_dhparam /etc/nginx/dhparam.pem;

# 生成 DH 参数文件
sudo openssl dhparam -out /etc/nginx/dhparam.pem 2048

4. HTTP 重定向到 HTTPS

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$host$request_uri;
}

5. 自动续订 Let's Encrypt 证书

Let's Encrypt 证书有效期为 90 天,设置自动续订:

# 测试续订
sudo certbot renew --dry-run

# 设置定时任务 (编辑 crontab)
sudo crontab -e
# 添加以下内容 (每天凌晨检查续订)
0 0 * * * /usr/bin/certbot renew --quiet

6. 测试 SSL 配置

使用以下工具测试您的 SSL 配置: - SSL Labs Test - 命令行测试: bash nginx -t # 测试配置语法 sudo systemctl restart nginx # 重启生效 openssl s_client -connect yourdomain.com:443 -servername yourdomain.com

完整示例配置

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name yourdomain.com www.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
    ssl_prefer_server_ciphers on;

    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 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    ssl_dhparam /etc/nginx/dhparam.pem;

    # HSTS (强制 HTTPS)
    add_header Strict-Transport-Security "max-age=63072000" always;

    # 其他安全头
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    # 网站内容配置
    root /var/www/yourdomain.com/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$server_name$request_uri;
}

通过以上配置,您的网站将获得 A+ 级别的 SSL 安全评级。