# Proxying/redirecting after request has been rewritten

**URL:** <https://www.ruby-forum.com/t/proxying-redirecting-after-request-has-been-rewritten/221997>\
**Category:** NGINX\
**Created:** [August 8, 2012, 5:04pm UTC](https://www.ruby-forum.com/t/proxying-redirecting-after-request-has-been-rewritten/221997 "2012-08-08T17:04:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 8, 2012, 5:04pm UTC](https://www.ruby-forum.com/t/proxying-redirecting-after-request-has-been-rewritten/221997/1 "2012-08-08T17:04:55Z")

</div>

Hi everyone.

I’m trying to get Nginx to run some Regex’s on requests and send them to  
another server block, returning the response headers from the second  
server block. At the moment I get a 302 response status, how do I get  
the headers from the second server block?

So as an example, I would like a request like:  
[http://nginxrouting.local/some/stuff/that/needs/to/be/removed/itemid=1234/more/stuff/topicid=1234](http://nginxrouting.local/some/stuff/that/needs/to/be/removed/itemid=1234/more/stuff/topicid=1234)  
to be sent to  
http://nginxrouting\_destination.local/itemid=1234topicid=1234 returning  
the headers from the new location

The server blocks look like this:

server {  
server\_name nginxrouting.local;  
root /var/nginxrouting/public;

location / {

```
if ($request_uri ~* ".*(itemid=[0-9]*){1}.*") {
  set $itemid $1;
}
if ($request_uri ~* ".*(topicid=[0-9]*){1}.*") {
  set $topicid $1;
}
if ($request_uri ~* ".*(&type=RESOURCES){1}.*") {
  set $resources $1;
}

rewrite ^

```

http://nginxrouting\_destination.local/$itemid$topicid$resources?  
redirect;  
add\_header itemid $itemid;

}

}

server {  
server\_name nginxrouting\_destination.local;  
root var/govuk/nginxrouting/public\_destination;  
location / {  
add\_header working yes;  
return 200;  
}  
}

Posted at Nginx Forum:

> **[proxying/redirecting after request has been rewritten](https://forum.nginx.org/read.php?2,229458,229458#msg-229458)**
>
> Hi everyone. I'm trying to get Nginx to run some Regex's on requests and send them to another server block, returning the response headers from the second server block. At the moment I get a 302 response status, how do I get the headers from the...

---

<div class="post-metadata">

**Author:** ![i0n](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/eada6e/32.png) [@i0n](https://www.ruby-forum.com/u/i0n)\
**Post date:** [August 8, 2012, 5:38pm UTC](https://www.ruby-forum.com/t/proxying-redirecting-after-request-has-been-rewritten/221997/2 "2012-08-08T17:38:51Z")

</div>

Hello!

On Wed, Aug 08, 2012 at 11:04:25AM -0400, i0n wrote:

> http://nginxrouting\_destination.local/itemid=1234topicid=1234 returning  
> the headers from the new location

Use proxy\_pass. See [Module ngx\_http\_proxy\_module](http://nginx.org/r/proxy_pass) for details.

Maxm Dounin
