首页主机资讯Debian Nginx SSL与Let's Encrypt集成方法

Debian Nginx SSL与Let's Encrypt集成方法

时间2025-12-15 19:25:03发布访客分类主机资讯浏览662
导读:Debian 上 Nginx 与 Let’s Encrypt 集成指南 一 准备与安装 更新系统并安装 Nginx 与 Certbot Nginx 插件: sudo apt update sudo apt install nginx p...

Debian 上 Nginx 与 Let’s Encrypt 集成指南

一 准备与安装

  • 更新系统并安装 NginxCertbot Nginx 插件
    • sudo apt update
    • sudo apt install nginx python3-certbot-nginx
  • 确保域名已正确解析到服务器,且防火墙放行 HTTP/HTTPS(如使用 UFW:sudo ufw allow ‘Nginx Full’)。
  • 说明:Let’s Encrypt 证书有效期为 90 天,建议配置自动续期。

二 申请与部署证书

  • 自动模式(推荐):Certbot 自动修改 Nginx 配置并开启 HTTPS 重定向
    • 单域名:sudo certbot --nginx -d example.com
    • 多域名:sudo certbot --nginx -d example.com -d www.example.com
  • 手动模式(适用于特殊场景)
    • Webroot:sudo certbot certonly --webroot -w /var/www/html -d example.com
    • Standalone(需临时占用 80/443):sudo certbot certonly --standalone -d example.com
  • 证书路径与生效
    • 证书默认位于:/etc/letsencrypt/live/example.com/,常用文件为 fullchain.pem(证书链)与 privkey.pem(私钥)。
    • 验证与重载:sudo nginx -t & & sudo systemctl reload nginx。

三 Nginx 推荐 SSL 配置

  • 强制 HTTP→HTTPS 与基础 SSL
    • server { listen 80; server_name example.com; return 301 https://$host$request_uri; }
    • server { listen 443 ssl http2; server_name example.com;
      • ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
      • ssl_certificate_key /etc/letsencrypt/live/example.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:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
      • ssl_session_cache shared:SSL:10m; ssl_session_timeout 1d;
      • add_header Strict-Transport-Security “max-age=63072000; includeSubDomains; preload” always;
      • add_header X-Content-Type-Options nosniff; add_header X-Frame-Options DENY;
      • root /var/www/html; index index.html; }
  • 进阶优化(可选)
    • OCSP Stapling:ssl_stapling on; ssl_stapling_verify on; ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; resolver 8.8.8.8 8.8.4.4 valid=300s;
    • 会话票据:ssl_session_tickets on;
    • 安全增强:ssl_ecdh_curve secp384r1; 禁用不安全协议/套件。

四 自动续期与验证

  • 测试续期:sudo certbot renew --dry-run
  • 定时续期(生产建议)
    • 方式 A(系统 Cron):sudo crontab -e 添加 “0 3 * * * /usr/bin/certbot renew --quiet”,每天 03:00 检查并续期即将过期的证书。
    • 方式 B(若使用 Standalone):在续期前后启停服务,例如 “0 3 * * * /usr/bin/certbot renew --pre-hook ‘systemctl stop nginx’ --post-hook ‘systemctl start nginx’ --quiet”。
  • 续期成功后 Certbot 会自动重载 Nginx;如未生效,可手动执行:sudo systemctl reload nginx。
  • 验证与测试
    • 配置语法:sudo nginx -t
    • 在线检测:Qualys SSL Labs(ssllabs.com)评估安全等级
    • 握手与证书信息:openssl s_client -connect example.com:443 -servername example.com -tlsextdebug 2> & 1 | grep -i “certificate|protocol”。

五 常见问题与排查

  • 端口占用导致验证/续期失败:确保 80/443 可被 Certbot 使用;Standalone 模式需临时停止占用端口的服务。
  • Webroot 验证不可达:确认 /.well-known/acme-challenge/ 路径可访问且目录权限正确。
  • 混合内容:将所有 HTTP 资源改为 HTTPS,避免浏览器告警。
  • 配置错误:使用 nginx -t 检查语法,查看 /var/log/nginx/error.log 定位问题。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Debian Nginx SSL与Let's Encrypt集成方法
本文地址: https://pptw.com/jishu/771958.html
centos缓存策略怎么选 如何优化Debian Nginx SSL连接数

游客 回复需填写必要信息