简介:本文详细讲解如何使用Let's Encrypt为泛域名申请SSL证书,并拆分Nginx配置实现高效管理,涵盖证书申请、配置拆分原则及自动化部署方案。
泛域名证书(Wildcard SSL)通过单个证书保护主域名及其所有子域名(如*.example.com),相比单域名证书具有显著优势:
Let’s Encrypt通过ACME协议实现自动化证书管理,Certbot作为主流客户端完成以下流程:
# 安装Certbot(以Ubuntu为例)sudo apt install certbot python3-certbot-dns-cloudflare# 配置Cloudflare API Token(其他DNS服务商类似)export CF_Token="您的API令牌"export CF_Email="邮箱地址"# 申请泛域名证书sudo certbot certonly \--dns-cloudflare \--dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \-d "*.example.com" \--server https://acme-v02.api.letsencrypt.org/directory
sudo certbot certonly --manual -d "*.example.com" \--agree-tos --no-eff-email \--manual-public-ip-logging-ok \--preferred-challenges http
需在.well-known/acme-challenge/目录下创建验证文件,验证通过后证书将保存在/etc/letsencrypt/live/example.com/目录。
单文件配置模式存在三大缺陷:
/etc/nginx/├── conf.d/ # 主配置文件├── sites-available/ # 业务站点配置├── sites-enabled/ # 启用站点链接└── snippets/ # 配置片段
| 拆分类型 | 适用场景 | 示例文件 | 
|---|---|---|
| 按业务拆分 | 多项目独立部署 | app1.conf, app2.conf | 
| 按功能拆分 | 静态资源/API接口分离 | static.conf, api.conf | 
| 按环境拆分 | 开发/测试/生产环境隔离 | dev.conf, prod.conf | 
| 按协议拆分 | HTTP/HTTPS/WebSocket分离 | http.conf, ws.conf | 
http {include /etc/nginx/mime.types;default_type application/octet-stream;# SSL全局配置ssl_protocols TLSv1.2 TLSv1.3;ssl_prefer_server_ciphers on;ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';# 加载业务配置include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;}
server {listen 443 ssl http2;server_name ~^(?<subdomain>.+)\.example\.com$;ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;# 根据子域名路由if ($subdomain = "api") {include /etc/nginx/snippets/api.conf;}location / {root /var/www/html/$subdomain;try_files $uri $uri/ =404;}}
# 创建续期脚本cat > /etc/letsencrypt/renew-hook.sh <<'EOF'#!/bin/bashsystemctl reload nginxEOFchmod +x /etc/letsencrypt/renew-hook.sh# 配置cron任务(crontab -l 2>/dev/null; echo "0 3 * * * /usr/bin/certbot renew --quiet --post-hook \"/etc/letsencrypt/renew-hook.sh\"") | crontab -
版本控制:将Nginx配置纳入Git管理
cd /etc/nginxgit initgit add .git commit -m "Initial Nginx configuration"
配置校验:修改前执行语法检查
sudo nginx -t
零宕机更新:使用nginx -s reload平滑加载配置
server {# ...其他配置...location / {http2_push_preload on;add_header Link '</css/style.css>; rel=preload; as=style';add_header Link '</js/app.js>; rel=preload; as=script';}}
对于大规模部署场景,可采用以下方案:
stream {map $ssl_preread_server_name $name {default example.com;~^(?<sub>.+)\.example\.com$ $sub.example.com;}server {listen 443 ssl;ssl_preread on;ssl_certificate /etc/letsencrypt/live/$name/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/$name/privkey.pem;proxy_pass backend;}}
DNS验证失败:
dig TXT _acme-challenge.example.comHTTP验证失败:
ls -la /var/www/.well-known/acme-challenge/
location /.well-known/acme-challenge/ {root /var/www/html;}
当出现duplicate upstream错误时:
include指令替代重复配置map指令实现动态变量分配server_nameSSL会话缓存:
ssl_session_cache shared10m;
ssl_session_timeout 10m;
OCSP Stapling:
ssl_stapling on;ssl_stapling_verify on;resolver 8.8.8.8 8.8.4.4 valid=300s;resolver_timeout 5s;
连接复用:
keepalive_timeout 75s;keepalive_requests 100;
通过Let’s Encrypt泛域名证书与模块化Nginx配置的结合,可实现:
未来发展方向包括: