Forum: NGINX Rewrite URI and pass request to the upstream server

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ruben D. (Guest)
on 2009-03-17 19:20
(Received via mailing list)
Hi friends, I need rewrite and old URI and pass the request to the
upstream
server, here is my current config file:
http://pastie.org/418871

If for example I have http://pe.domain.com/xx-xxx-689 I need redirect to
http://cl.domain.com/xx-xxx-689 and let the upstream process the new
request, with my current config I've obtained the rewrite of the URI but
nginx return me a 404 error, therefore it is not transfering the request
to
the upstream server because the 404 error page are different between
nginx
and my application.

Regards!
mike (Guest)
on 2009-03-17 19:30
(Received via mailing list)
On Tue, Mar 17, 2009 at 10:09 AM, Ruben. D. 
<removed_email_address@domain.invalid>
wrote:
> Hi friends, I need rewrite and old URI and pass the request to the upstream
> server, here is my current config file:
> http://pastie.org/418871
>
> If for example I have http://pe.domain.com/xx-xxx-689 I need redirect to
> http://cl.domain.com/xx-xxx-689 and let the upstream process the new
> request, with my current config I've obtained the rewrite of the URI but
> nginx return me a 404 error, therefore it is not transfering the request to
> the upstream server because the 404 error page are different between nginx
> and my application.

This is a wild shot but you might try

- proxy_pass http://mongrel;
+ proxy_pass http://mongrel/;

I've never used named upstream locations but the ending slash does
have a different effect when passing HTTP to a backend. I believe it
maintains the /uri/ request as is, instead of winding up being
/uri/uri duplicated (I think is the easiest way to explain it?)

Also if you only have one mongrels server you could probably just do

proxy_pass http://127.0.0.1:3000/;

If you don't plan on adding anymore in the future...
Maxim D. (Guest)
on 2009-03-17 19:46
(Received via mailing list)
Hello!

On Tue, Mar 17, 2009 at 12:09:29PM -0500, Ruben. D. wrote:

> Hi friends, I need rewrite and old URI and pass the request to the upstream
> server, here is my current config file:
> http://pastie.org/418871
>
> If for example I have http://pe.domain.com/xx-xxx-689 I need redirect to
> http://cl.domain.com/xx-xxx-689 and let the upstream process the new
> request, with my current config I've obtained the rewrite of the URI but
> nginx return me a 404 error, therefore it is not transfering the request to
> the upstream server because the 404 error page are different between nginx
> and my application.

According to your config, [new] request to cl.domain.com/xx-xxx-689
will end up in "location ~* [a-zA-Z]+(\-)+689$", which has no proxy_pass
rules.  You should either duplicate proxy_pass rules within this
location, or
(better, faster, more scalable) separate pe.* and cl.* servers:

server {
    listen       80;
    server_name  pe.domain.com;

    location ~* [a-zA-Z]+(\-)+689$ {
        rewrite  ^  http://cl.domain.com$request_uri?;
    }

    ...
}

server {
    listen       80;
    server_name  www.domain.com domain.com cl.domain.com;

    ...
}

For identical config chunks within this servers you may want to
use include.

Maxim D.
Maxim D. (Guest)
on 2009-03-17 19:54
(Received via mailing list)
Hello!

On Tue, Mar 17, 2009 at 10:19:58AM -0700, mike wrote:

> > and my application.
>
> This is a wild shot but you might try
>
> - proxy_pass http://mongrel;
> + proxy_pass http://mongrel/;
>
> I've never used named upstream locations but the ending slash does
> have a different effect when passing HTTP to a backend. I believe it
> maintains the /uri/ request as is, instead of winding up being
> /uri/uri duplicated (I think is the easiest way to explain it?)

No.

Form of proxy_pass with uri component (proxy_pass http://mongrel/)
changes matched location prefix to this uri component.  So, for

    location /blah/ {
        proxy_pass http://backend/boom/;
    }

and request "/blah/something" your backend will see
"/boom/something".

Without uri component (proxy_pass http://mongrel) uri will be
transfered to backend as is, i.e. for

    location /blah/ {
        proxy_pass http://backend;
    }

and request "/blah/something" your backend will see
"/blah/something".

For location / as you may see from the above form doesn't matter,
and

    proxy_pass http://mongrel;
    proxy_pass http://mongrel/;

are equivalent (while the second one will eat a bit more CPU).

Maxim D.
Ruben D. (Guest)
on 2009-03-18 00:32
(Received via mailing list)
Maxim, very thanks for your time, I've added another proxy_pass rule and
it
works fine ;)

mike: very thanks too!

Regards.
mike (Guest)
on 2009-03-18 00:35
(Received via mailing list)
On Tue, Mar 17, 2009 at 2:51 PM, Ruben. D. 
<removed_email_address@domain.invalid>
wrote:
> Maxim, very thanks for your time, I've added another proxy_pass rule and it
> works fine ;)
>
> mike: very thanks too!

yeah, too bad i was totally backwards there. :)
This topic is locked and can not be replied to.