This really is most likely a simple question, but I wish to get to know how Apache works together with virtual hosts. I'm establishing virtual hosts because Sometimes on multiple websites at the same time and I'd rather not use subdirectories. I had been virtually while using default Apache httpd.conf file using the DocumentRoot pointing to something similar to "/www". I uncommented the virtual hosts include and added the next:


NameVirtualHost *:80

<VirtualHost *:80>

    ServerName site1.dev

    DocumentRoot /www/site1

</VirtualHost>

<VirtualHost *:80>

    ServerName site2.dev

    DocumentRoot /www/site2

</VirtualHost>

Now after i visit http://localhost I recieve the default page for site1.

I am sure there's grounds why this will make sense, however i don't quite comprehend it. I would have believed that only demands which were clearly to http://site1.test would get routed using that directive also it wouldn't just end up being the default. Can someone explain why it might be the default.

http://httpd.apache.org/docs/1.3/vhosts/name-based.html

(Ought to be true for just two.by also)

"If no matching virtual host is located, then your first listed virtual host that suits the Ip is going to be used.

As a result, the very first listed virtual host may be the default virtual host. The DocumentRoot in the primary server should never be used when an Ip matches the NameVirtualHost directive. If you'd like to possess a special configuration for demands that don't match any particular virtual host, to put it simply that configuration inside a container and list it first within the configuration file."

answer 1 is correct and i'd add with namevirtualhosts because the first entry basically matches any not-named elsewhere virtualhost

it will ONLY be employed to catch unintended mal-created and damaged traffic

ie a machene with one ip known as john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz may have an optimal config like thus

    <VirtualHost *:80>
     DocumentRoot /var/webserver/static-sites/unknown/
    # a directory readable by apache with only a robots.txt denying everything
     ServerName bogus
     ErrorDocument 404 "/errordocuments/unknown-name.html"
    #custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
     ErrorLog /var/log/httpd/unknown-error.log
     CustomLog /var/log/httpd/unknown-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     DocumentRoot /var/webserver/static-sites/unknown/
    # a possibly different directory readable by apache with only a robots.txt denying everything
     ServerName www.xxx.yyy.zzz
     ServerAlias john.domain.com
     ErrorDocument 404 "/errordocuments/ip-name.html"
     ErrorDocument 403 "/errordocuments/ip-name.html"
    #custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
     ErrorLog /var/log/httpd/ip-error.log
     CustomLog /var/log/httpd/ip-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     ServerName domain.com
     RedirectPermanent / http://www.domain.com/
     ErrorLog logs/www.domain.com-error.log
     CustomLog logs/www.domain.com-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     DocumentRoot /var/webserver/ftpusers/domain
     ServerName www.domain.com
     ServerPath /domain
     ErrorLog logs/www.domain.com-error.log
     CustomLog logs/www.domain.com-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     ServerName domain2.com
     RedirectPermanent / http://www.domain2.com/
     ErrorLog logs/www.domain2.com-error.log
     CustomLog logs/www.domain2.com-access.log combined
    </VirtualHost>

    <VirtualHost *:80>
     DocumentRoot /var/webserver/ftpusers/domain2
     ServerName www.domain2.com
     ServerPath /domain2
     ErrorLog logs/www.domain2.com-error.log
     CustomLog logs/www.domain2.com-access.log combined
    </VirtualHost>