无法在 Lua 代码中使用环境变量

Unable to use environment variables in Lua code

我有一些 Lua 代码,我在我的 openresty nginx.conf 文件中使用了这些代码。此 Lua 代码包含以下行:

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

出于测试原因,我尝试检查是否设置了 PATH 变量,但由于某种原因断言语句未通过。我在控制台中看到:

Environment variable PATH not set

然而,当我运行这个

$ echo $PATH

我明白了,这个变量确实有一些价值。那么,这有什么问题,我该如何解决?

您需要告诉 nginx 使环境变量可用。来自 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."

因此,在您的情况下,您需要在 nginx.conf 中指定 env PATH;