当前位置: 首页 > news >正文

极简 Docker Compose + Nginx + Certbot 自动化 HTTPS 部署指南

极简 Docker Compose + Nginx + Certbot 自动化 HTTPS 部署指南

简要:打造属于您的全自动 HTTPS 服务,无需复杂配置,无需手动维护,只需 3 个核心文件和 5 分钟时间,即可实现永久自动化的 HTTPS 加密解决方案(适用于个人项目、测试项目)。
标签:Docker, Nginx, HTTPS, Certbot, 自动化

引言:三文件搞定 HTTPS 自动化部署

本文将展示如何仅用三个配置文件和 Docker Compose 实现:

  1. 全自动 HTTPS 证书管理
  2. HTTP 自动重定向到 HTTPS
  3. 零干预证书续期
  4. 极简目录结构

无需复杂脚本,无需额外工具,只需以下结构:

/home/middleware/nginx/
├── conf.d/
│   ├── default.conf    # HTTP 处理
│   └── ssl.conf        # HTTPS 服务配置
├── nginx.conf          # 主配置
├── docker-compose.yml  # 服务编排
└── cert/               # 证书存储目录

1. 创建目录结构

mkdir -p /home/middleware/nginx/{conf.d,cert}
cd /home/middleware/nginx

2. 配置文件内容

2.1 nginx.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.2 conf.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;}# 其他所有请求重定向到 HTTPSlocation / {return 301 https://$host$request_uri;}
}

2.3 conf.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;# 你的应用配置 (如果是已有的网站这里可配置代理跳转)location / {root /usr/share/nginx/html;index index.html;}# 保留证书验证路径location /.well-known/acme-challenge/ {root /var/www/certbot;}
}

2.4 docker-compose.yml (服务编排)

version: '3.8'services:nginx:image: nginx:alpinecontainer_name: nginxports:- "80:80"- "443:443"volumes:- ./nginx.conf:/etc/nginx/nginx.conf- ./conf.d:/etc/nginx/conf.d- ./cert:/etc/letsencrypt- certbot_www:/var/www/certbotrestart: unless-stoppeddepends_on:- certbotcertbot:image: certbot/certbot:latestcontainer_name: certbotvolumes:- ./cert:/etc/letsencrypt- certbot_www:/var/www/certbotcommand: >sh -c '# 首次运行获取证书if [ ! -f "/etc/letsencrypt/live/example.com/fullchain.pem" ]; thencertbot certonly --webroot -w /var/www/certbot -d example.com -d www.example.com --email your-email@example.com --agree-tos --noninteractive;fi;# 每12小时检查续期while :; dosleep 12hcertbot renewdone'restart: unless-stoppedvolumes:certbot_www:

3. 部署流程

步骤1: 替换域名

将配置文件中的所有 example.com 替换为你的实际域名

步骤2: 启动服务

docker-compose up -d

步骤3: 验证部署

curl -I https://yourdomain.com
# 应返回 200 OK

4. 工作原理

证书生命周期管理

  1. 首次启动

    • Certbot 检测到没有证书
    • 自动通过 HTTP 验证获取证书
    • 证书保存在 ./cert 目录
  2. 自动续期

    • Certbot 每12小时检查证书
    • 到期前30天内自动续期
    • Nginx 自动使用新证书

请求流程

  1. HTTP 请求到达 80 端口
  2. 如果是证书验证请求 → 由 Certbot 处理
  3. 其他请求 → 重定向到 HTTPS
  4. HTTPS 请求使用有效证书提供服务

5. 常见问题解决

问题1: 首次启动证书获取失败

解决方案:重启服务

docker-compose down
docker-compose up -d

问题2: 需要更新配置

# 修改配置后
docker-compose down
docker-compose up -d --force-recreate

问题3: 检查证书状态

docker-compose exec nginx openssl x509 -in /etc/letsencrypt/live/yourdomain.com/fullchain.pem -noout -dates

6. 进阶调整

自定义证书参数

docker-compose.yml 中修改 Certbot 命令:

command: >sh -c 'if [ ! -f "/etc/letsencrypt/live/example.com/fullchain.pem" ]; thencertbot 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" ]; thencertbot 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'

测试环境使用

command: >sh -c 'if [ ! -f "/etc/letsencrypt/live/example.com/fullchain.pem" ]; thencertbot certonly --webroot -w /var/www/certbot -d example.com --staging; # 使用测试环境fi;while :; do sleep 12h; certbot renew; done'

结语:极简 HTTPS 自动化

通过这个方案,你获得了:

  • 极简配置:仅需三个核心文件
  • 全自动化:证书获取、续期零干预
  • 易于维护:所有配置集中管理
  • 资源高效:轻量级容器方案

立即部署

  1. 创建目录结构
  2. 复制配置文件
  3. 替换域名
  4. 运行 docker-compose up -d

最佳实践

  • 定期执行 docker-compose pull 更新镜像
  • 监控 ./cert 目录的证书文件
  • 每季度检查一次部署状态

这种极简但功能完整的 HTTPS 解决方案,完美平衡了易用性和功能性,适合大多数 Web 应用场景。

资源链接

  • Nginx 官方镜像
  • Certbot 文档
  • Let’s Encrypt 官方文档
  • SSL Labs 测试工具
  • 在线证书链检查器

如有任何问题,欢迎在评论区交流讨论!
作者涉猎范围广泛,有技术问题可有偿提供技术支持!

http://www.dtcms.com/a/267480.html

相关文章:

  • 深度学习4(浅层神经网络)
  • Python之--基本知识
  • 马来西亚华韵海外华侨联合会宣布李子昂先生荣升名誉理事
  • HarmonyOS学习2---工程目录UIAbility
  • mysql 图形化界面工具 DataGrip 安装与配置
  • 基于人体骨架动作识别的神经信息处理技术(8 ANUBIS数据集)
  • UI前端与数字孪生结合实践案例分享:智慧水利的水情监测与预警系统
  • 信号与槽的总结
  • spring加载外部properties文件属性时,读取到userName变量值和properties文件的值不一致
  • 每日学习问题记录
  • 四、jenkins自动构建和设置邮箱
  • Matplotlib 安装部署与版本兼容问题解决方案(pyCharm)
  • nginx部署发布Vite项目
  • H3C WA6322 AP版本升级
  • 2 大模型高效参数微调;prompt tunning
  • (LeetCode 每日一题) 1394. 找出数组中的幸运数 (哈希表)
  • Vue前端项目接收webSocket信息
  • uniapp 国密sm2加密
  • 国产数据库之达梦DM:破甲成蝶
  • php协程
  • 【内存】Linux 内核优化实战 - net.ipv4.tcp_tw_reuse
  • Spring boot之身份验证和访问控制
  • FreeCAD傻瓜教程-拉簧拉力弹簧的画法及草图的附着位置设定和Part工作台中形体构建器的妙用
  • C#扩展方法全解析:给现有类型插上翅膀的魔法
  • spring中 方法上@Transation实现原理
  • Flink-Source算子状态恢复分析
  • 机器视觉对位中的常见模型与技术原理
  • HTML网页应用打包Android App 完整实践指南
  • 【Project】基于kafka的高可用分布式日志监控与告警系统
  • openstack安装并初始化