nginx: rewrite all except one location

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?


Solution 1:

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