0

So I'm deploying a React app into Openshift that will call a separate API service and I need to determine the proxy_pass in my nginx.conf based on the staging environment- dev or prod. This seems to be a common enough use case but seems to be extremely difficult to implement. I'm using the nginxinc/nginx-unprivileged for my nginx server and not the regular nginx image though- I don't know if that's causing any issues. It seems to be using nginx 1.19 which supports envsubst in nginx.conf files.

What I'm trying to do is this:

  1. Define a proxy_url environment variable in my buildConfig.yml and prodBuildConfig.yml respectively
  2. Fire off envsubst in my Dockerfile to replace the variables in my nginx.conf to pass into proxy_pass

The code looks like this

buildConfig.yml

...
  spec:
  resources: {}
  strategy:
    dockerStrategy:
      env:
        - name: "API_URL"
          value: "http://devservice:8080"
      dockerfilePath: Dockerfile

Dockerfile

...
CMD /bin/bash -c "envsubst '\$API_URL' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

and finally the nginx.conf

...
location /api {
    proxy_pass ${API_URL};
}

But nothing seems to work. I've tried a million variations of the CMD bin/sh envsubst command like

CMD /bin/sh -c envsubst \${API_URL} < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'

I've tried changing the quotes around as I've seen done on many posts, I've tried using bin/sh vs bin/bash, I've tried changing the name of the conf file to things like nginx.conf.template or just default.temp, I've tried copying the nginx template into the etc/nginx/templates folder which supposedly should automatically run envsubst and place it into /conf.d/ but that doesn't work. I've essentially tried it all. I'm completely new to Openshift, Docker, and Nginx so I really don't know what this workflow should even look like and whether or not I'm overcomplicating things or just on the wrong path all together. What am I missing? Please be as explicit as you can with how you explain things because I'm new to these technologies.

Al Pal
  • 275
  • 1
  • 9

1 Answers1

0

I got this doing the following:

  • first you have to add perl to your nginx.conf and add you env vars as shown this image
  • second you have to use your env vars in your default.conf as shown this image
luisbar
  • 676
  • 5
  • 11