0

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.

11
  • This reads to me like you missed the distinction between a "virtual host" and a "name based virtual host" that the apache http server makes (actually all http server's to that these days). A name based virtual host is not selected by the IP address the request is sent to and responded to. But by the host name used in the request. So the "domain name" as it is often, incorrectly referred to. The requests carry an additional Host header, that is what is used to select the responding virtual host inside the http server by means of the ServerName and ServerAlias configurations. Commented Aug 20, 2024 at 4:59
  • Yes, but still, if I access http://123.123.123.123 then it sends this ip as a host header, no ? And if I don't have this IP defined as a ServerName inside a <VirtualHost> then I expect it to match that _default_ virtual host. I think the problem here is that this _default_ only matches ServerNames that were not defined in any <VirtualHost> but only as long as both virtualhosts clearly specify same exact port config, both wildcard or both defined as 80 or whatever port. Commented Aug 20, 2024 at 12:29
  • Usually a request to an IP address does not carry a Host header, but that is up to the requesting client, obviously. Certainly the http server will not use an address to select the virtual host by means of the ServerName or ServerAlias directives. That makes no sense simply because no host name has been specified in the request. Instead the server will delegate the request to the first virtual host that matches the request. Which one that is depends on which addresses the virtual hosts did bind to and whether the port matches, obviously. Commented Aug 20, 2024 at 15:01
  • AFAIK the directive <VirtualHost _default_:*> is invalid. You should see an entry in the http server's error log about that when you restart the server process. You cannot somehow bind to all ports and listen on all ports of a system. Many ports are already blocked, and that simply makes no sense. You can omit the port specification in that directive according to the documentation. I am actually not quite sure what that does, I do not see how the server should know a default port (because the protocol is unknown, but it might simply default to port 80 for historical reasons). Commented Aug 20, 2024 at 15:08
  • This <VirtualHost _default_:*> is valid (is in their docs), most probably it means "any port apache listens on", not "listen on all these ports". These are the docs: httpd.apache.org/docs/2.4/vhosts/examples.html#default I am confused, I will do some more experiments. Commented Aug 20, 2024 at 15:14

1 Answer 1

0

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.