# Nested Locations Better?

**URL:** <https://www.ruby-forum.com/t/nested-locations-better/203100>\
**Category:** NGINX\
**Created:** [February 12, 2011, 8:35pm UTC](https://www.ruby-forum.com/t/nested-locations-better/203100 "2011-02-12T20:35:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 12, 2011, 8:35pm UTC](https://www.ruby-forum.com/t/nested-locations-better/203100/1 "2011-02-12T20:35:38Z")

</div>

Just noticed that in Igor’s response to an earlier query of mine on a  
separate issue, he took code I had shown effectively as …

```auto
location /set/subset/ {
  abc;
}
location /set/ {
  xyz;
}
```

… and wrote it like this in his response …

```auto
location /set/ {
  location /set/subset/ {
    abc;
  }
  xyz;
}
```

I had put the first location block on top so that it would be processed  
since both would match but now wondering if the nested version is more  
efficient.

I had been under the impression that nested locations were to be avoided  
but seeing Igor writting the code in that manner got me wondering.

Is it better to have them all independent as I had originally shown were  
Nginx would match against the two in some cases and then choose the  
closest match out of the two, or the second, nested version where it  
will always match the broader location and then sometimes also match the  
tighter location inside?

Also, how many layers deep can this go if so?

Thanks

Posted at Nginx Forum:

> **[Nested Locations Better???](https://forum.nginx.org/read.php?2,174517,174517#msg-174517)**
>
> Just noticed that in Igor's response to an earlier query of mine on a separate issue, he took code I had shown effectively as ... \[code\] location /set/subset/ { abc; } location /set/ { xyz; } \[/code\] ... and wrote it li

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [February 12, 2011, 9:21pm UTC](https://www.ruby-forum.com/t/nested-locations-better/203100/2 "2011-02-12T21:21:22Z")

</div>

On Sat, Feb 12, 2011 at 02:33:31PM -0500, Dayo wrote:

> [/code]
> 
> will always match the broader location and then sometimes also match the  
> tighter location inside?

If you use only locations without regexes, then you may not use nested  
locations. nginx uses some kind of binary tree to match locations, so

location /set/subset/ { }  
location /set/ { }

are slightly faster than nested locations. BTW you may write

location /set/ { }  
location /set/subset/ { }

or

location /set/subset/ { }  
location /set/ { }

- there is no difference for nginx: it finds the longest match using  
the tree. The nested locations are better if you had to use regex  
locations. Then I usually isolate regex location inside usual location:

location /dir/ {  
location ~ ^/dir/(.+)$ {  
…  
}  
}

> Also, how many layers deep can this go if so?

No limit, except memory, etc.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [February 12, 2011, 9:48pm UTC](https://www.ruby-forum.com/t/nested-locations-better/203100/3 "2011-02-12T21:48:54Z")

</div>

## Igor S. Wrote:

> the tree.  
> Understood. Thanks. I’ll continue to order them as …

location /set/subset/ { }  
location /set/ { }

… just to maintain consistency with what I have done before.

> The nested locations are better if you  
> had to use regex  
> locations. Then I usually isolate regex location  
> inside usual location:
> 
> location /dir/ {  
> location ~ ^/dir/(.+)$ {  
> …  
> }  
> }

Got it. The example I was referring to had …

location ~ subset { }  
location ~ set { }

… so I will nest them as per your indication in the original response

> > Also, how many layers deep can this go if so?
> 
> No limit, except memory, etc.

Good to know. I don’t expect to go deep than one layer anyway.

Thanks for the clarification.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,174517,174538#msg-174538](http://forum.nginx.org/read.php?2,174517,174538#msg-174538)

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [February 12, 2011, 10:01pm UTC](https://www.ruby-forum.com/t/nested-locations-better/203100/4 "2011-02-12T22:01:26Z")

</div>

Hi Igor.

## Igor S. Wrote:

> The nested locations are better if you  
> had to use regex  
> locations. Then I usually isolate regex location  
> inside usual location:
> 
> location /dir/ {  
> location ~ ^/dir/(.+)$ {  
> …  
> }  
> }

How about the other way round? Non regex nested in regex?

location ~ set {  
location /subset/ {  
…  
}  
}

Not likely to be common but possible. Any views?

Thanks

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,174517,174543#msg-174543](http://forum.nginx.org/read.php?2,174517,174543#msg-174543)

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [February 12, 2011, 10:10pm UTC](https://www.ruby-forum.com/t/nested-locations-better/203100/5 "2011-02-12T22:10:13Z")

</div>

On Sat, Feb 12, 2011 at 04:00:36PM -0500, Dayo wrote:

> > location /dir/ {  
> > }  
> > }
> 
> Not likely to be common but possible. Any views?

You may use it, but do not see any advantage: you still have unisolated  
configuration dependences.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [February 12, 2011, 10:24pm UTC](https://www.ruby-forum.com/t/nested-locations-better/203100/6 "2011-02-12T22:24:00Z")

</div>

## Igor S. Wrote:

> You may use it, but do not see any advantage: you  
> still have unisolated  
> configuration dependences.

I see. Seems to me that there is likely to be limited benefit in nested  
two regex based locations as well and that the main potential advantage  
would be static and then regex. Is this correct?

Now that I have the debug compiled in, I’ll try looking there to see how  
things flow as well.

Thanks.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,174517,174556#msg-174556](http://forum.nginx.org/read.php?2,174517,174556#msg-174556)

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [February 12, 2011, 9:55pm UTC](https://www.ruby-forum.com/t/nested-locations-better/203100/7 "2011-02-12T21:55:48Z")

</div>

On Sat, Feb 12, 2011 at 03:47:22PM -0500, Dayo wrote:

> > are slightly faster than nested locations. BTW you
> > 
> > - there is no difference for nginx: it finds the  
> > longest match using  
> > the tree.
> 
> Understood. Thanks. I’ll continue to order them as …
> 
> location /set/subset/ { }  
> location /set/ { }
> 
> … just to maintain consistency with what I have done before.

Note, that order is important for locations with regex. And this is  
causes configuraiton dependences. This is why I try to avoid regexes  
or try to isolate them as nested locations inside usual locations.

> Got it. The example I was referring to had …
> 
> location ~ subset { }  
> location ~ set { }
> 
> … so I will nest them as per your indication in the original response

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**
