Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
2  
Be sure to fully qualify the CNAME by putting a final dot (.) at the end. default.mydomain.com. –  Keith Jun 2 '11 at 4: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 2 '11 at 4: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 3 '11 at 17:13
    
@chris, yes that's true. But it's usually set to the canonical name. –  Keith Jun 3 '11 at 20:45
    
@Keith, why would that be?? It's usually set to whatever the hostmaster set it to. –  Chris S Jun 3 '11 at 21:19

3 Answers 3

up vote 10 down vote accepted

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).

share|improve this answer

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.

share|improve this answer
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 2 '11 at 5:46

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.