Proxy passing and the URI

Maxim Dounin mdounin at mdounin.ru
Tue Sep 23 12:52:21 MSD 2008


Hello!

On Mon, Sep 22, 2008 at 10:31:02PM -0700, mike wrote:

>I have a request for http://foo.com/sites/something
>
>I have this location block inside of server{} with root /home/foo/web/foo.com
>
>location ^~ /sites {
>   proxy_pass http://10.122.47.82;
>   proxy_set_header Host foo.com;
>}
>
>Trying to pass it to a second server.
>
>The problem is, the second server receives this as the full URI; I
>have to define "root" to be the base URI (/home/foo/web/foo.com) so
>the /sites/something maps to it properly. Is there any way to remove
>parts of the URI when passing via proxy? So the /sites/something/
>isn't needed on the upstream server?

There are two basic options:

1. Use proxy_pass with uri, e.g.

     location ^~ /sites {
         proxy_pass http://10.122.47.82/new;
     }

This will replace '/sites' part (the part that matched location) 
with the '/new' in the uri passed to backend server.  So if you 
write

     location ^~ /sites {
         proxy_pass http://10.122.47.82/;
     }

the '/sites' part will be removed.

See http://wiki.codemongers.com/NginxHttpProxyModule#proxy_pass 
for details.

2. Modify uri as needed with rewrite before proxy_pass.  This less 
efficient but may be used where 1 can't be (e.g. in regex 
locations).  E.g.


     location ~ ^/sites.*\.cgi$ {
         rewrite  ^/sites(.*)  $1  break;
         proxy_pass http://backend;
     }

Maxim Dounin





More information about the nginx mailing list