| |
|
|
Virtual Host examples for common setups
- Setup 1: Catching every request to any unspecified IP address
and port, i.e., an address/port combination that is not used for any other
virtual host.
Server configuration:
...
<VirtualHost _default_:*>
DocumentRoot /www/default
...
</VirtualHost>
Using such a default vhost with a wildcard port effectively prevents any request going
to the main server.
A default vhost never serves a request that was sent to an address/port that is used for
name-based vhosts. If the request contained an unknown or no Host: header
it is always served from the primary name-based vhost (the vhost for that address/port
appearing first in the configuration file).
You can use
AliasMatch
or
RewriteRule
to rewrite any request to a single information page (or script).
- Setup 2: Same as setup 1, but the server listens on several ports and
we want to use a second
_default_ vhost for port 80.
Server configuration:
...
<VirtualHost _default_:80>
DocumentRoot /www/default80
...
</VirtualHost>
<VirtualHost _default_:*>
DocumentRoot /www/default
...
</VirtualHost>
The default vhost for port 80 (which must appear before any default vhost with
a wildcard port) catches all requests that were sent to an unspecified IP address. The
main server is never used to serve a request.
- Setup 3: We want to have a default vhost for port 80, but no other
default vhosts.
Server configuration:
...
<VirtualHost _default_:80>
DocumentRoot /www/default
...
</VirtualHost>
A request to an unspecified address on port 80 is served from the default vhost any
other request to an unspecified address and port is served from the main server.
- Setup: The name-based vhost with the hostname www.otherdomain.tld
(from our name-based example, setup 2) should get its own IP address.
To avoid problems with name servers or proxies who cached the old IP address for the
name-based vhost we want to provide both variants during a migration phase.
The solution is easy, because we can simply add the new IP address (111.22.33.66)
to the VirtualHost directive.
Server configuration:
...
Port 80
ServerName www.domain.tld
DocumentRoot /www/domain
NameVirtualHost 111.22.33.55
<VirtualHost 111.22.33.55 111.22.33.66>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
ServerAlias *.sub.domain.tld
...
</VirtualHost>
The vhost can now be accessed through the new address (as an IP-based vhost) and through
the old address (as a name-based vhost).
- Setup: We have a server with two name-based vhosts. In order to match
the correct virtual host a client must send the correct
Host: header. Old
HTTP/1.0 clients do not send such a header and Apache has no clue what vhost the client
tried to reach (and serves the request from the primary vhost). To provide as much
backward compatibility as possible we create a primary vhost which returns a single page
containing links with an URL prefix to the name-based virtual hosts.
Server configuration:
...
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
# primary vhost
DocumentRoot /www/subdomain
RewriteEngine On
RewriteRule ^/.* /www/subdomain/index.html
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain/sub1
ServerName www.sub1.domain.tld
ServerPath /sub1/
RewriteEngine On
RewriteRule ^(/sub1/.*) /www/subdomain$1
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain/sub2
ServerName www.sub2.domain.tld
ServerPath /sub2/
RewriteEngine On
RewriteRule ^(/sub2/.*) /www/subdomain$1
...
</VirtualHost>
Due to the
ServerPath
directive a request to the URL http://www.sub1.domain.tld/sub1/ is always
served from the sub1-vhost.
A request to the URL http://www.sub1.domain.tld/ is only served from the
sub1-vhost if the client sent a correct Host: header. If no Host:
header is sent the client gets the information page from the primary host.
Please note that there is one oddity: A request to http://www.sub2.domain.tld/sub1/
is also served from the sub1-vhost if the client sent no Host: header.
The RewriteRule directives are used to make sure that a client which sent a
correct Host: header can use both URL variants, i.e., with or
without URL prefix.
|
|
|
|
|
|
© 2005 Active-Venture.com Web
Page Hosting
Service
|
|
|
|
 |
|
< Do what you think is interesting, do something that you think is fun and worthwhile, because otherwise you won't do it well anyway.
> |
|
|
| |
|
Disclaimer: This
documentation is provided only for the benefits of our hosting customers.
For authoritative source of the documentation, please refer to http://httpd.apache.org/docs/
|
|
|