# Another "nested locations" question

**URL:** <https://www.ruby-forum.com/t/another-nested-locations-question/223214>\
**Category:** NGINX\
**Created:** [September 26, 2012, 4:06am UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214 "2012-09-26T04:06:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 26, 2012, 4:06am UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214/1 "2012-09-26T04:06:28Z")

</div>

I am interested in using a nesting of some sort so that I don’t have to  
duplicate all the proxy- and other directives for one “special case”.

Basically, I’d like a very small subset of my webapp to also write to a  
separate access-log. When a user hits that particular page, I would  
like to log it, AND also perform all the directives for the rest of the  
site that are already configured.

Something like

location /site {  
location /site/search {  
access\_log /var/log/nginx/search\_access.log;  
}  
proxy\_pass …  
proxy\_redirect off;  
proxy\_set\_header …  
}

So accessing /site/search should write an entry in the “special log  
file”, and then do all the “normal stuff” that location /site has  
itemized.

Is this possible?

Thanks again,  
AJ

---

<div class="post-metadata">

**Author:** ![AJ\_Weber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@AJ\_Weber](https://www.ruby-forum.com/u/AJ_Weber)\
**Post date:** [September 26, 2012, 5:44pm UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214/2 "2012-09-26T17:44:29Z")

</div>

Hello!

On Tue, Sep 25, 2012 at 10:05:44PM -0400, AJ Weber wrote:

> file", and then do all the “normal stuff” that location /site has  
> itemized.
> 
> Is this possible?

Not exactly. Each location is expected to have it’s own “do  
something” list, i.e. if you need proxy\_pass in this location -  
you have to write it explicitly.

On the other hand, there is no need to duplicate normal  
configuration, e.g. proxy\_redirect and proxy\_set\_header will be  
inherited from previous level(s). I.e. you have to write  
explicitly only few directives which aren’t inherited, notably  
try\_files, rewrite module directives, and content handlers like  
proxy\_pass.

In the above config snippet you have to duplicate proxy\_pass into  
location /site/search.

–  
Maxim D.

> **[Technical Support for NGINX and NGINX Plus Software](https://www.nginx.com/support/)**
>
> Get the help you need with support directly from the authors and maintainers of NGINX Open Source and NGINX Plus.

---

<div class="post-metadata">

**Author:** ![AJ\_Weber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@AJ\_Weber](https://www.ruby-forum.com/u/AJ_Weber)\
**Post date:** [September 26, 2012, 9:01pm UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214/3 "2012-09-26T21:01:23Z")

</div>

OK, I am positive this is easy for you experienced nginx users!

I have a backend app server setup and am using nginx for  
caching/proxy/ssl-termination.

I would like to use the “default” server (listening on 443) to redirect  
the url [https://host/monit](https://host/monit) to the server’s monit-mini-http server (and  
continue to use it for ssl termination). so I need to direct the  
backend to [http://localhost:2812/](http://localhost:2812/).

Thus, I think I need a proxy\_pass and a rewrite (to basically just  
remove “/monit” from the url (pass the remainder of the url back to  
monit’s http server from root). I was trying to do this inside a  
location /monit { }.

But, of course, I can’t get it to work. ☹

Any hints?

---

<div class="post-metadata">

**Author:** ![AJ\_Weber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@AJ\_Weber](https://www.ruby-forum.com/u/AJ_Weber)\
**Post date:** [September 27, 2012, 12:03am UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214/4 "2012-09-27T00:03:11Z")

</div>

On 26 Set 2012 23h56 CEST, removed\_email\_address@domain.invalid wrote:

> > direct the backend to [http://localhost:2812/](http://localhost:2812/).
> 
> Very basic. No rewrites.
> 
> location ^~ /monit {  
> location ~\* ^/monit/(?\<monit\_request\_uri\>.\*)$ {  
> proxy\_pass [http://127.0.0.1:2812/$monit\_request\_uri](http://127.0.0.1:2812/$monit_request_uri);  
> proxy\_set\_header Host $host;  
> }

Err, make that:

location ^~ /monit {  
location ~\* ^/monit(?\<monit\_request\_uri\>.\*)$ {  
proxy\_pass [http://127.0.0.1:2812/$monit\_request\_uri](http://127.0.0.1:2812/$monit_request_uri);  
proxy\_set\_header Host $host;  
}

This way [https://myserver/monit](https://myserver/monit) won’t give a 404.

— appa

---

<div class="post-metadata">

**Author:** ![AJ\_Weber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@AJ\_Weber](https://www.ruby-forum.com/u/AJ_Weber)\
**Post date:** [September 27, 2012, 9:39am UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214/5 "2012-09-27T09:39:49Z")

</div>

On Thu, Sep 27, 2012 at 12:02:35AM +0200, Antnio P. P. Almeida wrote:

> > > redirect the url [https://host/monit](https://host/monit) to the server’s monit-mini-http  
> > > }
> 
> Err, make that:
> 
> location ^~ /monit {  
> location ~\* ^/monit(?\<monit\_request\_uri\>.\*)$ {  
> proxy\_pass [http://127.0.0.1:2812/$monit\_request\_uri](http://127.0.0.1:2812/$monit_request_uri);  
> proxy\_set\_header Host $host;  
> }
> 
> This way [https://myserver/monit](https://myserver/monit) won’t give a 404.

The regex is not required:

location ^~ /monit/ {  
proxy\_pass [http://127.0.0.1:2812/](http://127.0.0.1:2812/);  
proxy\_set\_header Host $host;  
}

Requests to “/monit” will be redirected to “/monit/” automatically.

–  
Igor S.

> **[Technical Support for NGINX and NGINX Plus Software](https://www.nginx.com/support/)**
>
> Get the help you need with support directly from the authors and maintainers of NGINX Open Source and NGINX Plus.

---

<div class="post-metadata">

**Author:** ![AJ\_Weber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@AJ\_Weber](https://www.ruby-forum.com/u/AJ_Weber)\
**Post date:** [September 27, 2012, 3:07pm UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214/6 "2012-09-27T15:07:01Z")

</div>

So this probably will work if I have the access\_log AND include a copy  
of the proxy\_pass directives in the nested location. The  
proxy\_set\_header, proxy\_cache…, etc. would all be inherited  
automatically in the nested location, so I can leave those in the  
parent?

---

<div class="post-metadata">

**Author:** ![AJ\_Weber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@AJ\_Weber](https://www.ruby-forum.com/u/AJ_Weber)\
**Post date:** [September 26, 2012, 11:56pm UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214/7 "2012-09-26T23:56:45Z")

</div>

On 26 Set 2012 21h00 CEST, removed\_email\_address@domain.invalid wrote:

> OK, I am positive this is easy for you experienced nginx users!
> 
> I have a backend app server setup and am using nginx for  
> caching/proxy/ssl-termination.
> 
> I would like to use the “default” server (listening on 443) to  
> redirect the url [https://host/monit](https://host/monit) to the server’s monit-mini-http  
> server (and continue to use it for ssl termination). so I need to  
> direct the backend to [http://localhost:2812/](http://localhost:2812/).

Very basic. No rewrites.

location ^~ /monit {  
location ~\* ^/monit/(?\<monit\_request\_uri\>.\*)$ {  
proxy\_pass [http://127.0.0.1:2812/$monit\_request\_uri](http://127.0.0.1:2812/$monit_request_uri);  
proxy\_set\_header Host $host;  
}

Feel free to add other headers to your liking.

— appa

---

<div class="post-metadata">

**Author:** ![AJ\_Weber](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@AJ\_Weber](https://www.ruby-forum.com/u/AJ_Weber)\
**Post date:** [September 27, 2012, 3:41pm UTC](https://www.ruby-forum.com/t/another-nested-locations-question/223214/8 "2012-09-27T15:41:31Z")

</div>

Hello!

On Thu, Sep 27, 2012 at 09:05:47AM -0400, AJ Weber wrote:

> So this probably will work if I have the access\_log AND include a  
> copy of the proxy\_pass directives in the nested location. The  
> proxy\_set\_header, proxy\_cache…, etc. would all be inherited  
> automatically in the nested location, so I can leave those in the  
> parent?

Yes. You actually can leave those on server or even http level,  
and these directives will be inherited from there as well.

> > > ```
> > > }
> > > 
> > > ```
> > 
> > Not exactly. Each location is expected to have it’s own "do  
> > In the above config snippet you have to duplicate proxy\_pass into  
> > location /site/search.
> 
> * * *
> 
> nginx mailing list  
> removed\_email\_address@domain.invalid  
> [nginx Info Page](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Maxim D.

> **[Technical Support for NGINX and NGINX Plus Software](https://www.nginx.com/support/)**
>
> Get the help you need with support directly from the authors and maintainers of NGINX Open Source and NGINX Plus.
