Docker?Compose+Nginx+Certbot自動(dòng)化部署HTTPS的詳細(xì)指南
引言:三文件搞定 HTTPS 自動(dòng)化部署
本文將展示如何僅用三個(gè)配置文件和 Docker Compose 實(shí)現(xiàn):
- 全自動(dòng) HTTPS 證書管理
- HTTP 自動(dòng)重定向到 HTTPS
- 零干預(yù)證書續(xù)期
- 極簡(jiǎn)目錄結(jié)構(gòu)
無需復(fù)雜腳本,無需額外工具,只需以下結(jié)構(gòu):
/home/middleware/nginx/
├── conf.d/
│ ├── default.conf # HTTP 處理
│ └── ssl.conf # HTTPS 服務(wù)配置
├── nginx.conf # 主配置
├── docker-compose.yml # 服務(wù)編排
└── cert/ # 證書存儲(chǔ)目錄
1. 創(chuàng)建目錄結(jié)構(gòu)
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 請(qǐng)求 server { listen 80; server_name example.com www.example.com; # Certbot 驗(yàn)證目錄 location /.well-known/acme-challenge/ { root /var/www/certbot; } # 其他所有請(qǐng)求重定向到 HTTPS location / { return 301 https://$host$request_uri; } }
2.3conf.d/ssl.conf(HTTPS 服務(wù))
# HTTPS 服務(wù)器 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; # 你的應(yīng)用配置 (如果是已有的網(wǎng)站這里可配置代理跳轉(zhuǎn)) location / { root /usr/share/nginx/html; index index.html; } # 保留證書驗(yàn)證路徑 location /.well-known/acme-challenge/ { root /var/www/certbot; } }
2.4docker-compose.yml(服務(wù)編排)
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 ' # 首次運(yùn)行獲取證書 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小時(shí)檢查續(xù)期 while :; do sleep 12h certbot renew done' restart: unless-stopped volumes: certbot_www:
3. 部署流程
步驟1: 替換域名
將配置文件中的所有 example.com
替換為你的實(shí)際域名
步驟2: 啟動(dòng)服務(wù)
docker-compose up -d
步驟3: 驗(yàn)證部署
curl -I https://yourdomain.com # 應(yīng)返回 200 OK
4. 工作原理
證書生命周期管理
1.首次啟動(dòng):
- Certbot 檢測(cè)到?jīng)]有證書
- 自動(dòng)通過 HTTP 驗(yàn)證獲取證書
- 證書保存在
./cert
目錄
2.自動(dòng)續(xù)期:
- Certbot 每12小時(shí)檢查證書
- 到期前30天內(nèi)自動(dòng)續(xù)期
- Nginx 自動(dòng)使用新證書
請(qǐng)求流程
- HTTP 請(qǐng)求到達(dá) 80 端口
- 如果是證書驗(yàn)證請(qǐng)求 → 由 Certbot 處理
- 其他請(qǐng)求 → 重定向到 HTTPS
- HTTPS 請(qǐng)求使用有效證書提供服務(wù)
5. 常見問題解決
問題1: 首次啟動(dòng)證書獲取失敗
解決方案:重啟服務(wù)
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. 進(jìn)階調(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'
測(cè)試環(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; # 使用測(cè)試環(huán)境 fi; while :; do sleep 12h; certbot renew; done'
結(jié)語:極簡(jiǎn) HTTPS 自動(dòng)化
通過這個(gè)方案,你獲得了:
- 極簡(jiǎn)配置:僅需三個(gè)核心文件
- 全自動(dòng)化:證書獲取、續(xù)期零干預(yù)
- 易于維護(hù):所有配置集中管理
- 資源高效:輕量級(jí)容器方案
立即部署:
- 創(chuàng)建目錄結(jié)構(gòu)
- 復(fù)制配置文件
- 替換域名
- 運(yùn)行
docker-compose up -d
最佳實(shí)踐:
- 定期執(zhí)行
docker-compose pull
更新鏡像 - 監(jiān)控
./cert
目錄的證書文件 - 每季度檢查一次部署狀態(tài)
這種極簡(jiǎn)但功能完整的 HTTPS 解決方案,完美平衡了易用性和功能性,適合大多數(shù) Web 應(yīng)用場(chǎng)景。
到此這篇關(guān)于Docker Compose+Nginx+Certbot自動(dòng)化部署HTTPS的詳細(xì)指南的文章就介紹到這了,更多相關(guān)Docker Nginx自動(dòng)化部署HTTPS內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Docker部署ELK7.3.0日志收集服務(wù)最佳實(shí)踐
這篇文章主要介紹了Docker部署ELK7.3.0日志收集服務(wù)最佳實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10三款Docker圖形化工具優(yōu)缺點(diǎn)對(duì)比
大家好,本篇文章主要講的是三款Docker圖形化工具優(yōu)缺點(diǎn)對(duì)比,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01k3d入門指南之在Docker中運(yùn)行K3s的詳細(xì)教程
在本文中,我們將簡(jiǎn)單了解k3d,這是一款可讓您在安裝了Docker的任何地方運(yùn)行一次性Kubernetes集群的工具,此外在本文中我們還將探討在使用k3d中可能會(huì)出現(xiàn)的一切問題,感興趣的朋友跟隨小編一起看看吧2021-05-05Docker部署nGrinder性能測(cè)試平臺(tái)過程解析
這篇文章主要介紹了Docker部署nGrinder性能測(cè)試平臺(tái)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11