Root works, alias doesn't

Can anyone tell me why this works:

root /var/www/localhost/htdocs;
location / {
root /var/www/localhost/htdocs/webalizer/;
}

And this doesn’t:

root /var/www/localhost/htdocs;
location / {
alias /webalizer/;
}

I get:

“/webalizer/index.html” is not found (2: No such file or directory)

/var/www/localhost/htdocs/webalizer/index.html does exist.

  • Grant

Absolute vs Relative paths.
The log file line says it all: ‘/webalizer/index.html’ doesn’t exist,
which
is not the path of the file you wanna serve…

Take a look at the following examples showing how ‘location’ address is
replaced or completed (depending on absolute or relative ‘alias’
directive)
by ‘alias’ path:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

PEBCAD? ;o)

B. R.

Absolute vs Relative paths.
The log file line says it all: ‘/webalizer/index.html’ doesn’t exist, which
is not the path of the file you wanna serve…

Take a look at the following examples showing how ‘location’ address is
replaced or completed (depending on absolute or relative ‘alias’ directive)
by ‘alias’ path:
Module ngx_http_core_module

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.

nginx alias+location directive - Stack Overflow

I tried both of the following with the same result:

location / {
alias webalizer/;
}

location ~ ^/$ {
alias webalizer/$1;
}

  • Grant

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 /”.

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

On Sunday 29 September 2013 23:20:35 B.R. wrote:
[…]

alias is meant for exceptional overload of root in a location block, so I
guess its use here is a good idea.​
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).
[…]

nginx is not trying to be a template engine.
http://nginx.org/en/docs/faq/variables_in_config.html

wbr, Valentin V. Bartenev

Hello,

On Sun, Sep 29, 2013 at 1:33 PM, Grant [email protected] wrote:

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.

Module ngx_http_core_module
Module ngx_http_core_module

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 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.​
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.

B. R.

On Tuesday 01 October 2013 11:12:41 Grant wrote:

That’s for root.

Not exactly. For example, request “/favicon.ico”:

location / {
alias /data/www;
}

will result in opening “/data/wwwfavicon.ico”, while:

location / {
root /data/www;
}

will return “/data/www/favicon.ico”.

But,

location / {
alias /data/www/;
}

will work the same way as

location / {
root /data/www;
}

or

location / {
root /data/www/;
}

wbr, Valentin V. Bartenev

That’s for root.

 root /data/www;

will work the same way as

location / {
root /data/www;
}

or

location / {
root /data/www/;
}

That’s true. Is alias or root preferred in this situation for
performance?

  • Grant

On Wednesday 02 October 2013 20:22:17 Grant wrote:
[…]

That’s true. Is alias or root preferred in this situation for performance?

The “root” directive is better from any point of view. It is less
complicated
and bugfree (“alias” has bugs, see
#97 (try_files and alias problems) – nginx ).

You should always prefer “root” over “alias” when it is possible.

wbr, Valentin V. Bartenev

That’s true. Is alias or root preferred in this situation for performance?

The “root” directive is better from any point of view. It is less complicated
and bugfree (“alias” has bugs, see https://trac.nginx.org/nginx/ticket/97 ).

You should always prefer “root” over “alias” when it is possible.

Many thanks Valentin.

  • Grant