Docker?Compose+Nginx+Certbot自動化部署HTTPS的詳細指南
引言:三文件搞定 HTTPS 自動化部署
本文將展示如何僅用三個配置文件和 Docker Compose 實現(xiàn):
- 全自動 HTTPS 證書管理
- HTTP 自動重定向到 HTTPS
- 零干預證書續(xù)期
- 極簡目錄結構
無需復雜腳本,無需額外工具,只需以下結構:
/home/middleware/nginx/
├── conf.d/
│ ├── default.conf # HTTP 處理
│ └── ssl.conf # HTTPS 服務配置
├── nginx.conf # 主配置
├── docker-compose.yml # 服務編排
└── cert/ # 證書存儲目錄
1. 創(chuàng)建目錄結構
mkdir -p /home/middleware/nginx/{conf.d,cert} cd /home/middleware/nginx
2. 配置文件內(nèi)容
2.1nginx.conf(主配置文件)
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; # 包含其他配置 include /etc/nginx/conf.d/*.conf; }
2.2conf.d/default.conf(HTTP 處理)
# 處理 HTTP 請求 server { listen 80; server_name example.com www.example.com; # Certbot 驗證目錄 location /.well-known/acme-challenge/ { root /var/www/certbot; } # 其他所有請求重定向到 HTTPS location / { return 301 https://$host$request_uri; } }
2.3conf.d/ssl.conf(HTTPS 服務)
# HTTPS 服務器 server { listen 443 ssl http2; server_name example.com www.example.com; # SSL 證書配置 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_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256'; ssl_session_cache shared:SSL:10m; ssl_session_timeout 1d; # 安全頭 add_header Strict-Transport-Security "max-age=63072000" always; # 你的應用配置 (如果是已有的網(wǎng)站這里可配置代理跳轉) location / { root /usr/share/nginx/html; index index.html; } # 保留證書驗證路徑 location /.well-known/acme-challenge/ { root /var/www/certbot; } }
2.4docker-compose.yml(服務編排)
version: '3.8' services: nginx: image: nginx:alpine container_name: nginx ports: - "80:80" - "443:443" volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./conf.d:/etc/nginx/conf.d - ./cert:/etc/letsencrypt - certbot_www:/var/www/certbot restart: unless-stopped depends_on: - certbot certbot: image: certbot/certbot:latest container_name: certbot volumes: - ./cert:/etc/letsencrypt - certbot_www:/var/www/certbot command: > sh -c ' # 首次運行獲取證書 if [ ! -f "/etc/letsencrypt/live/example.com/fullchain.pem" ]; then certbot certonly --webroot -w /var/www/certbot -d example.com -d www.example.com --email your-email@example.com --agree-tos --noninteractive; fi; # 每12小時檢查續(xù)期 while :; do sleep 12h certbot renew done' restart: unless-stopped volumes: certbot_www:
3. 部署流程
步驟1: 替換域名
將配置文件中的所有 example.com
替換為你的實際域名
步驟2: 啟動服務
docker-compose up -d
步驟3: 驗證部署
curl -I https://yourdomain.com # 應返回 200 OK
4. 工作原理
證書生命周期管理
1.首次啟動:
- Certbot 檢測到?jīng)]有證書
- 自動通過 HTTP 驗證獲取證書
- 證書保存在
./cert
目錄
2.自動續(xù)期:
- Certbot 每12小時檢查證書
- 到期前30天內(nèi)自動續(xù)期
- Nginx 自動使用新證書
請求流程
- HTTP 請求到達 80 端口
- 如果是證書驗證請求 → 由 Certbot 處理
- 其他請求 → 重定向到 HTTPS
- HTTPS 請求使用有效證書提供服務
5. 常見問題解決
問題1: 首次啟動證書獲取失敗
解決方案:重啟服務
docker-compose down docker-compose up -d
問題2: 需要更新配置
# 修改配置后 docker-compose down docker-compose up -d --force-recreate
問題3: 檢查證書狀態(tài)
docker-compose exec nginx openssl x509 -in /etc/letsencrypt/live/yourdomain.com/fullchain.pem -noout -dates
6. 進階調(diào)整
自定義證書參數(shù)
在 docker-compose.yml
中修改 Certbot 命令:
command: > sh -c ' if [ ! -f "/etc/letsencrypt/live/example.com/fullchain.pem" ]; then certbot certonly --webroot -w /var/www/certbot -d example.com --email your-email@example.com --agree-tos --noninteractive --rsa-key-size 4096; # 密鑰大小 fi; while :; do sleep 12h; certbot renew; done'
多域名支持
command: > sh -c ' if [ ! -f "/etc/letsencrypt/live/example.com/fullchain.pem" ]; then certbot certonly --webroot -w /var/www/certbot -d example.com -d www.example.com -d api.example.com; # 添加更多域名 fi; while :; do sleep 12h; certbot renew; done'
測試環(huán)境使用
command: > sh -c ' if [ ! -f "/etc/letsencrypt/live/example.com/fullchain.pem" ]; then certbot certonly --webroot -w /var/www/certbot -d example.com --staging; # 使用測試環(huán)境 fi; while :; do sleep 12h; certbot renew; done'
結語:極簡 HTTPS 自動化
通過這個方案,你獲得了:
- 極簡配置:僅需三個核心文件
- 全自動化:證書獲取、續(xù)期零干預
- 易于維護:所有配置集中管理
- 資源高效:輕量級容器方案
立即部署:
- 創(chuàng)建目錄結構
- 復制配置文件
- 替換域名
- 運行
docker-compose up -d
最佳實踐:
- 定期執(zhí)行
docker-compose pull
更新鏡像 - 監(jiān)控
./cert
目錄的證書文件 - 每季度檢查一次部署狀態(tài)
這種極簡但功能完整的 HTTPS 解決方案,完美平衡了易用性和功能性,適合大多數(shù) Web 應用場景。
到此這篇關于Docker Compose+Nginx+Certbot自動化部署HTTPS的詳細指南的文章就介紹到這了,更多相關Docker Nginx自動化部署HTTPS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!