Want to access UNIX environment variable

Hi,

I have system where I have defined OS environment variable $ENV=prod or
dev.
I want to access this variable inside my nginx configuration. Please
help.

I have tried -g “env ENV” in command line. But how do I access it? $ENV
does
not work?

Posted at Nginx Forum:

On 27 February 2013 06:49, amodpandey [email protected] wrote:

Hi,

I have system where I have defined OS environment variable $ENV=prod or dev.
I want to access this variable inside my nginx configuration. Please help.

I have tried -g “env ENV” in command line. But how do I access it? $ENV does
not work?

No, this isn’t supported - both specifically what you tried, and the
more general “envvars in config” concept.
You’ll have to find another way to achieve what you want.

Jonathan

Jonathan M. // Oxford, London, UK
http://www.jpluscplusm.com/contact.html

Let me put what I want to achieve.

Before that I want to know what is this ‘env’ global directive for?

I want to maintain a single set of configuration files for dev and prod.
The
machines would have environment variable set ENV=dev or ENV=prod. Based
on
the values of the ENV varibale I wanted to pick my upstream conf, either
upstream_dev.conf or upstream_prod.conf through ‘include
upstream_$ENV.conf’.

Please suggest an approach.

Posted at Nginx Forum:

Have puppet generate the configuration based on that variable.


Snet form my moblie phoen. Please excues tpyos.

Thank you.

I am looking for an simpler ( direct ) approach. For now I have put a
sed
script in my bounce nginx which does that.

Posted at Nginx Forum:

On 28 February 2013 06:04, amodpandey [email protected] wrote:

Let me put what I want to achieve.

Before that I want to know what is this ‘env’ global directive for?

I don’t know. The documentation suggests it’s not going to solve the
problem you want; but I’ve not used it.

I want to maintain a single set of configuration files for dev and prod. The
machines would have environment variable set ENV=dev or ENV=prod. Based on
the values of the ENV varibale I wanted to pick my upstream conf, either
upstream_dev.conf or upstream_prod.conf through ‘include
upstream_$ENV.conf’.

Please suggest an approach.

Use something that helps you template your config files. Chef, puppet,
Erb, Perl, bash - whatever works for you.

[ However, I will suggest that rolling your own templating setup is a
strong indicator of an inexperienced sysadmin; one who doesn’t yet
understand the value of consistency, the principle of least surprise,
and why NIH is a Bad Thing. ]

HTH,
Jonathan

Jonathan M. // Oxford, London, UK
http://www.jpluscplusm.com/contact.html

Late to contribute as well but wanted to mention that we reference a
single
include in the main config that gets linked to the actual file at the
deployment time only.

Posted at Nginx Forum:

Left out the fact make file is used to create the link.

Posted at Nginx Forum:

Posting a bit late to this thread, but thought I’d contribute. Having
found no better way myself, the way I “import” environment variables
into my nginx configurations is as follows.

At top config level:

 env MYVAR;

At http level:

 perl_set $myvar 'sub { return $ENV{"MYVAR"}; }';

Then, for example, at server level:

 root $myvar;

Works a treat. Obviously there must be an overhead cost with this
approach, but either it’s small, or I’m small, because I haven’t noticed
it. (And even then, if I found it significant, I’d probably get to work
on a proper patch, since environment variables are rather central to my
general service configuration strategy.)

I’d love to know if anyone does it differently and/or better. If a
helper is how it must be done, I’d actually prefer to use Lua – but
“perl_set” is documented for use at the http config level, whereas
“lua_set” (for whatever reason) is not.

$0.02,
Eric