Nginx location+proxy_pass?

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

On Friday 19 October 2007, David S. 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

On 19/10/2007, Roxis [email protected] 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

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

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

Nope, same error message when nginx tries to parse the config. But, as
I said, removing the ‘permanent’ part does the trick.

Cheers

David

On Friday 19 October 2007, David S. 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/;
}

}

}

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

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 S. 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

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 [email protected] :wink:
I’m here to stay.

Just don’t press + look around coz it’s hard with all the
technology today…

That’s it! TAK TAK!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs