1

I have installed apache 2 and created and enabled a virtual host.

The virtual host has: ServerName example.com ServerAlias *.example.com ServerAlias foo.com ServerAlias *.foo.com ServerAlias bar.com ServerAlias *.bar.com

If I direct the browser to www.example.com, test.example.com, foo.com, www.foo.com, etc, ANYTHING except the bare server name, it works as expected, showing the content of /srv/www/example.com/public_html/index.php which is the DocumentRoot for the virtual host.

However, if I write only "example.com" in the browser, i see the contents of /var/www/index.html

It's NOT cached in the browser, I have already cleared the cache and also tried another browser and tried through a proxy.

This makes no sense to me. Any idea?

Here are the contents of /etc/apache2/sites-enabled/000-default

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And here are the contents of /etc/apache2/sites-enabled/example.com:

    <VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    ServerAlias foo.com
    ServerAlias *.foo.com
    ServerAlias bar.com
    ServerAlias *.bar.com
    #... and a few others
    DocumentRoot /srv/www/example.com/public_html
    ErrorLog /srv/www/example.com/error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel notice
    CustomLog /srv/www/example.com/access.log combined

      <Directory /srv/www/example.com/public_html>
        Options FollowSymLinks
        AllowOverride All
      </Directory>

    </VirtualHost>

I can't understand why, with the bare servername, the default vhost prevails over the one that specifically matches the server name, while all aliases work fine.

And here's the output of # apache2ctl -S

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443                  is a NameVirtualHost
     default server example.com (/etc/apache2/sites-enabled/default-ssl:2)
     port 443 namevhost example.com (/etc/apache2/sites-enabled/default-ssl:2)
*:80                   is a NameVirtualHost
     default server example.com (/etc/apache2/sites-enabled/000-default:1)
     port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default:1)
     port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com:1)
Syntax OK

(I guess it's needless to say that I've replaced the actual domain names with "example.com", "foo.com" and "bar.com" just for posting here at SF)

2
  • What do you mean by 'bare server name'?
    – user9517
    Oct 20, 2014 at 19:52
  • I mean "example.com" as opposed to www.example.com or anything.example.com. That is, the one that is defined as ServerName as opposed to any of the ServerAlias-es.
    – matteo
    Oct 20, 2014 at 20:01

2 Answers 2

1

What other files are linked in /etc/apache2/sites-enabled/?

Often it is useful for diagnosing purposes to enable mod-info, like so (from http://httpd.apache.org/docs/2.2/mod/mod_info.html):

<Location /server-info>
  SetHandler server-info
</Location>

<Location /server-info>
  SetHandler server-info
  Order deny,allow
  Deny from all
  Allow from yourcompany.com
</Location> 

Generally I secure this with a certificate, but that goes beyond this explanation. Once you have that enabled, and restricted to 127.0.0.1 (tunnel with ssh to the server, set your hosts file etc). You can see the exact running configuration as parsed by Apache. You'll probably see that example.com is referenced elsewhere. Often things make more sense when you look using mod-info.

3
  • No other files in sites-enabled. I grepped /etc/apache2 recursively for "example.com" and there's no result except my example.com definition file in sites-enabled. Can it be because it's the reverse dns for the IP? As a workaround, if i disable the "default" site, all works as expected, but (a) I'd like to keep the default site for the sake of catching any request that is not recognized as a valid virtual host, and (b) don't actual virtual hosts inherit from the default one all the configurations that they don't define?
    – matteo
    Oct 20, 2014 at 20:02
  • Sorry I do have another file, default-ssl: pastebin.com/EkUqDUNN should be irrelevant, though, since it only applies to port 443 (yes, it is irrelevant, disabling it doesn't change a thing)
    – matteo
    Oct 20, 2014 at 20:20
  • server-info doesn't tell me anything that I don't already know. It shows me the settings of the default and example.com site definitions. This doesn't help in determining why those of default are being preferred over those of example.com
    – matteo
    Oct 20, 2014 at 20:25
1

Apparently, documentation isn't clear about this.

From https://issues.apache.org/bugzilla/show_bug.cgi?id=57384

"If you omit the ServerName [in the virtual host definition], it's calculated from the system hostname (double-reverse DNS lookup of the system hostname)". Hence, if the enabled site 000-default has no server name, it is exactly as if it had example.com as the ServerName, being example.com my server's host name.

Hence in my case, when the client requests the domain example.com, 000-default is the first server that matches the requested hostname, and "catches" the request.

If I want (like I do) to have the default site catch all the request that don't match any server name, I need to specify a dummy ServerName in the default VirtualHost definition. This way, requests to the server's hostname won't match the default virtual host but they will match the example.com virtual host. And requests that don't match any servername or alias will be caught by the default site because it's the first one.

"The first-listed vhost captures anything that doesn't match a servername/serveralias, even if you specify a servername."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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