CentOS配置SSL证书nginx

在 CentOS 上为 Nginx 配置 Let’s Encrypt SSL 证书(详细指南)

以下是针对 Nginx 的完整配置流程,使用 Certbot 工具自动化完成:


一、准备工作

  1. 系统要求

    • CentOS 7 或 8
    • Nginx 已安装并运行
    • 域名已解析到服务器 IP
  2. 安装必要组件

    1
    2
    3
    4
    5
    # 安装 EPEL 仓库
    sudo yum install epel-release

    # 安装 Certbot 和 Nginx 插件
    sudo yum install certbot python3-certbot-nginx
  3. 防火墙配置

    1
    2
    3
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload

二、配置 Nginx 虚拟主机

  1. 创建基础配置文件

    1
    sudo vim /etc/nginx/conf.d/yourdomain.conf
  2. 添加基本配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /usr/share/nginx/html;

    location / {
    try_files $uri $uri/ =404;
    }
    }
  3. 测试并重启 Nginx

    1
    2
    sudo nginx -t
    sudo systemctl restart nginx

三、获取 Let’s Encrypt 证书

  1. 运行 Certbot

    1
    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  2. 交互式配置

    1
    2
    3
    4
    - 输入邮箱(用于紧急通知)
    - 同意服务条款(A)
    - 是否分享邮箱(可选)
    - 选择重定向选项:推荐选2(强制HTTPS)
  3. 成功输出

    1
    2
    3
    Successfully received certificate.
    Certificate is saved at: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    Key is saved at: /etc/letsencrypt/live/yourdomain.com/privkey.pem

四、验证 Certbot 自动配置

  1. 查看 Nginx 配置变化

    1
    sudo vim /etc/nginx/conf.d/yourdomain.conf
  2. Certbot 添加的 SSL 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # ...其他配置...
    }

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

五、手动配置 SSL(可选高级设置)

  1. 优化 SSL 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    server {
    listen 443 ssl http2;
    server_name 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_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';

    # 会话设置
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # HSTS 增强安全
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    # ...其他配置...
    }
  2. 重启 Nginx

    1
    sudo nginx -t && sudo systemctl restart nginx

六、证书自动续订

  1. 测试续订功能

    1
    sudo certbot renew --dry-run
  2. 设置定时任务

    1
    2
    3
    4
    5
    6
    # 创建每日检查的定时任务
    echo "0 0 * * * /usr/bin/certbot renew --quiet" | sudo tee -a /etc/crontab > /dev/null

    # 或者使用 systemd 定时器
    sudo systemctl enable certbot-renew.timer
    sudo systemctl start certbot-renew.timer
  3. 续订后自动重载 Nginx

    1
    sudo vim /etc/letsencrypt/renewal-hooks/post/reload-nginx.sh

    添加内容:

    1
    2
    #!/bin/bash
    systemctl reload nginx

    设置权限:

    1
    sudo chmod +x /etc/letsencrypt/renewal-hooks/post/reload-nginx.sh

七、验证与测试

  1. 在线检测工具

  2. 命令行验证

    1
    2
    3
    4
    5
    6
    7
    8
    # 检查证书信息
    sudo openssl x509 -in /etc/letsencrypt/live/yourdomain.com/cert.pem -text -noout

    # 检查Nginx配置
    sudo nginx -t

    # 检查443端口监听
    sudo netstat -tuln | grep 443

八、故障排除

  1. 常见错误

    • Certbot 验证失败

      • 确保域名解析正确
      • 检查防火墙是否开放80端口
        1
        sudo firewall-cmd --list-ports
    • Nginx 启动失败

      • 检查证书路径权限:
        1
        sudo chmod 755 /etc/letsencrypt/{live,archive}
    • 混合内容警告

      • 确保网页中所有资源使用 HTTPS
  2. 日志检查

    1
    2
    3
    4
    5
    # Certbot 日志
    sudo tail -f /var/log/letsencrypt/letsencrypt.log

    # Nginx 错误日志
    sudo tail -f /var/log/nginx/error.log

最终效果

1
2
3
4
5
6
7
8
9
graph LR
A[用户访问] --> B{HTTP请求?}
B -->|是| C[Nginx 80端口]
C --> D[301重定向到HTTPS]
B -->|否| E[Nginx 443端口]
E --> F[SSL/TLS握手]
F --> G[使用Let's Encrypt证书]
G --> H[安全连接建立]
H --> I[提供网站内容]

通过以上步骤,你的 Nginx 服务器将:

  1. 自动重定向 HTTP → HTTPS
  2. 使用 Let’s Encrypt 的免费 SSL 证书
  3. 每90天自动续订证书
  4. 启用最佳安全实践配置

建议每半年检查一次 SSL 配置,使用在线工具验证安全性设置是否保持最新。