Welcome! Log In Create A New Profile

Advanced

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

Posted by German Jaber 
Hi folks,

Is there a reason why the "env" directive is only allowed inside the "main"
contexts?

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.

Regards, German Jaber
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
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/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
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/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Hello!

On Fri, Jan 05, 2018 at 02:28:45AM +0000, German Jaber wrote:

> 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.

So, this is certainly not about allowing the "env" directive
in other contexts, but instead using environment variables in the
configuration. This is not something nginx currently supports,
but rather a completely new mechanism - some preprocessing of the
configuration.

Something like this was discussed more than once, and the current
consensus is that using various template mechanisms like sed, cpp,
or whatever you prefer to is a good enough solution for such
tasks.

Note that using variables as you seem to imply in the example
above is a bad idea, as variables are evaluated at run-time.
See http://nginx.org/en/docs/faq/variables_in_config.html for
details.

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login