Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Right now my nginx is rewriting several domains to one main domain which we are using. Here's one rule from my config:

server {
  listen X.X.X.X:80;
  server_name .exampleblog.org;
  rewrite ^(.*) http://blog.example.org$1 permanent;
}

Every request to *exampleblog.org is redirected to blog.example.org

Now I want www.exampleblog.org/+ and exampleblog.org/+ to redirect the user to our Google Plus page. It tried different versions of:

server {
  listen X.X.X.X:80;
  server_name .exampleblog.org;
  location /+ {
    rewrite ^ https://plus.google.com/12345678901234567890/ permanent;
  }
  rewrite ^(.*) http://blog.example.org$1 permanent;
}

Above and other versions just redirect to blog.example.org/+ - what am I doing wrong?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Directives in nginx don't necessarily apply in the order they appear in the configuration file. The server-level rewrite acts before a location is selected, and it always matches, so it will redirect everything. You need a second location like so:

server {
  listen x.x.x.x:80;
  server_name .exampleblog.org;

  # Match /+ requests exactly    
  location = /+ {
    # I prefer return 301 instead of rewrite ^ <target> permanent,
    # but you can use either
    return 301 http://plus.google.com/1234567890/;
  }

  # Match everything else
  location / {
    return 301 http://blog.example.org$request_uri;
  }
}
share|improve this answer
    
Aaah! Didn't try with a second location - thank you very much for the quick help :) Works perfectly! –  lorem monkey Nov 16 '11 at 16:38

Your Answer

 
discard

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.