Unable to use environment variables in Lua code

Jacobian picture Jacobian · Jan 23, 2017 · Viewed 10.5k times · Source

I have some Lua code, which I use in my openresty nginx.conf file. This Lua code contains such lines:

...
local secret = os.getenv("PATH")
assert(secret ~= nil, "Environment variable PATH not set")
...

Just for testing reasons I tried to check if PATH variable is set and for some reason the assert statement does not pass. I see in the console:

Environment variable PATH not set

However, when I run this

$ echo $PATH

I see, that this variable indeed has some value. So, what is wrong with that and how can I fix it?

Answer

Paul Kulchenko picture Paul Kulchenko · Jan 23, 2017

You need to tell nginx to make environment variables available. From the docs for the env directive: "By default, nginx removes all environment variables inherited from its parent process except the TZ variable. This directive allows preserving some of the inherited variables, changing their values, or creating new environment variables."

So, in your case you'd need to specify env PATH; in nginx.conf.