0

I have a system where many (~20k) subdomains use nginx's default_server, which passes the work off to an app.

I also have many (~100) hostnames that need to be redirected to a correct one, that is different for each hostname and that would then redirect to the default_server.

one.example.com -> eleven.example.com
two.example.com -> twelve.domain.com
three.example.com -> wibble.example.com
blah.domain.com -> fifteen.example.com

The redirects are arbitrary, ie there is no pattern to them.

Rather than having to update nginx config to add a new server block whenever a new redirect is needed or updated I'd prefer to use a map file of some sort that nginx can check for redirects.

Sadly having searched about quite a bit I've not found anything like it, all examples I've found use a new server block for each redirecting host or use regexes. I'd prefer to be able to update a map file or database on the fly that nginx can refer to.

My current best option I have is to update the background app to apply the redirects.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
DaveS
  • 1
  • 1
  • Here's [a map solution](http://stackoverflow.com/questions/18037716/how-to-redirect-single-url-in-nginx/27383436#27383436) that sounds like what you're looking for. – Cole Tierney Jan 08 '16 at 02:11
  • @ColeTierney, have to mention, that it still requires nginx reload after map change. – Alexey Ten Jan 08 '16 at 09:42

1 Answers1

0

I did previously find the map but it wasn't clear that it could be used in this way and none of the examples showed it. Saying that it turned out to be quite easy.

This is what I have that seems to work;

map $host $redirect_host {
  hostnames;

  one.david.org     eleven.david.org;
  two.david.org     twelve.steve.org;
  three.steve.org   thirteen.david.org;
  four.steve.org    fourteen.steve.org;

}

server {
  ...
   if ($redirect_host) {
     return 301 $scheme://$redirect_host$request_uri;
   }
  ...
}

It's a shame that this solution requires nginx restart, but it's not a big deal.

DaveS
  • 1
  • 1