# Nginx location+proxy\_pass?

**URL:** <https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703>\
**Category:** NGINX\
**Created:** [October 19, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703 "2007-10-19T15:29:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 19, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/1 "2007-10-19T15:29:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 19, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/2 "2007-10-19T15:39:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 19, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/3 "2007-10-19T16:15:25Z")

</div>

On 19/10/2007, Roxis [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/4 "2007-10-19T16:30:42Z")

</div>

location /app1/ {  
proxy\_pass http://mongrel\_cluster1/;  
}

location /app2/ {  
proxy\_pass http://mongrel\_cluster2/;  
}

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 19, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/5 "2007-10-19T16:46:13Z")

</div>

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

Cheers

David

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 19, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/6 "2007-10-19T16:55:04Z")

</div>

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/;
}

```

}

}

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 19, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/7 "2007-10-19T17:12:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 1, 2008, 1:49pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/8 "2008-07-01T13:49:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [June 23, 2014, 11:41pm UTC](https://www.ruby-forum.com/t/nginx-location-proxy-pass/117703/9 "2014-06-23T23:41:45Z")

</div>

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 removed\_email\_address@domain.invalid 😉  
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!

```auto

```
