Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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/nginxrouting/public_destination;
  location / {
    add_header working yes;
    return 200;
share|improve this question

You should use proxy_pass configuration directive (more info here), rewrite is not suitable for this.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.