# How to reference OS Environment Variales in nginx.conf

**URL:** <https://www.ruby-forum.com/t/how-to-reference-os-environment-variales-in-nginx-conf/211883>\
**Category:** NGINX\
**Created:** [September 15, 2011, 11:04pm UTC](https://www.ruby-forum.com/t/how-to-reference-os-environment-variales-in-nginx-conf/211883 "2011-09-15T23:04:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [September 15, 2011, 11:04pm UTC](https://www.ruby-forum.com/t/how-to-reference-os-environment-variales-in-nginx-conf/211883/1 "2011-09-15T23:04:25Z")

</div>

Hi there,

After set a variable by `set $name value`, i can reference it like  
`$name`,  
but when i export a OS Environment Variale by `env name_from_env`, like  
[Core functionality](http://wiki.nginx.org/CoreModule#env) said,  
and i am sure the name\_from\_env is valid which defined form nginx’s  
parent process.

But, my friends, how to reference it ?  
$name\_from\_env or ${name\_from\_env} or %name\_from\_env% didn’t work what  
i’ve tried before.

Thanks a lot.

Posted at Nginx Forum:

> **[How to reference OS Environment Variales in nginx.conf](https://forum.nginx.org/read.php?2,215269,215269#msg-215269)**
>
> Hi there, After set a variable by \`set $name value\`, i can reference it like \`$name\`, but when i export a OS Environment Variale by \`env name\_from\_env\`, like http://wiki.nginx.org/CoreModule#env said, and i am sure the name\_from\_env is valid which...

---

<div class="post-metadata">

**Author:** ![caoxingk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/48db29/32.png) [@caoxingk](https://www.ruby-forum.com/u/caoxingk)\
**Post date:** [September 16, 2011, 4:51am UTC](https://www.ruby-forum.com/t/how-to-reference-os-environment-variales-in-nginx-conf/211883/2 "2011-09-16T04:51:16Z")

</div>

2ed version is here:

> <https://stackoverflow.com/questions/7439515/how-to-reference-os-environment-variables-in-nginx-conf>

Posted at Nginx Forum:

> **[Re: How to reference OS Environment Variales in nginx.conf](https://forum.nginx.org/read.php?2,215269,215278#msg-215278)**
>
> Hi there, After set a variable by \`set $name value\`, i can reference it like \`$name\`, but when i export a OS Environment Variale by \`env name\_from\_env\`, like http://wiki.nginx.org/CoreModule#env said, and i am sure the name\_from\_env is valid which...

---

<div class="post-metadata">

**Author:** ![caoxingk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/48db29/32.png) [@caoxingk](https://www.ruby-forum.com/u/caoxingk)\
**Post date:** [September 16, 2011, 6:07am UTC](https://www.ruby-forum.com/t/how-to-reference-os-environment-variales-in-nginx-conf/211883/3 "2011-09-16T06:07:08Z")

</div>

On Fri, Sep 16, 2011 at 5:03 AM, caoxingk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> After set a variable by `set $name value`, i can reference it like  
> `$name`,  
> but when i export a OS Environment Variale by `env name_from_env`, like  
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/CoreModule#env) said,  
> and i am sure the name\_from\_env is valid which defined form nginx’s  
> parent process.
> 
> But, my friends, how to reference it ?

You can read system environment variables with ngx\_lua enabled in your  
nginx build: [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpLuaModule)

```
env PATH;
http {
    ...
    server {
        location /path {
            set_by_lua $path 'return os.getenv("PATH")';
            ...
        }
    }

```

BTW, to use the set\_by\_lua directive, you also need to enable the  
ngx\_devel\_kit module here: [GitHub - vision5/ngx\_devel\_kit: Nginx Development Kit - an Nginx module that adds additional generic tools that module developers can use in their own modules · GitHub](https://github.com/simpl/ngx_devel_kit)  
(it’ll be easier if you use the ngx\_openresty bundle).

Regards,  
-agentzh
