Forum: NGINX root works, alias doesn't

35c5f4b67fb34acfa1fe1e7beb869b2d?d=identicon&s=25 Grant (Guest)
on 2013-09-25 20:14
(Received via mailing list)
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
1266aa99d1601b47bbd3ec22affbb81c?d=identicon&s=25 B.R. (Guest)
on 2013-09-25 20:26
(Received via mailing list)
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...
http://stackoverflow.com/questions/10084137/nginx-...

PEBCAD? ;o)
---
*B. R.*
35c5f4b67fb34acfa1fe1e7beb869b2d?d=identicon&s=25 Grant (Guest)
on 2013-09-29 19:34
(Received via mailing list)
> 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...

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

I tried both of the following with the same result:

location / {
    alias webalizer/;
}

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

- Grant
1266aa99d1601b47bbd3ec22affbb81c?d=identicon&s=25 B.R. (Guest)
on 2013-09-29 21:22
(Received via mailing list)
Hello,


On Sun, Sep 29, 2013 at 1:33 PM, Grant <emailgrant@gmail.com> 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...
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.*
35c5f4b67fb34acfa1fe1e7beb869b2d?d=identicon&s=25 Grant (Guest)
on 2013-10-01 09:13
(Received via mailing list)
> 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
40b4c848b8fcd63b0cb60b9d170c3a77?d=identicon&s=25 Valentin V. Bartenev (Guest)
on 2013-10-01 15:55
(Received via mailing list)
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
40b4c848b8fcd63b0cb60b9d170c3a77?d=identicon&s=25 Valentin V. Bartenev (Guest)
on 2013-10-01 16:10
(Received via mailing list)
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
35c5f4b67fb34acfa1fe1e7beb869b2d?d=identicon&s=25 Grant (Guest)
on 2013-10-02 18:22
(Received via mailing list)
>> > 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
40b4c848b8fcd63b0cb60b9d170c3a77?d=identicon&s=25 Valentin V. Bartenev (Guest)
on 2013-10-02 20:23
(Received via mailing list)
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
35c5f4b67fb34acfa1fe1e7beb869b2d?d=identicon&s=25 Grant (Guest)
on 2013-10-02 21:03
(Received via mailing list)
>> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.