"root" directive weirdness

Marcus Clyne ngx.eugaia at gmail.com
Fri Jan 22 03:45:46 MSK 2010


Hi,

Cliff Wells wrote:
> On Thu, 2010-01-21 at 22:10 +0100, Dennis J. wrote:
>   
>> Hi,
>> I'm trying to configure a vhost where in the default case I want it to get 
>> the files from /opt/nginx/html but when the uri starts with "/users/" I 
>> want to to deliver the files from /web/users/ instead:
>>
>>          location / {
>>              root   /opt/nginx/html;
>>              index  index.html index.htm;
>>          }
>>
>>          location ~* ^/users/ {
>>              root   /web/users;
>>              rewrite ^/users(/.*) $1 last;
>>          }
>>     
The reason why you're having problems is the 'last' tag on the rewrite, 
which will end the rewrite rules and then look for the corresponding URL 
(which sends it back to the first location).  Using 'break' instead 
would prevent this, and do as you expect.  However, for this situation, 
the second regex is an unnecessary waste of processing.

Cliff's solution is better :
>
> location / {
>     root /opt/nginx/html;
>     index index.html index.htm;
> }
>
> location /users {
>     root /web;
> }
>
>   
but using

location ^~ /users/ {
    root   /web;
}

is more efficient than using

location /users {
    root /web;
}

if you have any other location directives (especially regex ones) - see 
http://wiki.nginx.org/NginxHttpCoreModule#location to find out why.

Also, if you don't have the final / on the location (i.e. /users/ 
instead of /users), you may get unwanted matching urls (e.g. 
/usersfiles) - depending on your URL hierarchy.

Marcus.



More information about the nginx mailing list