Вопрос

Is it possible to tell Apache to serve only configured VHosts? My current solution is to have a VHost with * as alias that gets used when non of the previous VHosts matches.

Это было полезно?

Решение

In Apache, the first listed VirtualHost is the default one, if no other VirtualHosts are matched. So, you could create a dummy VirtualHost with no content or just a landing page ("No site configured at this address" or similar) and put that as your first VirtualHost. No ServerName of ServerAlias is required, as long as it's the first one.

See also: http://www.onlamp.com/pub/a/apache/2004/01/08/apacheckbk.html

Другие советы

What I usually do is forbid access to the root directory in the conf.d/security file (that's the standard way in Ubuntu, in other OS you could put that in httpd.conf)

<Directory />
   AllowOverride None
   Order Deny,Allow
   Deny from all
</Directory>

Therefore, Apache will only allow access to subdirectories configured in VirtualHosts. For instance, you could have:

/var/www/example1.domain.com/your_files
/var/www/example2.domain.com/your_files

And have VirtualHosts like those:

    ServerName example1.domain.com

    ErrorLog /var/log/apache2/example1.domain.com_error.log
    CustomLog /var/log/apache2/example1.domain.com_access.log Combined

    DocumentRoot /var/www/example1.domain.com/

    (...)

    <Directory />
        Options FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

    ServerName example2.domain.com

    ErrorLog /var/log/apache2/example2.domain.com_error.log
    CustomLog /var/log/apache2/example2.domain.com_access.log Combined

    DocumentRoot /var/www/example2.domain.com/

    (...)

    <Directory />
        Options FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

However, mrjink noted that this configuration will use the first VirtualHost will server all requests that somehow reach the server (by ip or unknown hostname). Therefore, an additional Dummy VirtualHost is needed, which name should be something like "000-dummy-virtualhost" to ensure that it is the first VirtualHost to serve these requests.

With that conf, petitions to your server or your IP won't be served, but petitions to example1.domain.com or example2.domain.com will be.

Regards,

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top