On limited variable usage
Maxim Dounin
mdounin at mdounin.ru
Mon Aug 30 16:44:42 MSD 2010
Hello!
On Mon, Aug 30, 2010 at 02:09:24PM +0300, Simas Toleikis wrote:
> Hey,
>
> So is there any reason why some configuration directives can't use certain
> variables?
>
> Like:
>
> alias $document_root/my/stuff;
>
> Will trigger "the $document_root variable may not be used in the "alias"
> directive" error.
This is because $document_root is set by alias, and using this
variable in alias directive itself (as well as in root directives)
introduce recursion.
> However, the following trick:
>
> set $root $document_root;
> alias $root/my/stuff;
>
> .. will simply remove this restriction.
Yes, it will "remove" the restriction by explicitly breaking
recursion. This will produce something like "/my/stuff/my/stuff"
as alias though, and warning about "using uninitialized "root"
variable" in logs.
> And another note (not sure if it is related) but:
>
> root /my/root;
> access_log $document_root/logs/;
>
> will expand into "/path/to/nginx/my/root/logs"
>
> That is, it will prefix nginx install path when used in this way (might be a
> bug?).
nginx expands paths during reading configuration, and prepends
anything not starting with '/' with prefix.
Note well: using variables in access_log directive will cause many
extra work and isn't recommended unless you really don't know
desired log location before actual request processing. See below.
> And error_log directive seems to not allow variables at all.
Yes.
> I find this behavior very limiting that usually requires a lot of path
> duplications in config file. So the question is, is there any reason why
> variable expansion can't be unified and be the same for all directives?
It looks like you misinterpreted purpose of variables in nginx.
They are evaluated during request processing and were never
designed to "shorten" configs. Using them for config shortening
is bad idea.
There are some proposals about introducing "config file macros"
for that task. While they aren't here - use some config generator
instead if you really need to.
Maxim Dounin
More information about the nginx-devel
mailing list