Greetings.

1 IP, on it N hosts.
Described as virtual hosts, entries in httpd.conf like this:

NameVirtualHost *:80 <VirtualHost *:80> ServerName www.domain.tld ServerAlias domain.tld DocumentRoot /www/domain </VirtualHost> <VirtualHost *:80> ServerName www.otherdomain.tld ServerAlias otherdomain.tld DocumentRoot /www/otherdomain </VirtualHost> 

Faced with the fact that when you try to open a subdomain undefined in the config, for example, ns2.domain.tld, the contents of domain.tld open. But in servername, serveralias this subdomain does not appear. How to prevent open it?

    2 answers 2

    The last defined VirtualHost is a virtual host by default, it gets all requests for which there was no suitable virtual host (probably, you can somehow explicitly set the default virtual host, but I don’t know how).

    Accordingly, you need to create a service VirtualHost, which will be picked up last (for example, to be the last in the config) and place some stub in it.

      Add at the end:

       <VirtualHost *:80> ServerName default Redirect 404 / </VirtualHost> <VirtualHost _default_:80> Redirect 404 / </VirtualHost> 

      This will return 404 to all non-existent hosts.

      • did not help. - Anton Shevtsov
      • But is it worth it on top of Apache frontend, for example, nginx? Then, receiving 404, he can do something else of his own - for example. show default site. - Sergiks