<p>I have an nginx pod deployed in my kubernetes cluster to serve static files. In order to set a specific header in different environments I have followed the instructions in the <a href="https://github.com/docker-library/docs/tree/master/nginx#using-environment-variables-in-nginx-configuration" rel="nofollow noreferrer">official nginx docker image docs</a> which uses <code>envsubst</code> to generate the config file from a template before running nginx.</p>

<p>This is my nginx template (nginx.conf.template):</p>

<pre><code>events {
  worker_connections 1024;
}

http {

  include mime.types;
  default_type application/octet-stream;
  sendfile on;

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  gzip on;

  server {
    listen 443 ssl;

    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

    root /usr/share/nginx/html;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location ~ \.css {

      add_header Content-Type text/css;
    }

    location ~ \.js {

      add_header Content-Type application/x-javascript;
    }

    location / {
      add_header x-myapp-env $MYAPP_ENV;
      index index.html index.htm;
      try_files $uri $uri/ /index.html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {

      root /usr/share/nginx/html;
    }
  }

  server {
    listen      80;
    server_name _;
    return 301 https://$host$request_uri;
  }
}
</code></pre>

<p>I use the <a href="https://v1-6.docs.kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/" rel="nofollow noreferrer">default command override</a> feature of Kubernetes to initially generate the nginx conf file before starting nginx. This is the relevant part of the config:</p>

<pre><code> command: ["/bin/sh"]
 args: ["-c", "envsubst &lt; /etc/nginx/nginx.conf.template &gt; /etc/nginx/nginx.conf &amp;&amp; nginx -g 'daemon off;'" ]
</code></pre>

<p>Kubernetes successfully deploys the pod however when I make a request I get a <code>ERR_TOO_MANY_REDIRECTS</code> error in my browser.</p>

<p>Strangely, when I deploy the container without running the command override using an nginx.conf almost identical to the above (but without the <code>add_header</code> directive) it works fine.</p>

<p>(All SSL certs and files to be served are happily copied onto the container at build time so there should be no issue there)</p>

<p>Any help appreciated. </p>

<p>I am pretty sure <code>envsubst</code> is biting you by making <code>try_files $uri $uri/ /index.html;</code> into <code>try_files  / /index.html;</code> and <code>return 301 https://$host$request_uri;</code> into <code>return 301 https://;</code>. This results in a loop of redirections.</p>

<p>I suggest you run <code>envsubst '$MYAPP_ENV' &lt;template &gt;nginx.conf</code> instead. That will only replace that single variable and not the unintended ones. (Note the escaping around the variable in the sample command!) If later on you need to add variables you can specify them all like <code>envsubsts '$VAR1$VAR2$VAR3'</code>.</p>

<p>If you want to replace <em>all</em> environment variables you can use this snippet:</p>

<pre><code>envsubst `declare -x | sed 's/^declare -x \([^=]*\)=.*/$\1/' | tr -d '\n'` &lt;template &gt;nginx.conf
</code></pre>

<p>Also, while it's not asked in the question you can save yourself some trouble by using <code>... &amp;&amp; exec nginx -g 'daemon off;'</code>. The <code>exec</code> will replace the running shell (pid 1) with the nginx process instead of forking it. This also means that signals will be received by nginx, etc.</p>
