Johannes Linkels
Johannes Linkels

Reputation: 223

Apache virtualhost defaults to FQDN

I have set up a VirtualHost on my Apache system which happens to have the same name as the FQDN of the server. This is the FQDN:

root@mail:/etc/apache2/sites-available# hostname -f
mail.example.com

This is the contents of the .conf files:

000-default.conf:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

example.com.conf:
<VirtualHost *:80>
    ServerName mail.example.com
    DocumentRoot /home/example/public_html
</VirtualHost>

Now I would expect mail.example.com would match the VirtualHost, and as a consequence use the DocumentRoot /home/example/public_html. It does not.

Running apache2ctl -S yields:

*:80                   is a NameVirtualHost
     default server mail.example.com (/etc/apache2/sites-enabled/000-default.conf:1)
     port 80 namevhost mail.example.com (/etc/apache2/sites-enabled/000-default.conf:1)
     port 80 namevhost mail.example.com (/etc/apache2/sites-enabled/abcarrental.nl.conf:1)

So the default server is mail.example.com and consequently the DocumentRoot is /var/www/html.

Choosing a different VirtualHost name (e.g. webmail.example.com) and changing the VirtualHostName to webmail.example.com solves the problem.

I have gone through the Apache NamedVirtualHost manual pages and I cannot find an explanation. Only explanation that if a name does not match any of the vhosts, the default is used. But nothing that the default is used if the FQDM is equal to the requested name.

Is this behaviour by design?

Upvotes: 3

Views: 1750

Answers (1)

covener
covener

Reputation: 17872

Yes, it's by design when you omit a ServerName from a VirtualHost that is is initialized from the systems local hostname, or barring that, from the reverse DNS lookup of any local IP address.

This config is unfortunately done by the default virtual host created by debian/ubuntu packages.

With hindsight -- It's considered preferrable to set a completely dummy ServerName in a catch-all (first listed) virtual host.

Upvotes: 3

Related Questions