# Passing request directly to named location

**URL:** <https://www.ruby-forum.com/t/passing-request-directly-to-named-location/203054>\
**Category:** NGINX\
**Created:** [February 11, 2011, 11:10am UTC](https://www.ruby-forum.com/t/passing-request-directly-to-named-location/203054 "2011-02-11T11:10:33Z")\
**Posts on this page:** 6\
**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 11, 2011, 11:10am UTC](https://www.ruby-forum.com/t/passing-request-directly-to-named-location/203054/1 "2011-02-11T11:10:33Z")

</div>

Hello all.

I currently have my php set up as so

```auto
...
  location ~ \..*/.*\.php$ {
    return 403;
  }
  location ~* ^.+\.php$ {
    try_files $uri/fail @proxy;
  }
  location @proxy {
    proxy pass etc
  }
  location
some-location-that-sometimes-needs-rewriting-in-apache-htaccess {
    try_files $uri $uri/ @proxy;
  }
...
```

Is there a way where when a ‘.php’ file is equested, I just pass the  
request to the ‘@proxy’ location.

Now I have to use ‘$uri/fail’ (a location that will never be found) so  
that it will fail and then go to the ‘@proxy’ location.

I see the time spent determining that ‘$uri/fail’ does not exist to be a  
waste but I can’t do ‘try\_files @proxy’ directly.

An option at also works is …

```auto
...
  location ~* ^.+\.php$ {
    error_page 403 = @proxy;
    return 403;
  }
...
```

Just wondering which is more efficient.

Thanks

Posted at Nginx Forum:

> **[Passing request directly to named location](https://forum.nginx.org/read.php?2,174090,174090#msg-174090)**
>
> Hello all. I currently have my php set up as so \[code\] ... location ~ \\..\*/.\*\\.php$ { return 403; } location ~\* ^.+\\.php$ { try\_files $uri/fail @proxy; } location @proxy { proxy pass etc }

---

<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 11, 2011, 11:25am UTC](https://www.ruby-forum.com/t/passing-request-directly-to-named-location/203054/2 "2011-02-11T11:25:39Z")

</div>

On Fri, Feb 11, 2011 at 05:08:40AM -0500, Dayo wrote:

> ```
> try_files $uri/fail @proxy;
> 
> ```
> 
> ```auto
> ...
> location ~* ^.+\.php$ {
> error_page 403 = @proxy;
> return 403;
> }
> ...
> ```
> 
> Just wondering which is more efficient.

The one and single efficient and scaleable way is to define explictly  
how do you want to handle location, that is:

location ~\* ^.+.php$ {  
# BTW, what does this hack mean ???  
location ~ .._/._.php$ {  
return 403;  
}

```
 proxy_pass ...

```

}

The lesser you have dependences, the lesser you will have maintence  
issues in future. Yes, you have to write more, but you see what  
how exactly this location is handled. If you ever need to change  
proxied server address, your favourite editor with find/replace  
functionality is your friend.

–  
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 11, 2011, 12:39pm UTC](https://www.ruby-forum.com/t/passing-request-directly-to-named-location/203054/3 "2011-02-11T12:39:45Z")

</div>

> one place and not run the risk of missing stuff out.  
> I heard this “centralise” mantra many times. Believe me, it works until  
> your configuration fits on screen or two. When it grows, every, note,  
> EVERY configuration modification becames investigation of  
> configuration dependences. I have an configuraiton with 180 locaitons.  
> When I need to add some functionality, I simply add location and  
> that is all. I do not look through whole 90K file.

Why ? Because I

1. use only locations without regex.
2. do not use rewrite’s at all.

If I need to change a proxied server name or something else, I use  
simple  
find/replace in VIM. This is very easy, it’s much easy than  
investigation  
of configuration dependences on every change.

> So in terms of the try files and error page options, can you advise  
> which is better … taking note of what you said about doing each proxy  
> pass every time?

error\_page is faster because it does not make syscall.

–  
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 11, 2011, 1:09pm UTC](https://www.ruby-forum.com/t/passing-request-directly-to-named-location/203054/4 "2011-02-11T13:09:48Z")

</div>

## Igor S. Wrote:

> that is all. I do not look through whole 90K file.  
> of configuration dependences on every change.  
> Noted. I used to have them like you have but with time, I have found  
> having stuff centralised more convenient for me.  
> That is the great thing about this little webserver you have written …  
> it allows tons of options and with agentzh and the other fellas in  
> Shanghai coming out with all sorts of crazy modules, it is just mouth  
> watering.

Would be nice if some of the stuff coming out of Shanghai could be  
pushed into the core.

BTW, please consider ‘proxy\_no\_cache\_null’ and ‘proxy\_cache\_bypass\_null’  
options to complement the ‘proxy\_no\_cache’ and ‘proxy\_cache\_bypass’  
options. That is, they should not cache / bypass cache for requests  
where the passed variables are null. I use the current options to bypass  
cache for logged in users after checking a cookie but it is difficult to  
bypass cache based on absence of cookie.

Anyway, this is deviating and I managed to use the lua module from  
agentzh (with his help) to do this.

> > So in terms of the try files and error page  
> > options, can you advise  
> > which is better … taking note of what you said  
> > about doing each proxy  
> > pass every time?
> 
> error\_page is faster because it does not make  
> syscall.

Thanks. I thought that might be the case.

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

---

<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 11, 2011, 12:14pm UTC](https://www.ruby-forum.com/t/passing-request-directly-to-named-location/203054/5 "2011-02-11T12:14:38Z")

</div>

## Igor S. Wrote:

> > }  
> > try\_files $uri $uri/ @proxy;  
> > that it will fail and then go to the ‘@proxy’  
> > …  
> > to define explictly  
> > how do you want to handle location, that is:
> 
> location ~\* ^.+.php$ {  
> # BTW, what does this hack mean ???  
> location ~ .._/._.php$ {  
> return 403;  
> }  
> Got it from here:  
> [http://forum.nginx.org/read.php?2,88845,88854#msg-88854](http://forum.nginx.org/read.php?2,88845,88854#msg-88854)  
> Not using fastcgi at present but wanted to stop any ‘.jpg/.php’ type  
> attempts  
> proxied server address, your favourite editor with  
> find/replace  
> functionality is your friend.  
> Thanks but I prefer to centralise things so that I only have to edit in  
> one place and not run the risk of missing stuff out.  
> So in terms of the try files and error page options, can you advise  
> which is better … taking note of what you said about doing each proxy  
> pass every time?

Thanks

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

---

<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 13, 2011, 1:52pm UTC](https://www.ruby-forum.com/t/passing-request-directly-to-named-location/203054/6 "2011-02-13T13:52:12Z")

</div>

## Dayo Wrote:

> That is the great thing about this little  
> webserver you have written … it allows tons of  
> options and with agentzh and the other fellas in  
> Shanghai coming out with all sorts of crazy  
> modules, it is just mouth watering.
> 
> Would be nice if some of the stuff coming out of  
> Shanghai could be pushed into the core.

Looks like someone is on my wavelength.

> **[GitHub - ezmobius/super-nginx: killer build of nginx \*and\* luajit-beta6 plus 21...](https://github.com/ezmobius/super-nginx)**
>
> killer build of nginx \*and\* luajit-beta6 plus 21 other modules

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