Docker 中自动配置NGINX HTTPS 和反向代理

使用 Docker Compose 部署 NGINX 服务,并自动配置 HTTPS 和反向代理的示例

发布于  星期一,十月 7 2024

docker-compose.yaml 示例

# 文档参考:
# 1. https://github.com/nginx-proxy/acme-companion/blob/main/docs/Docker-Compose.md
# 2. https://github.com/nginx-proxy/nginx-proxy/tree/main/docs
# 3. https://hub.docker.com/r/nginxproxy/acme-companion
# 4. https://github.com/nginx-proxy/acme-companion/blob/main/docs/Let's-Encrypt-and-ACME.md
services:
  nginx-proxy:
    container_name: nginx-proxy
    image: nginxproxy/nginx-proxy:1.6-alpine
    restart: unless-stopped
    network_mode: "host"
    environment:
      TZ: Asia/Shanghai
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - certs:/etc/nginx/certs
      - vhost:/etc/nginx/vhost.d
      - conf:/etc/nginx/conf.d
      - html:/usr/share/nginx/html

  acme-companion:
    container_name: nginx-proxy-acme
    image: nginxproxy/acme-companion:2.5
    restart: unless-stopped
    network_mode: "host"
    volumes_from:
      - nginx-proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - acme:/etc/acme.sh
    environment:
      TZ: Asia/Shanghai
#      VIRTUAL_HOST: fuxiaochen.com
#      VIRTUAL_PORT: 3000
      VIRTUAL_HOST_MULTIPORTS: |-
        fuxiaochen.com:
          "/":
            port: 3000
        node1.fuxiaochen.com:
          "/":
            port: 1234
        node2.fuxiaochen.com:
          "/":
            port: 4567
      # 配置域名: fuxiaochen.com node1.fuxiaochen.com node2.fuxiaochen.com
      LETSENCRYPT_HOST: fuxiaochen.com,node1.fuxiaochen.com,node2.fuxiaochen.com
      LETSENCRYPT_EMAIL: aifuxi.js@gmail.com
      # 每个域名都有单独的证书
      LETSENCRYPT_SINGLE_DOMAIN_CERTS: true

volumes:
  vhost:
  conf:
  html:
  certs:
  acme:
  • NGINX
  • Docker