init_by_lua* can't read environment variable values set by nginx "env" directive

824 views
Skip to first unread message

Josh Stockin

unread,
Jan 17, 2021, 4:19:37 PM1/17/21
to openre...@googlegroups.com
nginx configuration:

```
env RESTY_GITWEB=;
env RESTY_GITWEB_ENV=PROD;
env RESTY_GITWEB_CONFIG="/etc/joshstock.in/resty-gitweb.yaml";
...

http {
...
init_by_lua_file "/home/josh/repos/resty-gitweb/init.lua";
```

In init.lua:

```
ngx.log(ngx.ERR,
"RESTY_GITWEB: \""..os.getenv("RESTY_GITWEB").."\", ",
"RESTY_GITWEB_ENV: \""..os.getenv("RESTY_GITWEB_ENV").."\", ",
"RESTY_GITWEB_CONFIG: \""..os.getenv("RESTY_GITWEB_CONFIG").."\"")
```

Attempting to start nginx/OpenResty produces this in the error.log:

```
[lua] init.lua:20: RESTY_GITWEB: "", RESTY_GITWEB_ENV: "",
RESTY_GITWEB_CONFIG: ""
```

OpenResty actually reads these environment variables as "", not nil as
happens when unset. i.e. os.getenv() in OpenResty can identify which
variables are set but can't read any of their values.

Am I doing something wrong or is this a bug in OpenResty?

Using nginx 1.19.6 built with lua-nginx-module@138c1b96 and
lua-resty-core@3b4ad75
--
Josh Stockin <jo...@joshstock.in>
<https://joshstock.in>

Kshitij Joshi

unread,
Apr 2, 2021, 4:19:38 PM4/2/21
to openresty-en
When you set env= it means you are setting it to "".
You should be setting 
env RESTY_GITWEB;

http {
    env TEST1;
    env TEST2=;
    env TEST3=abcd;
    init_by_lua_file /usr/local/openresty/lualib/test.lua;
}
The test.lua file has 

print(os.getenv("TEST1"))
print(os.getenv("TEST2"))
print(os.getenv("TEST3"))

The output is

2021/04/02 13:17:36 [notice] 1#1: [lua] test.lua:1: nil
2021/04/02 13:17:36 [notice] 1#1: [lua] test.lua:2: 
2021/04/02 13:17:36 [notice] 1#1: [lua] test.lua:3: abcd

Reply all
Reply to author
Forward
0 new messages