温馨提示×

Nginx在Debian上的SSL证书配置步骤

小樊
42
2025-12-06 01:11:30
栏目: 云计算

在 Debian 上为 Nginx 配置 SSL 证书

一 准备与安装

  • 更新系统并安装必要软件与 Nginx:
    • sudo apt update
    • sudo apt install -y nginx ca-certificates
  • 安装 Certbot(两种常用方式,二选一):
    • 方式 A(Debian 仓库插件,自动修改 Nginx 配置):sudo apt install -y certbot python3-certbot-nginx
    • 方式 B(Snap 方式,通用性强):sudo apt install -y snapd && sudo snap install core && sudo snap refresh core && sudo ln -sfn /snap/bin/certbot /usr/bin/certbot
  • 防火墙放行(如使用 UFW):sudo ufw allow 80,443/tcp;如使用 firewalld:sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload。

二 获取 SSL 证书

  • 自动模式(推荐,Certbot 自动配置 Nginx 并重启):sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  • 手动模式(Webroot 验证,适合已有 Nginx 配置):
    • 在 Nginx 的 80 端口 server 中放行 ACME 验证目录:
      • location /.well-known/acme-challenge/ { root /var/www/certbot; }
    • 获取证书:sudo certbot certonly --webroot -w /var/www/certbot -d yourdomain.com -d www.yourdomain.com
  • 泛域名证书(DNS 验证,适合多子域):sudo certbot certonly --manual --preferred-challenges dns -d *.yourdomain.com
    • 按提示为 _acme-challenge 添加对应的 TXT 记录,等待生效后再继续。

三 Nginx SSL 配置示例

  • 推荐的 HTTP→HTTPS 跳转与 443 配置(证书路径为 Let’s Encrypt 默认路径):
    • 80 端口:
      • server { listen 80; server_name yourdomain.com www.yourdomain.com; location /.well-known/acme-challenge/ { root /var/www/certbot; } location / { return 301 https://$host$request_uri; } }
    • 443 端口:
      • server { 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;
        • include /etc/letsencrypt/options-ssl-nginx.conf;
        • ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
        • root /var/www/html; index index.html;
        • location / { try_files $uri $uri/ =404; }
      • }
  • 手动证书路径示例(非 Let’s Encrypt 时):
    • ssl_certificate /etc/ssl/certs/yourdomain.com.crt;
    • ssl_certificate_key /etc/ssl/private/yourdomain.com.key;
    • ssl_trusted_certificate /etc/ssl/certs/chain.pem;(如有中间证书)
  • 启用站点并校验重载:
    • sudo ln -sfn /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
    • sudo nginx -t && sudo systemctl reload nginx

四 自动续期与验证

  • 测试续期:sudo certbot renew –dry-run
  • 启用自动续期(两种方式,二选一):
    • 使用 systemd 定时器:sudo systemctl enable --now certbot.timer(推荐,Certbot 自带)
    • 使用 cron:sudo crontab -e 添加 0 0,12 * * * certbot renew --quiet && systemctl reload nginx
  • 验证结果:
    • 访问 https://yourdomain.com 检查锁标识与证书有效期
    • 命令行:echo | openssl s_client -connect yourdomain.com:443 -servername yourdomain.com 2>/dev/null | openssl x509 -noout -dates -subject
    • 在线检测:SSL Labs SSL Server Test(ssllabs.com/ssltest)。

五 常见问题与排查

  • 权限与路径:确保 /var/www/certbot 可被 Nginx 读取(如 www-data 可访问),证书文件权限为 600/644 且属主为 root
  • 配置校验与重载:每次修改后先执行 sudo nginx -t,再 sudo systemctl reload nginx,避免语法错误导致中断。
  • 端口与连通性:确认 80/443 已放行且域名解析指向服务器;云厂商安全组/本机防火墙需同步放行。
  • ACME 验证失败:检查 Webroot 路径是否一致、DNS 记录是否生效(泛域名)、是否存在 CDN/反代拦截 /.well-known/acme-challenge/
  • 续期失败排查:查看 /var/log/letsencrypt/ 日志,先解决域名可达性与权限问题,再重试 --dry-run。

0