Apache can serve more than one page per apache instance. One way of doing it is to create virtual hosts based on domain name. Http server simply serves different context based on domain that was used to access it. For example, you can have two domains: www.example1.com and www.example2.com, both pointing to the same IP address. Apache will show different pages based on what address was typed in the browser.

Great. so why do I need a default host?

It’s because apache will serve first virtual host which is defined in configuration files if it can’t match domain name. Is it bad? In general no. But there is some unwanted traffic, back there, in Internet. There are some scanners or other abuses.

  • Your http server don’t necessary need to serve content for those requests, especially when generation of your page is heavy.
  • Catching this traffic into separate log files can be very useful when analyzing problems of your site.

Better serve something light and fast. With big red “go away” letters. Simply add a default host

<VirtualHost _default_:80>
    DocumentRoot "/var/www/html/default"
    <Directory "/var/www/html/default">
        Options None
        AllowOverride None
        DAV Off
    </Directory>
    LogLevel warn
</VirtualHost>

<VirtualHost *:80>
        ServerAdmin mail@mail.com
        ServerName www.example1.com

        DocumentRoot "/var/www/html/www.example1.com"

        <Directory "/var/www/html/www.example1.com">
                Options FollowSymLinks MultiViews Includes ExecCGI
                AllowOverride none
                Order allow,deny
                allow from all
                Require all granted
                RewriteEngine On
                RewriteBase /
                RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
                RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
                RewriteRule ^index\.php$ - [L]
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule . /index.php [L]
        </Directory>

        ErrorLog /var/log/httpd/www.example1.com-error.log
        LogLevel warn
        CustomLog /var/log/httpd/www.example1.com-access.log combined

</VirtualHost>

Lines 1 – 9 give you default host definition and basically it is all you need. From now on, if someone access your web server with different domain name than configured one, he will be sent to default virtual host.
If you have modular apache configuration (in separate files in conf.d directory), don’t forget to name a file with default host like 00-default.conf. Order matters!

Leave a Reply