How to use env vars with Openresty and Lua

1.3k views Asked by At

I'm trying to use the environment variables with Openresty using Lua:

worker_processes auto;
events { worker_connections 1024; }

env API_USERS;
env API_ROLESTECHS;

http{
  server {
    set_by_lua $api_users 'return os.getenv("API_USERS")';
    set_by_lua $api_rolestechs 'return os.getenv("API_ROLESTECHS")';
    listen 80;
    location / {
      root /usr/local/openresty/nginx/html;
    }
    rewrite ^/users/(.*)$ $api_users:8080/$1;
    rewrite ^/rolestechs/(.*)$ $api_rolestechs:1323/$1;
  }
}

I'm basically saying:

  • If the URL doesn't match the following regexps, go to the html folder and serve the static files.
  • Else, rewrite the incoming URI with the env vars.

The following config works fine, but the URL's (myurlusers and myurlrolestechs) are hardcoded:

worker_processes auto;
events { worker_connections 1024; }

http{
  server {
    listen 80;
    location / {
      root /usr/local/openresty/nginx/html;
    }

    rewrite ^/users/(.*)$ http://myurlusers:8080/$1;
    rewrite ^/rolestechs/(.*)$ http://myurlrolestechs:1323/$1;
  }
}

API_USERS and API_ROLESTECH are environment variables from the host Openresty runs on.

What am I missing?

0

There are 0 answers