【问题标题】:How to create single NGINX.conf file for multiple environment如何为多个环境创建单个 NGINX.conf 文件
【发布时间】:2019-01-26 05:37:36
【问题描述】:

我使用 NGINX 作为反向代理。

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

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

我已经配置了如下的 nginx.conf 文件,它在 develop 环境下工作得非常好。

在构建这些 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 可信仓库,而无需重新构建镜像。

我的问题是我可以做些什么来为所有环境使用这些单一的配置。

比如 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;
        }
 }


}

【问题讨论】:

    标签: docker nginx nginx-reverse-proxy nginx-config


    【解决方案1】:

    根据这位优秀的answer

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

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

      # 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 作为映像的一部分进行安装。即RUN apt-get -y update &amp;&amp; apt-get -y install gettext

    【讨论】:

    • 感谢您的回复。在我的情况下,图像将只构建一次。虽然我指定了环境变量,但它将建立在这些配置之上。我正在寻找可以让我在所有环境中使用相同图像的东西。
    • 没问题。这只需要您构建一次映像,并使用相同的映像。我已经更新了答案以使这一点更清楚。
    猜你喜欢
    • 2022-01-04
    • 2018-05-27
    • 2019-06-21
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2011-04-29
    相关资源
    最近更新 更多