Documentation PortalBack to Self Assist PortalBack
Documentation Portal
Contents

Using Environment Variables with OpenResty - V 2

Configuration

TODO: configure environment variables using helm chart

Usage

Inherit required environment variables inside nginx

By default, NGINX removes all environment variables inherited from its parent process except the TZ variable.

In order to inherit required environment variables from parent process, NGINX provides env directive.

To inherit required environment variables, mention them in nginx.conf file as shown below:

worker_processes  1;
error_log logs/error.log;

# Required environment variables
env HOME;
env DB_HOST;
env DB_PORT;

events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        include loc/*.conf;
    }
}

For more details, refer this.

Using environment variables inside OpenResty

Lua provides standard library function os.getenv to get environment variable. It receives the name of the variable and returns a string with its value:

print(os.getenv("HOME")) --> /home/lua

If the variable is not defined, the call returns nil.

Exaples:

local dbhost = os.getenv("DB_HOST")
local dbport = os.getenv("DB_PORT")