Correct usage of location context?

I’m trying to shift my nginx instance from a multi-subdomain-setting to
a one-domain-setting. I have the following config in the same server
context:

location /redmine/ {
root /var/www/redmine/public;
passenger_enabled on;
}

location /test/ {
root /var/www/test;
autoindex on;
}

location / {
root /var/www/main;
autoindex on;
}

When I try to access http://example.com/test/testfile, nginx tries to
read the file at /var/www/test/test/testfile instead of at
/var/www/test/testfile. I guess my configuration is not correct; how can
I force nginx to e.g.

serve everything beginning with /redmine/
from /var/www/redmine/public

?

Any help would be appreciated.

would not it be a trailing(redundant) slash in location statement? -
location /test

On Fri, 2009-06-12 at 18:23 +0100, lejeczek wrote:

would not it be a trailing(redundant) slash in location statement? -
location /test

Also his root settings are wrong:

location /test {
root /var/www;
}

Cliff

lejeczek wrote:

would not it be a trailing(redundant) slash in location statement? -
location /test

No.

If you want to map a request to /test/x to /var/www/test/x instead of
/var/www/test/test/x, it sounds like you need a rewrite to remove
“/test”
from the request URI. There may be another way of doing this, but
basically:


location /test {
root /var/www/test;
rewrite ^/test(.*)$ $1;
}

That will get rid of the “duplicate” /test in the final path nginx uses
to
access the filesystem. Also note that the new regular expression
support in
the “location” should make it easy to have a single block (like above)
for
many sites.

Nick

Cliff W. wrote:

On Fri, 2009-06-12 at 18:23 +0100, lejeczek wrote:

would not it be a trailing(redundant) slash in location statement? -
location /test

  1. The trailing slash doesn’t matter, /test looks for either a file or
    directory (that’s what nginx’s error-log says to me at least). It
    shouldn’t be looking for /test though, it should list/process the
    content from the filesystem as set by the root parameter
    (/var/www/test).

Also his root settings are wrong:

location /test {
root /var/www;
}

  1. My root-settings are not wrong. I have many sites in /var/www and
    test should not be mapped to /var/www but to /var/www/test.

With that mapping, I assume that calling http://example.com/test/foobar
should try to access /var/www/test/foobar on the filesystem and not
/var/www/test/test/foobar like it does now.

What I’m trying to achieve is to move away from a vhost-configuration to
a multiple-apps/sites-configurations, e.g.

