Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.getenv not work in custom plugin #3424

Closed
Alsmile opened this issue Apr 28, 2018 · 2 comments
Closed

os.getenv not work in custom plugin #3424

Alsmile opened this issue Apr 28, 2018 · 2 comments

Comments

@Alsmile
Copy link

Alsmile commented Apr 28, 2018

handler.lua

function CtgLogHandler:log(conf)
  CtgLogHandler.super.log(self)

  --  os.getenv("PATH")  not work, eq nil
  ngx.log(ngx.DEBUG, "[ctg-log] env - path: ", os.getenv("PATH"))

  local ok, err = timer_at(0, log, conf, serializer.serialize(ngx))
  if not ok then
    ngx.log(ngx.ERR, "[ctg-log] could not create timer: ", err)
  end
end

I can not get my environment variables in handler.lua of custom plugin.

@jeremyjpj0916
Copy link
Contributor

You need to add env PATH to your nginx.conf / Kong template file.

env PATH;

@thibaultcha
Copy link
Member

Hi,

Indeed @jeremyjpj0916 is right, this is an expected behaviour from Nginx, which only adds declared environment variables to the worker processes environ variable.

BTW, better avoid calling such non JIT-able functions like os.getenv in hot-code paths and cache them somewhere instead (like the top-level chunk of your Lua module for example).

erran added a commit to erran/kong-datadog-k8s that referenced this issue Jan 19, 2021
Note env must be set in your Nginx configuration's main context. This can be done by exporting `KONG_NGINX_MAIN_ENV`.

See also Kong/kong#3424, https://docs.konghq.com/2.2.x/configuration/, and 0535f0e.
erran added a commit to erran/kong-datadog-k8s that referenced this issue Jan 19, 2021
Note env must be set in your Nginx configuration's main context. This
can be done by exporting `KONG_NGINX_MAIN_ENV`.

See also:
* Kong/kong#3424
* https://docs.konghq.com/2.2.x/configuration/
* http://nginx.org/en/docs/ngx_core_module.html#env
* 0535f0e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants