2

In one of my nginx sites-available configuration file, I can specify a server name alias, like so:

server_name example.com www.example.com;

If I had an Apache server, I can do something similar with ServerName and ServerAlias:

ServerName example.com
ServerAlias www.example.com

I have also specified a CNAME record in my zone file:

www.example.com. 1800 IN CNAME example.com.

I want www.example.com and example.com to server the same content. Since I have a CNAME record, all www.example.com will be pointed to example.com anyways, so there's no need for the alias. The argument can also be turned on its head to say the CNAME record is not needed.

Is the alias in my web server (nginx or Apache) and the CNAME record redundant? If so, should I get rid of one of them? And if so, which one?

  • 1
    It is best to always have one domain version in use (either www.example.com or example.com), and then have a permanent redirect from the other version to the main version. This way you won't be flagged with duplicate content penalties from search engines. – Tero Kilkanen Jan 22 '15 at 15:13
  • @TeroKilkanen Thanks for the tip! I was literally reading about it as you posted the comment! 301 Redirect! – dayuloli Jan 22 '15 at 15:15
3

Without the CNAME, you would need an A record for the www-subdomain instead, otherwise DNS resolution for www.example.com won't work and the request won't reach your server.

You will also need the ServerAlias, because even when using CNAME, the host header in the HTTP request won't change, so it will still be www.example.com and you need to tell Apache/nginx which vhost should serve this request.

So nothing's redundant here. The only advantage of using a CNAME for www.example.com instead of an A record is that when your IP changes, you will only have to change the A record for example.com . When using A records, you would have to change them both.

  • Had to read it a few times to realize it is a fantastic answer, cleared everything up for me! CNAME record to point the HTTP packet to the correct IP address, but the host header within this HTTP packet still wouldn't change and still needs an alias from the web server virtual host/server block configuration. – dayuloli Jan 22 '15 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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