Unfortunately 'export' is mandatory, otherwise it won't work. Here is an example.
First, the input file. Note that I only want to change NGINX_HOST and NGINX_PORT and don't want to touch other variables in the input file such as '$uri'.
$ egrep 'server_name|listen|try_files' nginx-default.conf.template
server_name ${NGINX_HOST};
listen ${NGINX_PORT};
try_files $uri $uri/ /index.php?$args;
Now the empty variables:
$ echo ${NGINX_HOST}
$ echo ${NGINX_PORT}
Set default values to these variables:
$ NGINX_HOST=${NGINX_HOST:-localhost}
$ NGINX_PORT=${NGINX_PORT:-80}
$ echo ${NGINX_HOST}
localhost
$ echo ${NGINX_PORT}
80
Try to use envsubst:
$ envsubst \
'${NGINX_HOST} ${NGINX_PORT}' \
< nginx-default.conf.template \
| egrep 'server_name|listen|try_files'
server_name ;
listen ;
try_files $uri $uri/ /index.php?$args;
As you can see, it did not work. The variables must be 'export'-ed for this to work. Here is the second run:
$ export NGINX_HOST=${NGINX_HOST:-localhost}
$ export NGINX_PORT=${NGINX_PORT:-80}
$ envsubst '${NGINX_HOST} ${NGINX_PORT}' < nginx-default.conf.template | egrep 'server_name|listen|try_files'
server_name localhost;
listen 80;
try_files $uri $uri/ /index.php?$args;
As you can see, now it worked. Hope it helps others.