23

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?

Jacobian
  • 10,122
  • 29
  • 128
  • 221

1 Answers1

25

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.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • @Jacobian, you may want to accept the answer if it worked for you. – Paul Kulchenko Jan 25 '17 at 04:20
  • Yes, sure! Thank you, sir! – Jacobian Jan 30 '17 at 12:08
  • "you'd need to specify env PATH; in nginx.conf". Can you please show example? Should I also specify a path? or just add "env PATH" ? Thanks. – jrz Mar 01 '21 at 15:48
  • Right; that's the entire command you need to add to nginx.conf. – Paul Kulchenko Mar 01 '21 at 15:58
  • @PaulKulchenko Hi, I had the same issue. I defined a new env var "export SECRET=mySecret" and inside nginx.conf I specify env SECRET; but when calling it, it seems like nothing exists. Only if I specify an env var that already existed, calling it will print back the right value. I opened a thread please take a look if you can: https://stackoverflow.com/questions/66518718/env-var-is-defined-on-docker-but-returns-none?noredirect=1#comment117593106_66518718 – jrz Mar 07 '21 at 18:13
  • @PaulKulchenko Hi man your advise is really needed as this situation is critical. Thanks a lot. – jrz Mar 07 '21 at 20:31
  • You need to make sure you're setting up the environment variable that nginx can see; there is nothing else you may need to do (other than what's written in the answer). – Paul Kulchenko Mar 08 '21 at 01:20