Created
July 26, 2015 11:57
-
-
Save mauvm/5de07085f3b51e117378 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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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;" |
I improved the script a bit, now it does not replace "=" in for example proxy_cache_path
and it works with the docker image nginx:alpine
(if one add bash).
#!/bin/bash
# NOTE: Brackets are not supported and '$' in values will break the script.
function replace_env {
mkdir /etc/nginx/conf.d 2> /dev/null
for file in /etc/nginx/conf.d/*.conf
do
TPL=$(cat $file)
for row in $(env)
do
SAVEIFS=$IFS
IFS="="
read key val <<< "$row"
IFS=$SAVEIFS
if [[ $val == tcp://* ]]
then
valhttp=$(echo -n $val | sed 's#^tcp://#http://#')
TPL=$(echo -n $TPL | sed "s\$\\\$$key\_HTTP\$$valhttp\$")
else
TPL=$(echo -n $TPL | sed "s\$\\\$$key\$$val\$")
fi
done
echo -n $TPL > "/etc/nginx/conf.d/$(basename $file)"
done
}
replace_env
nginx -g "daemon off;"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about the following: