0

So I have a website running on a lamp stack on Ubuntu.

I have two vhosts files...

000-default.conf:

<VirtualHost *:80>
    ServerName www.example.co.uk
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/

        ErrorLog ${APACHE_LOG_DIR}/site-a_error.log
        CustomLog ${APACHE_LOG_DIR}/site-a_access.log combined
</VirtualHost>

And moodle.example.co.uk.conf:

<VirtualHost *:80>
    ServerName moodle.example.co.uk
        ServerAdmin [email protected]
        DocumentRoot /var/www/moodle.example.co.uk/public_html

        ErrorLog ${APACHE_LOG_DIR}/site-b_error.log
        CustomLog ${APACHE_LOG_DIR}/site-b_access.log combined
</VirtualHost>

If I go to www.example.co.uk then I see the home page. Exactly as expected.

However if I go to moodle.example.co.uk I still see the homepage not the page stored in the DocumentRoot specified.

If I add a ServerAlias directive to the Moodle conf then it works fine.

Why does the ServerName directive not work?

EDIT: an update. If i disable ALL virtual hosts then my website is still served. this seems like very odd behaviour.

The access is logged in other_vhosts_access.log but I cannot find anything that should lead to this behaviour.

With no vhosts defined should the site just give a connection refused error?

2
  • Does this system only have two virtual hosts? Or is your post about these two virtual hosts? Apr 28, 2018 at 17:50
  • Currently only 2 as far as I am aware (in sites-enabled/) I usually have more but I am cutting down to fewer to troubleshoot before restoring the whole server.
    – James
    Apr 28, 2018 at 20:18

1 Answer 1

0

From Apache 2.4 documentation (may help):

When a request arrives, the server will find the best (most specific) matching argument based on the IP address and port used by the request. If there is more than one virtual host containing this best-match address and port combination, Apache will further compare the ServerName and ServerAlias directives to the server name present in the request.

If you omit the ServerName directive from any name-based virtual host, the server will default to a fully qualified domain name (FQDN) derived from the system hostname. This implicitly set server name can lead to counter-intuitive virtual host matching and is discouraged.

The following is most interesting: "If you omit the ServerName directive from any name-based virtual host, the server will default to a fully qualified domain name (FQDN) derived from the system hostname"

2
  • But I do have ServerName in both virtual hosts files. Or is there a hidden one I don't know about?
    – James
    Apr 28, 2018 at 19:10
  • It is possible you could. Execute in /etc/apache2 egrep -R 'Server(Name|Aliaas)' * to find all occurrences. Do you have browser cache completely disabled? Apr 29, 2018 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .