docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > Docker Nginx自动化部署HTTPS

Docker Compose+Nginx+Certbot自动化部署HTTPS的详细指南

作者:码上有潜

打造属于您的全自动 HTTPS 服务,无需复杂配置,无需手动维护,只需 3 个核心文件和 5 分钟时间,即可实现永久自动化的 HTTPS 加密解决方案,下面我们就来看看具体方法吧

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

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

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

/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.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;
    
    # 你的应用配置 (如果是已有的网站这里可配置代理跳转)
    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小时检查续期
      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.首次启动

2.自动续期

请求流程

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" ]; 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'

测试环境使用

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

结语:极简 HTTPS 自动化

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

立即部署

最佳实践

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

到此这篇关于Docker Compose+Nginx+Certbot自动化部署HTTPS的详细指南的文章就介绍到这了,更多相关Docker Nginx自动化部署HTTPS内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文