2

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


}
Anand Deshmukh
  • 1,086
  • 2
  • 17
  • 35

2 Answers2

7

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. i.e RUN apt-get -y update && apt-get -y install gettext

moebius
  • 2,061
  • 11
  • 20
  • Thanks for the reply. In my case, the image will be built only once. Though I specified environment variable it will build on those configurations. I am looking for something which will allow me to use the same image in all the environment. – Anand Deshmukh Aug 20 '18 at 13:31
  • No problem. This will only require you to build the image once, and use the same image. I've updated the answer to make this clearer. – moebius Aug 20 '18 at 20:31
0

You can use variables declared via map in the main http block:

http {
  ...
  map "" $env {
    default dev;
  }
  ...
}

Then you can combine with if statement:

server {
  ...
  if ($env = "dev") {
    // do something
  }
}

Source: Using variables in Nginx location rules

Arthur Ronconi
  • 2,290
  • 25
  • 25