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 a domain name example.com, for which I'd like mail to be routed to different servers depending on the circumstances.

Mail sent to an @example.com address should go to the server mail.server1.com, and mail to any subdomain @*.example.com should to to mail.server2.com

Is there a way to configure MX records to do this (or any other way to do this for that matter)? Can I use wildcard DNS entries for MX records?

share|improve this question
add comment

3 Answers

It all depends on the DNS server you are using.

In Bind from this example:

; zone fragment for 'zone name' example.com
; name servers in the same zone
$TTL 2d ; zone default TT = 2 days
$ORIGIN example.com.
@              IN      SOA   ns1.example.com. hostmaster.example.com. (
               2003080800 ; serial number
               2h         ; refresh =  2 hours 
               15M        ; update retry = 15 minutes
               3W12h      ; expiry = 3 weeks + 12 hours
               2h20M      ; minimum = 2 hours + 20 minutes
               )
; main domain name servers
              IN      NS     ns1.example.com.
              IN      NS     ns2.example.com.
; mail servers for main domain
              IN      MX 10  mail.example.com.
; A records for name servers above 
ns1           IN      A      192.168.0.3
ns2           IN      A      192.168.0.4
; A record for mail servers above 
mail          IN      A      192.168.0.5
; other domain level hosts and services
bill          IN      A      192.168.0.6
....
; sub-domain definitions
$ORIGIN us.example.com.
              IN      MX 10  mail
; record above uses blank substituition 
; and could have been written as 
; us.example.com.   IN  MX 10 mail.us.example.com.
; OR (using @ substitution)
; @ IN MX 10 mail
; A record for subdomain mail server
mail          IN      A      10.10.0.28
; the record above could have been written as 
; mail.us.example.com. A 10.10.0.28 if it's less confusing
ftp           IN      A      10.10.0.29 
; the record above could have been written as 
; ftp.us.example.com. A 10.10.0.29 if it's less confusing
....
; other subdomain definitions as required 

Additional sub-domains could be defined in the same file using the same strategy. For administrative convenience you could use $INCLUDE directives e.g.

; snippet from file above showing use of $INCLUDE
....
; other domain level hosts and services
bill          IN      A      192.168.0.5
....
; sub-domain definitions
$INCLUDE us-subdomain.sub
; other subdomain definitions as required
share|improve this answer
add comment
up vote 1 down vote accepted

I discovered the answer to this almost immediately after posting :)

Set the MX records for example.com to mail.server1.com as expected.

Then to set the wildcard, you can create a wildcard CNAME *.example.com which points to some generic subdomain sub.example.com, then create an MX record for sub.example.com which points to mail.server2.com.

share|improve this answer
    
You can mark your own answer as the accepted one then, so that it can help others. –  Drazisil Oct 13 '11 at 13:19
add comment

A wildcard MX record is perfectly valid, there's no need for your extra CNAME.

You could simply have...

example.com    IN MX 10 mail.server1.com
*.example.com  IN MX 10 mail.server2.com

Your CNAME just causes an extra DNS lookup

share|improve this answer
add comment

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.