The other virtual hosting option is to have one IP address per Web site, which is also known as IP-based virtual hosting. In this case, you will not have a NameVirtualHost directive for the IP address, and you must only have a single < VirtualHost >
container per IP address.
Also, because there is only one Web site per IP address, the ServerName directive isn’t needed in each
container, unlike in named virtual hosting.
IP Virtual Hosting Example: Single Wild Card
In this example, Apache listens on all interfaces, but gives the same content. Apache displays the content in the first
directive even if you add another right after it. Apache also seems to enforce the single
container per IP address requirement by ignoring any ServerName directives you may use inside it.
< VirtualHost * > DocumentRoot /home/www/site1 < /VirtualHost >
IP Virtual Hosting Example: Wild Card and IP addresses
In this example, Apache listens on all interfaces, but gives different content for addresses 97.158.253.26 and 97.158.253.27. Web surfers get the site1 content if they try to access the web server on any of its other IP addresses.
< VirtualHost *> DocumentRoot /home/www/site1 < /VirtualHost > < VirtualHost 97.158.253.26 > DocumentRoot /home/www/site2 < /VirtualHost > < VirtualHost 97.158.253.27 > DocumentRoot /home/www/site3 < /VirtualHost >
A Note On Virtual Hosting And SSL
Because it makes configuration easier, system administrators commonly replace the IP address in the
and NameVirtualHost directives with the * wildcard character to indicate all IP addresses.
If you installed Apache with support for secure HTTPS/SSL, which is used frequently in credit card and shopping cart Web pages, then wild cards won’t work. The Apache SSL module demands at least one explicit
< VirtualHost > directive for IP-based virtual hosting. When you use wild cards, Apache interprets it as an overlap of name-based and IP-based
directives and gives error messages because it can’t make up its mind about which method to use:
Starting httpd: [Sat Oct 12 21:21:49 2002] [error] VirtualHost _default_:443 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results
If you try to load any Web page on your web server, you’ll see the error:
Bad request! Your browser (or proxy) sent a request that this server could not understand. If you think this is a server error, please contact the webmaster
The best solution to this problem is to use wild cards more sparingly. Don’t use virtual hosting statements with wild cards except for the very first
directive that defines the web pages to be displayed when matches to the other < VirtualHost >
directives cannot be found. Here is an example.
NameVirtualHost * < VirtualHost *> Directives for other sites < /VirtualHost > < VirtualHost 97.158.253.28 > Directives for site that also run on SSL < /VirtualHost >