7

I have many sub-domains set to my development machine like the following.

  • test1.example.com => 192.168.0.7
  • test2.example.com => 192.168.0.7
  • ... (about 30 domains like this)

I have many sites on my dev server and Apache can handle those based on the domain name.

However, I feel bad about setting the fixed IP. So I want to use aliases like so:

  • default.example.com => 192.168.0.7
  • test1.example.com => (CNAME) default.example.com
  • test2.example.com => (CNAME) default.example.com

The advantage here is when the dev server's IP is changed, I only need to change one instead of many.

If I request test1.example.com from the Apache server, does it take it as test1.example.com or default.example.com?

random
  • 450
  • 1
  • 9
  • 16
Sam Kong
  • 883
  • 1
  • 8
  • 10
  • 2
    Be sure to fully qualify the CNAME by putting a final dot (.) at the end. `default.mydomain.com.` – Keith Jun 02 '11 at 04:21
  • One catch, that only sometimes matters but mostly with emailing, is the reverse lookup (from IP address) will get the canonical name. So the forward and reverse names won't match. – Keith Jun 02 '11 at 04:22
  • @Keith, the reverse lookup will get whatever the reverse entry is. Reverse lookups do not use the forward entries to figure out what to return. – Chris S Jun 03 '11 at 17:13
  • @chris, yes that's true. But it's usually set to the canonical name. – Keith Jun 03 '11 at 20:45
  • @Keith, why would that be?? It's usually set to whatever the hostmaster set it to. – Chris S Jun 03 '11 at 21:19
  • @chris, exactly, and why would the hostmaster set it to something other than the canonical host name? – Keith Jun 03 '11 at 21:48

3 Answers3

10

This is generally a good idea, as you point out you only have to change the IP in one place now.

The client would look-up test1, get pointed to default, then get the IP. It would then connect to Apache and tell Apache that it wants the test1 site (it does not tell the server how it found the server, just what the original request is).

Chris S
  • 77,945
  • 11
  • 124
  • 216
3

You've identified one key advantage to the CNAME record.

As far as apache goes, the HTTP header Host field will remain whatever was entered into the client and the CNAME will have no effect.

One possible downside to CNAME is that looking up a record that has a CNAME entry will require two queries, first to get the CNAME and second to get the A record. This is probably not a big deal but worth mentioning.

Finally, be careful with "chaining CNAMEs," that is creating a CNAME to a CNAME. While it is permissible under the RFC, it is discouraged.

dmourati
  • 25,540
  • 2
  • 42
  • 72
  • 2
    Looking up a CNAME doesn't *necessarily* require two queries. If the DNS server supports recursive queries (most do), it will resolve the chain on behalf of the client; and if it owns the whole chain then the whole request will take about the same amount of time as looking up an A record. – Tom Shaw Jun 02 '11 at 05:46
1

To answer your last question: If you are using NameVirtualHost directive in Apache, and have a vhost set up for each CNAMEed entry, you will get unique pages as defined by that vhost.

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName test1.example.com
    DocumentRoot /var/www/test1
</VirtualHost>

Generally speaking From my experience, I'd recommend that you don't depend on the default configuration for any live content, and instead set up a vhost (with serveraliases if required) for each name. There are always exceptions, but this is generally easier to debug because every name that you are hosting is accounted for by being explicitly defined in the config.

Alex
  • 6,603
  • 1
  • 24
  • 32