restrict default server on apache

I would like to deny access to the default server on apache. Only want to allow the virtual servers. Example: I want to disable access to a request with a sub domain like server1.mydomain.com (because server1.mydomain.com is not a web server but a simplya name of a machine). But by default apache will interpret that sub domain as adefault server and serve pages from /var/www directory. How can I disable that?

Thanks in advance.

Status: 
Active

Comments

Howdy -- it sounds like what you may want is to setup a Virtual Server that can act as a default page... but one that's served out of /home rather than /var/www.

If you setup a new Virtual Server, you can set it up as the default website for your server by going into Server Configuration -> Website Options, and setting "Default website for IP address" to "Yes".

Will that do what you're after?

I dont want it to use my virtual server as default website and in website option NO option is disabled. ANY IDEA?

Ilia's picture
Submitted by Ilia on Tue, 03/10/2020 - 09:03

I don't want it to use my virtual server as default website and in website option

Apache has global options for LISTEN, DocumentRoot, <Directory></Directory>,AssignUserId and many other, that affect default response for servers that are not registered as part of <VirtualHost></VirtualHost>. There are plenty of discussions about that. It's something you should figure out yourself, besides it's not difficult at all, just start picking it.

Unfortunately there is no way to prevent Apache from serving some content when a domain with no website is opened in a browser. The suggesting in comment #1 is the way to go - just create a blank website and make it the default.

actually I want to use my IP and redirect it to some other directory using trailing slash and because the empty server only redirects to its own folder. I want to point my IP to "/home" directory because the directory I want to point is there so when I was to use my IP I can do IPAddress/somedirectory to point to my website but due to this issue when I create empty server it create and points to /home/server which I don't want. in earlier version of virtual min this was not an issue :/

Ilia's picture
Submitted by Ilia on Thu, 03/12/2020 - 07:16

Unfortunately there is no way to prevent Apache from serving some content when a domain with no website is opened in a browser.

Unless we are talking about completely different setups, I'm personally convinced that it's possible, and here is how!

Example

DNS: 1.2.3.4 - dom1 dom2
     2.3.4.5 - dom3

Apache: 
  Listen 80
  [VirtualHost _default_:80]        # default for all other hosts, originating from IPs, that aren't used in VirtualHost
    DocumentRoot "/var/www/html"
    DirectoryIndex index-2.html
  [/VirtualHost]

  [VirtualHost 1.2.3.4:80]          # registered VirtualHost for dom1
    ServerName dom1
  [/VirtualHost]

Question 1: What happens if you visit dom2? It will fall to dom1. What happens if you visit dom3 - it will go to _default_ and look for index-2.html from /var/www/html.

Question 2: How to make dom2 not to use dom1 default website DocumentRoot? Simply by registering dummy VirtualHost with no particular ServerName directive for the same IP, and before any VirtualHost declaration for this IP:

[VirtualHost 1.2.3.4:80]           # registered for all other unregistred virtual-hosts originating from the same IP (1.2.3.4)
DocumentRoot "/var/www/html"
DirectoryIndex index-2.html
[/VirtualHost]

In both examples we registered defaults. :) Moving directive up and down also works, and hitting the first VirtualHost makes it global default but it's not always what you might want.