Skip to content

Instantly share code, notes, and snippets.

@afloesch
Created January 29, 2020 02:41
Show Gist options
  • Save afloesch/0b80de2e356f28d1985e7957a7cda4ca to your computer and use it in GitHub Desktop.
Save afloesch/0b80de2e356f28d1985e7957a7cda4ca to your computer and use it in GitHub Desktop.
Nginx Environment Variables

Nginx Environment Variables

I had a hard time finding a decent example on how to use the lua nginx module to get environment variables and pass them to the location block for the proxy settings, so wanted to share the solution that worked for me.

Example nginx conf:

env NGROK_URL;

events {
  worker_connections  1024;
}

http{
  access_log   /var/log/access.log;

  server {
    listen 80;

    location / {
      set_by_lua $ngrok_url 'return os.getenv("NGROK_URL")';
      proxy_pass http://$ngrok_url;
      proxy_set_header Host $ngrok_url;
      resolver 8.8.8.8;
    }
  }
}

If you are trying to do this using the openresty docker image, another thing you will want to do is overwrite the base default nginx.cong in the image. Replace the nginx.conf at /usr/local/openresty/nginx/conf/nginx.conf in the openresty docker image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment