Allow the usen of the "env" directive in contexts other than "main"

German Jaber germanjaber at gmail.com
Fri Jan 5 02:28:45 UTC 2018


What I want to be able to do is basically what this guy tried to do:
https://serverfault.com/questions/577370/how-can-i-use-environment-variables-in-nginx-conf

Succinctly, I want to pass the value of environment variables to my server
and location contexts, and use those values in the arguments passed to my
directives.

For example. Say I have two servers, dev and prod; and that I have a file
in sites-enabled that reads:

env URL;
server {
    listen 80;
    server_name $URL;

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

    location /static/ {
        alias /app/static/;
    }

    location / {
        alias /app/main/;
    }
}

I want the server_name directive to receive as argument the domains (or
list of domains) contained in the environment variable $URL.

This is very useful when using container technology (Docker, Kubernetes,
etc) as the same Nginx configuration files may be used by many different
environments (dev, staging, prod, CI, CD, etc). Many times these
environments are created dynamically and programmatically by our
continuous delivery pipelines, so setting them by hand is cumbersome or
downright impossible. Right now I use sed and/or envsubst in our scripts to
modify the file before using it.

It would simplify our deployment scripts a ton if Nginx could just read
these environment variables and substitute them for us wherever we need.
> Hello!
>
> On Thu, Jan 04, 2018 at 01:22:15AM +0000, German Jaber wrote:
>
> > Is there a reason why the "env" directive is only allowed inside the
"main"
> > contexts?
>
> The "env" directive controls which environment variables will be
> available in the nginx worker processes.  Environment variables
> apply to the whole worker process, and are not differentiated based on
> what the worker process is doing in the particular time.  As such,
> these directives are to be specified at the global level.
>
> > It would simplify many of my Docker deployments if I could do away with
sed
> > and envsubst and use the "env" directive directly.
> >
> > If the maintainers approve the inclusion of this feature in Nginx, I
would
> > like to offer my time to this project by implementing this
functionality.
>
> Sorry, from your description it is not clear what you are trying
> to do and how the "env" directive can help here.  You may want to
> elaborate on this.
>
> --
> Maxim Dounin
> http://mdounin.ru/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20180105/2c17b1ad/attachment.html>


More information about the nginx mailing list