Docker 中的 Nginx 配置文件

Docker 中的 Nginx 配置文件

我有一台具有以下功能的服务器docker-compose.yml

version: "3"
services:
  frontend:
    restart: unless-stopped
    image: staticfloat/nginx-certbot
    ports:
      - 80:80/tcp
      - 443:443/tcp
    environment:
      CERTBOT_EMAIL: [email protected]
    volumes:
      - ./conf.d:/etc/nginx/user.conf.d:ro
      - letsencrypt:/etc/letsencrypt
  studio:
    image: bitnami/nginx:latest
    restart: always
    volumes: 
      - ./build:/app
      - ./default.conf:/opt/bitnami/nginx/conf/server_blocks/default.conf:ro
      - ./configs/config.prod.js:/app/lib/config.js
    depends_on: 
    - frontend

volumes:
  letsencrypt:

networks:
  default:
    external:
      name: studio

此外,还有一个conf.d/prod.conf用于 nginx 配置的文件。

我照做了docker exec -it frontend_frontend_1 /bin/bash,我可以看到有/etc/nginx/user.conf.d/prod.conf/etc/nginx/conf.d/prod.conf是相同的。我知道是/etc/nginx/user.conf.d/prod.conf因为- ./conf.d:/etc/nginx/user.conf.d:ro在而安装的docker-compose.yml,但是为什么会有呢/etc/nginx/conf.d/prod.conf

有人知道吗?

答案1

但是为什么有/etc/nginx/conf.d/prod.conf?

因为当你运行staticfloat/nginx-certbot图像时,它会运行脚本/scripts/entrypoint.sh,该脚本会运行此代码(以及其他代码):

template_user_configs() {
    SOURCE_DIR="${1-/etc/nginx/user.conf.d}"
    TARGET_DIR="${2-/etc/nginx/conf.d}"

    # envsubst needs dollar signs in front of all variable names
    DENV=$(echo ${ENVSUBST_VARS} | sed -E 's/\$*([^ ]+)/\$\1/g')

    echo "templating scripts from ${SOURCE_DIR} to ${TARGET_DIR}"
    echo "Substituting variables ${DENV}"

    if [ ! -d "$SOURCE_DIR" ]; then
        echo "no ${SOURCE_DIR}, nothing to do."
    else
        for conf in ${SOURCE_DIR}/*.conf; do
            echo " -> ${conf}"
            envsubst "${DENV}" <"${conf}" > "${TARGET_DIR}/$(basename ${conf})"
        done
    fi
}

在执行环境变量替换时将文件复制到/etc/nginx/user.conf.d其中。/etc/nginx/conf.d

相关内容