Configuring Name-based Virtual Hosting


The configuration for named virtual hosts is similar to IP virtual hosts. The example in Listing 5.2 shows two virtual hosts sharing the 192.168.200.2 IP address.

Apache will decide to which virtual host to "route" the request based on the value of the Host: header of the HTTP request. It will be compared for a match with the hostname provided by ServerName and any additional hostnames provided by the ServerAlias directives, which are optional.

Listing 5.2. Configuring Name-based Virtual Hosts

Listen 80 NameVirtualHost 192.168.200.2 <VirtualHost 192.168.200.2>     ServerName www.example.com     ServerAlias example.com web.example.com     DocumentRoot /usr/local/apache/sites/example.com </VirtualHost> <VirtualHost 192.168.200.2>     ServerName www.example.net     DocumentRoot /usr/local/apache/sites/example.net </VirtualHost>

The NameVirtualHost directive is required to tell Apache that a particular IP address will be used for name-based virtual hosts. You can tell Apache to use any available IP address for name-based virtual hosting with

NameVirtualHost *


Of course, your DNS servers need to be configured properly so the domains www.example.com, example.com, and web.example.com resolve to the 192.168.200.2 address.




Apache(c) Phrase Book(c) Essential Code and Commands
Apache Phrasebook
ISBN: 0672328364
EAN: 2147483647
Year: 2006
Pages: 254

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net