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é)構(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 檢測到?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'
測試環(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'
結(jié)語:極簡 HTTPS 自動(dòng)化
通過這個(gè)方案,你獲得了:
- 極簡配置:僅需三個(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)
這種極簡但功能完整的 HTTPS 解決方案,完美平衡了易用性和功能性,適合大多數(shù) Web 應(yī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)文章希望大家以后多多支持腳本之家!
- docker部署nginx并實(shí)現(xiàn)https教程
- 如何在Docker環(huán)境下為Nginx配置HTTPS
- Nginx配置ssl支持https全過程(docker版)
- docker部署nginx并且實(shí)現(xiàn)https的方法步驟
- ubuntu服務(wù)器部署gitlab docker并配置nginx反向代理https訪問的過程解析
- docker nginx + https 子域名配置詳細(xì)教程
- 為docker中的nginx配置https的方法步驟
- docker安裝nginx并配置通過https訪問的方法
- Docker部署Nginx HTTPS服務(wù)的實(shí)現(xiàn)步驟
相關(guān)文章
docker部署golang http服務(wù)時(shí)端口無法訪問的問題解決
本文主要介紹了docker部署golang http服務(wù)時(shí)端口無法訪問的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
docker部署rabbitmq集群的實(shí)現(xiàn)方法
這篇文章主要介紹了docker部署rabbitmq集群的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
阿里云docker容器固定應(yīng)用到到某一個(gè)節(jié)點(diǎn)記錄
這篇文章主要介紹了阿里云docker容器固定應(yīng)用到到某一個(gè)節(jié)點(diǎn)記錄,需要的朋友可以參考下2018-05-05
Docker 教程之?dāng)?shù)據(jù)管理詳細(xì)介紹
這篇文章主要介紹了Docker 教程之?dāng)?shù)據(jù)管理詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-01-01
Docker 通過端口來連接一個(gè)容器的實(shí)現(xiàn)
這篇文章主要介紹了Docker 通過端口來連接一個(gè)容器的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11

