root works, alias doesn't

Grant emailgrant at gmail.com
Tue Oct 1 07:12:41 UTC 2013


>> It works if I specify the full path for the alias.  What is the
>> difference between alias and root?  I have root specified outside of
>> the server block and I thought I could use alias to avoid specifying
>> the full path again.
>
> http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
> http://nginx.org/en/docs/http/ngx_http_core_module.html#root
>
> The docs says that the requested filepath is constructed by concatenating
> root + URI
> That's for root.
>
> The docs also say that alias replaces the content directory (so it must be
> absolutely defined through alias).
> By default, the last part of the URI (after the last slash, so the file
> name) is searched into the directory specified by alias.
> alias doesn't construct itself based on root, it's totally independent, so
> by using that, you'll need to specify the directory absolutely, which is
> precisely what you wish to avoid.

I see.  It seems like root and alias function identically within "location /".

>> I tried both of the following with the same result:
>>
>> location / {
>>     alias webalizer/;
>> }
>>
>> location ~ ^/$ {
>>     alias webalizer/$1;
>> }
>
>
> For
>
> what you wish to do, you might try the following:
>
> set $rootDir /var/www/localhost/htdocs
> root $rootDir/;
> location / {
>     alias $rootDir/webalizer/;
> }
>
> alias is meant for exceptional overload of root in a location block, so I
> guess its use here is a good idea.

I'm not sure what you mean by that last sentence.  When should alias
be used instead of root inside of "location /"?

> However, there seems to be no environmental propagation of some $root
> variable (which may be wanted by developers to avoid confusion and unwanted
> concatenation of values in the variables tree).
> $document_root and $realpath_root must be computed last, based on the value
> of the 'root' directive (or its 'alias' overload), so they can't be used
> indeed.
>
> I'd be glad to know the real reasons of the developers behind the absence of
> environmental propagation of some $root variable.

Me too.

- Grant



More information about the nginx mailing list