Root works, alias doesn't


#1

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

#2

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
http://stackoverflow.com/questions/10084137/nginx-aliaslocation-directive

PEBCAD? ;o)

B. R.


#3

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

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://stackoverflow.com/questions/10084137/nginx-aliaslocation-directive

I tried both of the following with the same result:

location / {
alias webalizer/;
}

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

  • Grant

#4

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

#5

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


#6

Hello,

On Sun, Sep 29, 2013 at 1:33 PM, Grant removed_email_address@domain.invalid 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.

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


#7

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


#8

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

#9

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
https://trac.nginx.org/nginx/ticket/97 ).

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

wbr, Valentin V. Bartenev


#10

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