# Nginx for proxy + rewrite

**URL:** <https://www.ruby-forum.com/t/nginx-for-proxy-rewrite/130373>\
**Category:** NGINX\
**Created:** [February 27, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/nginx-for-proxy-rewrite/130373 "2008-02-27T19:40:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavel\_Georgiev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5e925/32.png) [@Pavel\_Georgiev](https://www.ruby-forum.com/u/Pavel_Georgiev)\
**Post date:** [February 27, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/nginx-for-proxy-rewrite/130373/1 "2008-02-27T19:40:21Z")

</div>

Hi, List!

I`ve beed using nxingx as a local balancer for few backend servers:

http {  
upstream [mydomain.com](http://mydomain.com) {  
server 192.168.8.30; # backend server  
}  
server {  
listen 192.168.10.1:8080;  
server\_name [cmydomain.com](http://cmydomain.com);

```
  location / {
     proxy_pass http://mydomain.com;
     proxy_redirect off;
     proxy_set_header Host $host;
  }

```

}  
}

What I need to do is for a certain url to rewrite it to an external url  
but  
serve the requests as a proxy instead of returing a redirect, so this is  
transparent to the client:

[http://mydomain.com/redirect/(.\*)$](http://mydomain.com/redirect/(.*)$) should go to  
[http://extranal.comain.com/$1](http://extranal.comain.com/$1)

I saw this is possible with a simple rewrite but it returns a redirect  
to the  
client. Is is possible to make nginx to server the rewrite as a proxy?

---

<div class="post-metadata">

**Author:** ![Pavel\_Georgiev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5e925/32.png) [@Pavel\_Georgiev](https://www.ruby-forum.com/u/Pavel_Georgiev)\
**Post date:** [February 27, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/nginx-for-proxy-rewrite/130373/2 "2008-02-27T20:07:36Z")

</div>

On Wed, Feb 27, 2008 at 08:30:47PM +0200, Pavel Georgiev wrote:

> serve the requests as a proxy instead of returing a redirect, so this is  
> transparent to the client:
> 
> [http://mydomain.com/redirect/(.\*)$](http://mydomain.com/redirect/(.*)$) should go to [http://extranal.comain.com/$1](http://extranal.comain.com/$1)
> 
> I saw this is possible with a simple rewrite but it returns a redirect to the  
> client. Is is possible to make nginx to server the rewrite as a proxy?

I’m not sure that understand your problem, but this may help:

```
   proxy_pass http://mydomain.com;

   proxy_redirect http://mydomain.com/ /;
   proxy_redirect http://mydomain.com/redirect/ /;

```

---

<div class="post-metadata">

**Author:** ![Pavel\_Georgiev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5e925/32.png) [@Pavel\_Georgiev](https://www.ruby-forum.com/u/Pavel_Georgiev)\
**Post date:** [February 27, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/nginx-for-proxy-rewrite/130373/3 "2008-02-27T20:17:02Z")

</div>

On Wed, Feb 27, 2008 at 08:30:47PM +0200, Pavel Georgiev wrote:

> serve the requests as a proxy instead of returing a redirect, so this is  
> transparent to the client:
> 
> [http://mydomain.com/redirect/(.\*)$](http://mydomain.com/redirect/(.*)$) should go to [http://extranal.comain.com/$1](http://extranal.comain.com/$1)
> 
> I saw this is possible with a simple rewrite but it returns a redirect to the  
> client. Is is possible to make nginx to server the rewrite as a proxy?

Or probably, you need X-Accel-Redirect:

[http://wiki.codemongers.com/NginxXSendfile](http://wiki.codemongers.com/NginxXSendfile)

---

<div class="post-metadata">

**Author:** ![Pavel\_Georgiev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5e925/32.png) [@Pavel\_Georgiev](https://www.ruby-forum.com/u/Pavel_Georgiev)\
**Post date:** [February 27, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/nginx-for-proxy-rewrite/130373/4 "2008-02-27T21:50:22Z")

</div>

On Wed, Feb 27, 2008 at 10:27:18PM +0200, Pavel Georgiev wrote:

> > > ```
> > > listen 192.168.10.1:8080;
> > > 
> > > ```
> 
> This however returns a 302 code, what I want is nginx to get the file  
> requested from [http://some.domain.com/](http://some.domain.com/) and server it to the client, so that  
> the client doesn’t have a clue that this was taken from an external server.  
> In other words, I`d like to treat [http://some.domain.com](http://some.domain.com) as a backend server,  
> but just for a given location.
> 
> Hope that makes sense. I don’t think X-Accel-Redirect is what I need here.

I still do not understand your problem. Probably, you need:

```
location / {
    proxy_pass http://mydomain.com;
    proxy_redirect off;
    proxy_set_header Host $host;
}

location /redirect/ {
    proxy_pass http://some.domain.com/;
}

```

---

<div class="post-metadata">

**Author:** ![Pavel\_Georgiev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5e925/32.png) [@Pavel\_Georgiev](https://www.ruby-forum.com/u/Pavel_Georgiev)\
**Post date:** [February 27, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/nginx-for-proxy-rewrite/130373/5 "2008-02-27T21:40:15Z")

</div>

On Wednesday 27 February 2008 21:08:45 Igor S. wrote:

> > ```
> > server_name cmydomain.com;
> > 
> > ```
> > 
> > What I need to do is for a certain url to rewrite it to an external url  
> > Or probably, you need X-Accel-Redirect:
> 
> [http://wiki.codemongers.com/NginxXSendfile](http://wiki.codemongers.com/NginxXSendfile)

What I`m trying to do is to server some location (/redirect/ in the  
example  
above) to an external server. It is doable with this:

location /redirect {  
rewrite ^/redirect/(.\*)$ [http://some.domain.com/$1](http://some.domain.com/$1)  
}

This however returns a 302 code, what I want is nginx to get the file  
requested from [http://some.domain.com/](http://some.domain.com/) and server it to the client, so  
that  
the client doesn’t have a clue that this was taken from an external  
server.  
In other words, I`d like to treat [http://some.domain.com](http://some.domain.com) as a backend  
server,  
but just for a given location.

Hope that makes sense. I don’t think X-Accel-Redirect is what I need  
here.

---

<div class="post-metadata">

**Author:** ![Pavel\_Georgiev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5e925/32.png) [@Pavel\_Georgiev](https://www.ruby-forum.com/u/Pavel_Georgiev)\
**Post date:** [February 28, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/nginx-for-proxy-rewrite/130373/6 "2008-02-28T12:27:25Z")

</div>

On Wednesday 27 February 2008 22:42:37 Igor S. wrote:

> > > > server {
> > > > 
> > > > rewrite as a proxy?  
> > > > }  
> > > > I still do not understand your problem. Probably, you need:
> 
> ```
> location / {
> proxy_pass http://mydomain.com;
> proxy_redirect off;
> proxy_set_header Host $host;
> }
> 
> location /redirect/ {
> proxy_pass http://some.domain.com/;
> }
> 
> ```

Thats exactly what I needed, thanks a lot!
