2

我使用 NGINX 作为反向代理。

我有 3 个环境(开发、QA、生产)

考虑一下,develop 的 IP 地址是 1.2.3.4,qa 是 4.3.2.1,production 是 3.4.1.2

我已经如下配置了 nginx.conf 文件,并且在开发环境中它工作得很好。

在构建这些 docker-image 时,我已经明确提到应该在哪个配置上构建图像,如下所示

cd conf/clustered-develop/;sudo docker build -t jcibts-swmdtr-dev.jci.com/nginx:1 --build-arg PORT=8765 --build-arg ENVIRONMENT=clustered-develop .

要求是 docker-image 应该只构建 1,然后它将被推送到 Docker 受信任的存储库。

它将被提升到其他环境的 docker 可信存储库,而无需再次构建镜像。

我的问题是我能做些什么来为所有环境工作这些单一的conf。

就像 ip 被 localhost 替换或 ip 被 127.0.0.1 替换(我都尝试过但没有工作)

worker_processes 4;

events { worker_connections 1024; }
http {

    sendfile on;

    upstream consumer-portal {

        server 1.2.3.4:9006;

    }

    upstream licenseportal {

        server 1.2.3.4:9006;

    }

server {
        listen 8765;

        location /consumer-portal/ {
            proxy_pass         http://consumer-portal/;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }

        location /licenseportal/ {
            proxy_pass         http://licenseportal/;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
 }


}
4

1 回答 1

7

根据这个出色的答案

  1. 您可以使用模板配置(例如)构建一次/etc/nginx/conf.d/nginx.template映像,其中包含您希望在 dev、qa 和 prod 之间更改的所有值的变量名称。例如:

    upstream licenseportal {
      server ${NGINX_HOST}:${NGINX_PORT};
    }
    
  2. 然后为所有环境运行相同的映像,在运行映像时使用特定于环境的值替换模板中的变量envsubst来创建新映像:nginx.conf

    # For Develop
    docker run -d \
      -e NGINX_HOST='1.2.3.4' \
      -e NGINX_PORT='9006' \
      -p 9006:9006 \
      jcibts-swmdtr-dev.jci.com/nginx:1 \
      /bin/bash -c "envsubst < /etc/nginx/conf.d/nginx.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
    
    # For Production
    docker run -d \
      -e NGINX_HOST='4.3.2.1' \
      -e NGINX_PORT='9006' \
      -p 9006:9006 \
      jcibts-swmdtr-dev.jci.com/nginx:1 \
      /bin/bash -c "envsubst < /etc/nginx/conf.d/nginx.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
    

注意:为此工作 -envsubst需要作为图像的一部分安装。IERUN apt-get -y update && apt-get -y install gettext

于 2018-08-20T12:59:18.960 回答