Proxy passing and the URI

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


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

>I have a request for
>I have this location block inside of server{} with root /home/foo/web/
>location ^~ /sites {
>   proxy_pass;
>   proxy_set_header Host;
>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/ 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 {

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

     location ^~ /sites {

the '/sites' part will be removed.

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