Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valmayaki/be6bea9b0e9658fecf999a2042c1648a to your computer and use it in GitHub Desktop.
Save valmayaki/be6bea9b0e9658fecf999a2042c1648a to your computer and use it in GitHub Desktop.
An "envsubst" alternative for replacing env variables in NGinX site configurations (for using it with Docker)
#!/bin/bash
# NOTE: Brackets are not supported and '$' in values will break the script.
mkdir /etc/nginx/sites-enabled 2> /dev/null
for file in /etc/nginx/sites-available/*.conf
do
TPL=$(cat $file)
for row in $(env)
do
IFS="="
read key val <<< "$row"
if [[ $val == tcp://* ]]
then
valhttp=$(echo -n $val | sed 's#^tcp://#http://#')
TPL=$(echo -n $TPL | sed "s\$\\\$$key\_HTTP\$$valhttp\$")
fi
TPL=$(echo -n $TPL | sed "s\$\\\$$key\$$val\$")
done
echo -n $TPL > "/etc/nginx/sites-enabled/$(basename $file)"
done
nginx -g "daemon off;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment