I'm in the middle of writing an article about using nginx and docker with the Angular CLI. I want the nginx config to support multiple environments. To do this, first I have an nginx-local.env file:

view plain print about

My nginx template has a line like this:

view plain print about
1upstream myui.local {
2 server ${HOST_MACHINE_ADDRESS}:4200;

The intent is for my docker file to run a script that loads the env file, performs an environment substitution on the values, outputs the file, and then runs nginx.

Roughly, like this:

view plain print about
1# load the environment file and turn it into environment varaibles
2set -a
3env >
4. "/etc/nginx/my-ui/nginx-$NODE_ENV.env"
5. "/tmp/priority.env"
6set +a
8# Resolve environment variables in NGINX Config
9envsubst '
11' < /etc/nginx/my-ui/my-ui-nginx.conf > /etc/nginx/nginx.conf
13# run nginx
14nginx -c /etc/nginx/nginx.conf -g 'daemon off;'

My problem after running all this was that the resulting nginx file was something like this:

view plain print about
1upstream myui.local {
2 :4200;server host.docker.internal

The port was moved to the front of the line, causing the nginx to reject the config file and fail to start up. What was going on?

It took me four hours and a good night's sleep to figure this out. when I Created the environment file, I did so on a Windows machine and the line endings used carriage return / line feed instead of just line feed. The carriage return was being considered as part of the value; causing the weird formatting in the file config file.

I opened up the file in IntelliJ and changed the line endings. You can see options in the bottom left of your IntelliJ window:

After I did that, everything started working as expected, and the resulting lines in my nginx config were correct:

view plain print about
1upstream myui.local {
2 server host.docker.internal:4200;