Apache virtual host based on CNAME

Posted on

Apache virtual host based on CNAME – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, apache-2.2, domain-name-system, virtualhost, .

Can I make a vhost on apache to respond to a cname request? For example:

A-record of example.com is 1.2.3.4
foo.example.com is a CNAME for example.com

in http.conf:

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

<VirtualHost foo.example.com>
 ServerName foo.example.com
 DocumentRoot /foo/www
</VirtualHost>

apache starts fine and digests my conf, but when I visit foo.example.com I get the content in /var/www, not the one in /foo/www. I think I’m on the wrong track here.

Solution :

The following snippet should work (without any warning):

NameVirtualHost *:80

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

<VirtualHost *:80>
 ServerName foo.example.com
 DocumentRoot /foo/wwww
</VirtualHost>

Make sure you read the documentation on Name-based Virtual Host Support to understand why it didn’t work in the first place.

I got it working like this:

NameVirtualHost _default_:80

<VirtualHost _default_:80>
  ServerName example.com
  DocumentRoot /var/www
</VirtualHost>

<VirtualHost _default_:80>
 ServerName foo.example.com
 DocumentRoot /foo/wwww
</VirtualHost>

Although this gives a warning at http start:

[warn] NameVirtualHost _default_:80 has no VirtualHosts

Did you try adding the port number on the foo.example.com vhost ?

ie:

<VirtualHost foo.example.com:80>
    ServerName foo.example.com
    DocumentRoot /foo/www
</VirtualHost>

Leave a Reply

Your email address will not be published.