简体   繁体   中英

How to create single NGINX.conf file for multiple environment

I using NGINX as a reverse proxy.

I have 3 environments (develop, QA, production)

Consider, The IP address for develop is 1.2.3.4, qa is 4.3.2.1 and production is 3.4.1.2

I have configure the nginx.conf file as below and its working perfectly fine in case of develop environment.

While building these docker-image I have explisitly mention on which configuraton the image should be build as below

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

The requirement is docker-image should be build only 1's and then it will be push to Docker Trusted repository.

It will be promoted to other environment's docker trusted repository without building the image again.

MY question is what can I do to work these single conf for all the environment.

Like ip replaced by localhost or ip replaced by 127.0.0.1 (I have tried both but not working)

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


}

As per this excellent answer :

  1. You can build your image once with a template config (eg. /etc/nginx/conf.d/nginx.template ), which contains variable names for all the values which you expect to change between dev, qa and prod. For example:

     upstream licenseportal { server ${NGINX_HOST}:${NGINX_PORT}; } 
  2. Then run the same image for all environments, using envsubst when running the image to create a new nginx.conf by replacing the variables in the template with values specific to the environment:

     # 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;'" 

Note : For this to work - envsubst needs to be installed as part of the image. ie RUN apt-get -y update && apt-get -y install gettext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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