How to make nginx to redirect multiple subdomains?

Virtual Hosts problems

  • Virtual Hosts are driving me insane. What am I doing wrong...? I have example.com I want example.com and www.example.com to be bounced to the main corporate site (examplecorp.com) and the rest of the subdomains to stay on the machine they went to originally. So, I have this (which works)<VirtualHost *:80> ServerName www.example.com ServerName example.com Redirect 302 / http://www.examplecorp.com</VirtualHost><VirtualHost *:80> ServerName sub1.example.com ServerAlias sub2.example.com DocumentRoot /var/www/html/codebase[...]</VirtualHost>[...]The thing is, I want to make it so that subdomains can be added automatically. I've tried changing ServerName to example.com or *.example.com and it's sending me to the CentOS default page. Any suggestions what this non SysAdmin is doing wrong?

  • Answer:

    As far as I know, you can only have 1 ServerName per Virtual Host. You need ServerAlias: ServerName www.example.com ServerAlias example.com

twine42 at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

Only ServerAlias can take wildcards. ServerName is meant to be the one main name for that vhost, and as volatilebit says there should be only one per stanza.

Rhomboid

Ah, fantastic. The following works nicely. Ta! ServerName example.com ServerAlias *.example.com

twine42

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.