adrianTNT
adrianTNT

Reputation: 4098

Confusion regarding apache default virtual host and wildcard port

I rewritten my whole question ...

So I want to use apache's _default_ in order to match any requests that are not clearly defined as hosts / ServerName on my server.

e.g when accessing server IP http://123.123.123.123 or http://test.mysite.tld (in below example).

See comments before the in below code samples.

# This correctly matches test.mysite.tld because is not defined anywhere, but ONLY if I define :80 here AND on second virtualhost

<VirtualHost _default_:80>
        DocumentRoot /var/www/html/default
</VirtualHost>


# this correctly matches mysite.tld and www.mysite.tld and NOT test.mysite.tld , but only when :80 is defined in both virtualhosts

<VirtualHost *:80>

        ServerName mysite.tld
        ServerAlias www.mysite.tld

        DocumentRoot /var/www/html/mysite.tld

</VirtualHost>

Problem is, this only works if both the default and defined virtualhosts are eider both defined as :80 or both :* wildcard.

But in this below example, accessing http://test.mysite.com matches the second vhost, I expect it to match the first default one:

# when this has wildcard port and second has :80, an undefined host like test.mysite.tld matches on second VirtualHost, I expected to match here :(

<VirtualHost _default_:*>
        DocumentRoot /var/www/html/default
</VirtualHost>


# I expect this to NOT match test.mysite.tld while I have a wildcard in above virtualhost

<VirtualHost *:80>

        ServerName mysite.tld
        ServerAlias www.mysite.tld

        DocumentRoot /var/www/html/mysite.tld

</VirtualHost>

I am not sure but I understand that this _default_ can only match an undefined host for exact same port pattern, but then I don't really see the point of wildcard port in _default_:*.

Virtualhost order doesn't matter here also, when default one has _default_:* and the defined ones are *:80 then an undefined host like http://test.mysite.tld always matches that defined one on *:80, NOT the _default_.

The apache documentation on default virtualhost.

Upvotes: 0

Views: 84

Answers (1)

adrianTNT
adrianTNT

Reputation: 4098

OK, I spent over 24 hours on this ...

Short answer: you need to have a ServerName defined in a _default_ virtualhost, even if is a dummy one, otheriwse it will have VERY strange virtualhosts matches (as docs say here).


If you wrongly define a "default" VirtualHost like this (without a ServerName):

<VirtualHost _default_:80>
        DocumentRoot /var/www/html/default
</VirtualHost>

hoping to match any requests to your server using hosts/domains that are not defined in next vhost ...

<VirtualHost *:80>

        ServerName mysite.tld
        ServerAlias www.mysite.tld

        DocumentRoot /var/www/html/mysite.tld

</VirtualHost>

The above config will fail with a strange result:

Even thou both mysite.tld and www.mysite.tld are defined in second virtualhost and NONE in first (_default_) virtualhost ...

  • mysite.tld will match the first (_default_) virtualhost
  • www.mysite.tld will match the second virtualhost

Because you need to have a ServerName in the _default_ virtualhost, even if it is a dummy value.

Otheriwse (as docs say here):

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.

So in my case, I didn't provide a ServerName in that _default_ virtualhost and that caused http://mysite.tld to match that default vhost, but NOT http://www.mysite.tld (which matched the second), even thou both were defined just in second virtualhost.


So my final config looks like below. Matching any undefined host/domain/site to that first default virtualhost AND mysite.tld and www.mysite.tld to second virtualhost:

<VirtualHost *:80>

        # IMPORTANT, any value would do !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        ServerName foo-required

        DocumentRoot /var/www/html/default
</VirtualHost>


<VirtualHost *:80>

        ServerName mysite.tld
        ServerAlias www.mysite.tld

        DocumentRoot /var/www/html/mysite.tld

</VirtualHost>

Upvotes: 0

Related Questions