Interview Prep/Shell Scripting/Troubleshooting: tools, portability, permissions and environment

A container entrypoint renders `nginx.conf.template` with `envsubst`, and nginx fails with `nginx: [emerg] invalid number of arguments in "proxy_set_header" directive in /etc/nginx/conf.d/default.conf:12`.

IntermediateTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer

Likely causes → envsubst replaces every $VAR it sees, including nginx's own variables like $host and $remote_addr, which are unset in the shell environment and become empty. proxy_set_header Host $host; turns into proxy_set_header Host ;.

Diagnose →

sed -n '12p' /etc/nginx/conf.d/default.conf      # proxy_set_header Host ;
nginx -t

Fix → Give envsubst an explicit list of variables to substitute:

envsubst '${BACKEND_HOST} ${BACKEND_PORT}' < nginx.conf.template > /etc/nginx/conf.d/default.conf
nginx -t && exec nginx -g 'daemon off;'

The official nginx image does this for /etc/nginx/templates/*.template, substituting only variables that are defined in the environment.

Prevent → Always pass the variable list to envsubst, and run nginx -t (or the equivalent config check) before starting or reloading.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: tools, portability, permissions and environment