example.com/railsapp1/* would access /var/www/railsapp1 and below only,
example.com/railsapp2/* would access /var/www/railsapp2 and below only,
example.com/phpapp1/* would access /var/www/phpapp2 and below only,
example.com/ would access /var/www/static_html and below unless below is
one of the above mentioned “virtual” URIs.

PS: It’s “awesome” how slow this mailing list is (I don’t mean the
response time, but the time the system managing this list takes to
process an email and to deliver it out to subscribers).

On Fri, 2009-06-12 at 13:57 -0500, Nick P. wrote:

If you want to map a request to /test/x to /var/www/test/x instead
of /var/www/test/test/x, it sounds like you need a rewrite to remove
“/test” from the request URI.

A rewrite adds needless processing to every request.

location /test/x {
root /var/www;
}

is much simpler and more efficient. All requests that map through this
location will serve files from /var/www/test/x.

Cliff

On Fri, 2009-06-12 at 20:31 +0200, Szymon Polom wrote:

Also his root settings are wrong:

location /test {
root /var/www;
}

  1. My root-settings are not wrong. I have many sites in /var/www and
    test should not be mapped to /var/www but to /var/www/test.

They are wrong:

location /test {
root /var/www/test;
}

This will map the request /test/foo.html to /var/www/test/test/foo.html.
This is probably NOT what you want.

location /test {
root /var/www;
}

This will map the request /test/foo.html to /var/www/test/foo.html.

Whichever one you think is what you want, use. You can think whichever
one you want is wrong, but this is how it works. The $location is
appended to $document_root.

With that mapping, I assume that calling http://example.com/test/foobar
should try to access /var/www/test/foobar on the filesystem and not
/var/www/test/test/foobar like it does now.

Your assumption is incorrect.

What I’m trying to achieve is to move away from a vhost-configuration to
a multiple-apps/sites-configurations, e.g.

example.com/railsapp1/* would access /var/www/railsapp1 and below only,

location /railsapp1 {
root /var/www;
}

Request http://example.com/railsapp1/images/foo.gif would map
to /var/www/railsapp1/images/foo.gif

example.com/railsapp2/* would access /var/www/railsapp2 and below only,

location /railsapp2 {
root /var/www;
}

Request http://example.com/railsapp2/images/foo.gif would map
to /var/www/railsapp2/images/foo.gif

example.com/phpapp1/* would access /var/www/phpapp2 and below only,
example.com/ would access /var/www/static_html and below unless below is
one of the above mentioned “virtual” URIs.

PS: It’s “awesome” how slow this mailing list is (I don’t mean the
response time, but the time the system managing this list takes to
process an email and to deliver it out to subscribers).

I saw the response to your query before I saw your original query. I
have no idea why that is.

Cliff

On Fri, Jun 12, 2009 at 06:05:34PM +0200, Szymon Polom wrote:

root /var/www/test;
/var/www/test/testfile. I guess my configuration is not correct;
As it was already said by Cliff:

location /test/ {
root /var/www;
autoindex on;
}

how can I force nginx to e.g.

serve everything beginning with /redmine/
from /var/www/redmine/public

?

location /redmine/ {
alias /var/www/redmine/public/;
}

Nick P. wrote:

That will get rid of the “duplicate” /test in the final path nginx uses
to access the filesystem. Also note that the new regular expression
support in the “location” should make it easy to have a single block
(like above) for many sites.

Thank you Nick, I tried it before already. It does a redirection for me
and goes stuff which is in

location / { root /var/www/main; }

instead of in /var/www/test.

I’m trying to shift my nginx instance from a multi-subdomain-setting
to a one-domain-setting. I have the following config in the same
server context:

location /redmine/ {
root /var/www/redmine/public;
passenger_enabled on;
}

To deploy passanger app onto a sub uri then you might wish to look
into this
http://modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rails_to_sub_uri

Igor S. wrote:

On Fri, Jun 12, 2009 at 06:05:34PM +0200, Szymon Polom wrote:
As it was already said by Cliff:

[…]

As someone on #nginx pointed out I was actually looking for location +
alias not for location + root.

On Jun 13, 2009, at 5:53, Igor S. [email protected] wrote:

location +
location /test/ {
root /var/www;
}

is just the some, but more efficient.

This is almost correct except that the OP forgot one important detail
which I got from him on IRC last night. ‘test’ is not a directory on
his filesystem.

Thus, requests for /var/www/test would fail with ‘No such file or
directory’.

This is why think he needs an alias :slight_smile:

On Fri, Jun 12, 2009 at 11:01:57PM +0200, Szymon Polom wrote:

Igor S. wrote:

On Fri, Jun 12, 2009 at 06:05:34PM +0200, Szymon Polom wrote:
As it was already said by Cliff:

[…]

As someone on #nginx pointed out I was actually looking for location +
alias not for location + root.

You should not use “alias” there, where “root” is enough. If you use

location /test/ {
alias /var/www/test/;
}

then

location /test/ {
root /var/www;
}

is just the some, but more efficient.

Avleen Vig wrote:

This is almost correct except that the OP forgot one important detail
which I got from him on IRC last night. ‘test’ is not a directory on his
filesystem.

Thus, requests for /var/www/test would fail with ‘No such file or
directory’.

This is why think he needs an alias :slight_smile:

Yeah, I’m sorry for the confusion, I need all use cases (root, alias and
base url for some additional modules).