繁体   English   中英

如何为多个环境创建单个NGINX.conf文件

[英]How to create single NGINX.conf file for multiple environment

我使用NGINX作为反向代理。

我有3个环境(开发,质量检查,生产)

考虑一下,要开发的IP地址是1.2.3.4,qa是4.3.2.1,生产是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 Trusted存储库。

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

我的问题是我应该怎么做才能在所有环境中使用这些单独的配置文件。

就像用localhost替换ip或用127.0.0.1替换ip一样(我都尝试过但不能正常工作)

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


}

根据这个很好的答案

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM