Forum: NGINX nginx location+proxy_pass?

13ee02bb9012e843dbe7d0a23a6656f7?d=identicon&s=25 David Salgado (digitalronin)
on 2007-10-19 15:29
(Received via mailing list)
Hi All

I'm trying to move a bunch of rails apps from an apache/fastcgi
platform to nginx/mongrel_cluster.

What I want is something like this;

    http://my.server/app1 --> mongrel_cluster1

    http://my.server/app2 --> mongrel_cluster2

I can do this in nginx with something like this;
    ...
    upstream mongrel_cluster1 {
      server 127.0.0.1:3001;
      server 127.0.0.1:3002;
    }

    upstream mongrel_cluster2 {
      server 127.0.0.1:4001;
      server 127.0.0.1:4002;
    }

  server {
    ...
    location /app1 {
      proxy_pass http://mongrel_cluster1;
      break;
    }

    location /app2 {
      proxy_pass http://mongrel_cluster2;
      break;
    }

   ...

But, the problem is that I'm now hitting my rails apps with paths like
this;

    /app1/controller/method

    /app2/controller/method

...when the apps want the paths to be just /controller/method

So, I want to use something like this to remove the 'app1' part;

   rewrite  ^/app1/(.*)$  /$1  permanent;

But, that seems to override the proxy_pass directive, because if I put
that in my location blocks, I just get 404 errors, and it's not
allowed in my upstream blocks.

I know I could use virtual hosts, with a different subdomain for each
app, but that would be this;

    http://app1.my.server --> mongrel_cluster1

    http://app2.my.server --> mongrel_cluster2

...which is not what I want.

Is there any way to achieve what I want, using nginx?

Thanks in advance for any help.

David
B06eeb3c664683b78f1bd62a80cd8ca4?d=identicon&s=25 Roxis (Guest)
on 2007-10-19 15:39
(Received via mailing list)
On Friday 19 October 2007, David Salgado wrote:
>
>     }
>       break;
>
> I know I could use virtual hosts, with a different subdomain for each
> Thanks in advance for any help.
>
> David

proxy_pass http://mongrel_cluster1/app1
13ee02bb9012e843dbe7d0a23a6656f7?d=identicon&s=25 David Salgado (digitalronin)
on 2007-10-19 16:15
(Received via mailing list)
On 19/10/2007, Roxis <roxis@list.ru> wrote:
>
> proxy_pass http://mongrel_cluster1/app1
>

Thanks, but that's not quite what I want. I'm trying to remove the
'app1' part before the request gets to the mongrel cluster. Also, the
line above results in the error;

    "proxy_pass" may not have URI part in location given by regular
expression

Actually, I think I've solved the initial problem. Instead of;

  rewrite  ^/app1/(.*)$  /$1  permanent;

...it should be;

  rewrite  ^/app1/(.*)$  /$1;

This does what I want - rewrites the URL but continues processing so
that it hits the proxy_pass directive on the next line.

Thanks again

David
B06eeb3c664683b78f1bd62a80cd8ca4?d=identicon&s=25 Roxis (Guest)
on 2007-10-19 16:30
(Received via mailing list)
location /app1/ {
    proxy_pass http://mongrel_cluster1/;
}

location /app2/ {
    proxy_pass http://mongrel_cluster2/;
}
13ee02bb9012e843dbe7d0a23a6656f7?d=identicon&s=25 David Salgado (digitalronin)
on 2007-10-19 16:46
(Received via mailing list)
Nope, same error message when nginx tries to parse the config. But, as
I said, removing the 'permanent' part does the trick.

Cheers

David
B06eeb3c664683b78f1bd62a80cd8ca4?d=identicon&s=25 Roxis (Guest)
on 2007-10-19 16:55
(Received via mailing list)
On Friday 19 October 2007, David Salgado wrote:
> Nope, same error message when nginx tries to parse the config. But, as
> I said, removing the 'permanent' part does the trick.
>
> Cheers
>
> David

error message says that you are using regular expression location

my nginx version: 0.6.13
and here is my full working nginx.conf:

user www;
worker_processes 1;

events {
    worker_connections 1024;
}

http {

include mime.types;

upstream mongrel_cluster1 {
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
}

upstream mongrel_cluster2 {
    server 127.0.0.1:4001;
    server 127.0.0.1:4002;
}

server {
    listen 127.0.0.1:80 default deferred;

    location /app1/ {
        proxy_pass http://mongrel_cluster1/;
    }

    location /app2/ {
        proxy_pass http://mongrel_cluster2/;
    }
}

}
13ee02bb9012e843dbe7d0a23a6656f7?d=identicon&s=25 David Salgado (digitalronin)
on 2007-10-19 17:12
(Received via mailing list)
You're absolutely right - your config works perfectly for me too.

I must have screwed up with some of the other stuff in my nginx.conf.

Thanks for your help

David
15bf61332913bfe999465ca290370304?d=identicon&s=25 Jesse Cai (caiwangqin)
on 2008-07-01 13:49
Yep, this config file works find in Rails 1.2.x, but not work for Rails
2.1. do you know how to make it works for Rails 2.1?

David Salgado wrote:
> You're absolutely right - your config works perfectly for me too.
>
> I must have screwed up with some of the other stuff in my nginx.conf.
>
> Thanks for your help
>
> David
Dfc75d21a3afff083c35214a1dca6454?d=identicon&s=25 Amir Eldor (amir_e54)
on 2014-06-23 23:41
Hi guys :),

Back then in 1987 I think (when I was born) I think that I heard some
dutch.  Really helped me today when I grew older and 'm 27 (in August).

Find me on amir@amir-x.com ;)
I'm here to stay.

Just don't press <Ctrl><Z> + look around coz it's hard with all the
technology today...

That's it! TAK TAK!

 ~~~~ Amir Eldor
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.