NGINX: How to forward multiple domains to a primary domain

I looked all over the interwebs for this and all I could find were articles on redirecting www to non-www or vice versa. Which is essentially the same thing.. but I figured the web deserves an article dedicated to just this.

The first server block sets up the redirects and the second block is what you are redirecting to. Pretty straight forward. Just make sure to /usr/local/nginx/sbin/nginx -t (or where ever the nginx binary is) before you try to restart as you may accidentally enter some funky characters.


server {
    listen 80;
    server_name secondarydomain.com tertiarydomain.com www.secondarydomain.com www.tertiarydomain.com;
    rewrite ^(.*) http://www.primarydomain.com$1 permanent;
}

server {
    listen 80;
    server_name www.primarydomain.com;
    # config for primary domain here
}
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Tumblr
  • Twitter

Leave a Reply