Saturday, May 28, 2022

[SOLVED] nginx: use environment variables

Issue

I have the following scenario: I have an env variable $SOME_IP defined and want to use it in a nginx block. Referring to the rel="noreferrer">nginx documentation I use the env directive in the nginx.conf file like the following:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

env SOME_IP;

Now I want to use the variable for a proxy_pass. I tried it like the following:

location / {
    proxy_pass http://$SOME_IP:8000;
}

But I end up with this error message: nginx: [emerg] unknown "some_ip" variable


Solution

The correct usage would be $SOME_IP_from_env, but environment variables set from nginx.conf cannot be used in server, location or http blocks.

You can use environment variables if you use the openresty bundle, which includes Lua.



Answered By - Ben Whaley
Answer Checked By - David Marino (WPSolving Volunteer)