# Inconsistency in ability to use variables in nginx config

**URL:** <https://www.ruby-forum.com/t/inconsistency-in-ability-to-use-variables-in-nginx-config/237434>\
**Category:** NGINX\
**Created:** [November 13, 2013, 7:26am UTC](https://www.ruby-forum.com/t/inconsistency-in-ability-to-use-variables-in-nginx-config/237434 "2013-11-13T07:26:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [November 13, 2013, 7:26am UTC](https://www.ruby-forum.com/t/inconsistency-in-ability-to-use-variables-in-nginx-config/237434/1 "2013-11-13T07:26:26Z")

</div>

It seems like it’s possible to use variables in some configuration  
directives but not in others. This works:

root /usr/www/$sitename/httpdocs;

works, while these:

access\_log /var/log/www/$sitename/access.log;  
proxy\_cache $sitename;

do not, instead generating errors when I try to restart nginx.

Is there any documentation that explains where I can and cannot use a  
variable? Or is there some syntax I should be using to indicate variable  
interpolation? It would be immensely helpful if they worked in all these  
contexts, because then we wouldn’t have to repeat so much across  
different  
site configuration files, leading to the requirement for generating them  
with clunky scripts. As it is, the variables are of very limited  
utility.

Posted at Nginx Forum:

> **[Inconsistency in ability to use variables in nginx config](https://forum.nginx.org/read.php?2,244605,244605#msg-244605)**
>
> It seems like it's possible to use variables in some configuration directives but not in others. This works: root /usr/www/$sitename/httpdocs; works, while these: access\_log /var/log/www/$sitename/access.log; proxy\_cache $sitename; d

---

<div class="post-metadata">

**Author:** ![miguel\_hamster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@miguel\_hamster](https://www.ruby-forum.com/u/miguel_hamster)\
**Post date:** [November 13, 2013, 11:16am UTC](https://www.ruby-forum.com/t/inconsistency-in-ability-to-use-variables-in-nginx-config/237434/2 "2013-11-13T11:16:35Z")

</div>

On Wednesday 13 November 2013 10:25:44 miguel\_hamster wrote:

> It seems like it’s possible to use variables in some configuration  
> directives but not in others. This works:
> 
> root /usr/www/$sitename/httpdocs;
> 
> works, while these:
> 
> access\_log /var/log/www/$sitename/access.log;

This should work too. See documentation: [Module ngx\_http\_log\_module](http://nginx.org/r/access_log)

> proxy\_cache $sitename;
> 
> do not, instead generating errors when I try to restart nginx.
> 
> Is there any documentation that explains where I can and cannot use a  
> variable?

See official docs: [nginx documentation](http://nginx.org/en/docs/)  
It’s usually explicitly stated if directive supports variables

> Or is there some syntax I should be using to indicate variable  
> interpolation? It would be immensely helpful if they worked in all these  
> contexts, because then we wouldn’t have to repeat so much across different  
> site configuration files, leading to the requirement for generating them  
> with clunky scripts. As it is, the variables are of very limited utility.

Yes, it is. Variables in nginx are not supposed to replace template  
engines  
or special configuration-generation tools.

See FAQ: [Is there a proper way to use nginx variables to make sections of the configuration shorter, using them as macros for making parts of configuration work as templates?](http://nginx.org/en/docs/faq/variables_in_config.html)

Also, some duplication in config isn’t something horrible. Nginx  
configuration  
is not a programming language, it utilizes another paradigm and trying  
to be  
declarative.

wbr, Valentin V. Bartenev
