<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML><HEAD>
<TITLE>VirtualHost Examples</TITLE>
</HEAD>

<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
<BODY
 BGCOLOR="#FFFFFF"
 TEXT="#000000"
 LINK="#0000FF"
 VLINK="#000080"
 ALINK="#FF0000"
>
<DIV ALIGN="CENTER">
 <IMG SRC="../images/sub.gif" ALT="[APACHE DOCUMENTATION]">
 <H3>
  Apache HTTP Server Version 1.3
 </H3>
</DIV>

<H1 ALIGN="CENTER">Virtual Host examples for common setups</H1>


<H2>Base configuration</H2>

<UL>
<LI><A HREF="#ip">IP-based vhosts only</A>
<LI><A HREF="#name">Name-based vhosts only</A>
<LI><A HREF="#mixed">Mixed name-/IP-based vhosts</A>
<LI><A HREF="#port">Port-based vhosts</A>
</UL>

<H2>Additional features</H2>

<UL>
<LI><A HREF="#default">Using <CODE>_default_</CODE> vhosts</A>
<LI><A HREF="#migrate">Migrating a named-based vhost to an IP-based vhost</A>
<LI><A HREF="#serverpath">Using the <CODE>ServerPath</CODE> directive</A>
</UL>

<HR>

<H3><A NAME="ip">IP-based vhosts only</A></H3>

<UL>

<LI><STRONG>Setup 1:</STRONG>
    The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
    and <SAMP>111.22.33.55</SAMP>)
    which resolve to the names <SAMP>server.domain.tld</SAMP> and
    <SAMP>www.otherdomain.tld</SAMP> respectively.
    The hostname <SAMP>www.domain.tld</SAMP> is an alias (CNAME)
    for <SAMP>server.domain.tld</SAMP> and will represent the
    main server.
    <P>
    <STRONG>Server configuration:</STRONG>


    <BLOCKQUOTE><PRE>
    ...
    Port 80
    DocumentRoot /www/domain
    ServerName www.domain.tld

    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/otherdomain
    ServerName www.otherdomain.tld
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    <SAMP>www.otherdomain.tld</SAMP> can only be reached through the
    address <SAMP>111.22.33.55</SAMP>, while <SAMP>www.domain.tld</SAMP>
    can only be reached through <SAMP>111.22.33.44</SAMP>
    (which represents our main server).
    </BLOCKQUOTE>
    <P>

<LI><STRONG>Setup 2:</STRONG>
    Same as setup 1, but we don't want to have a dedicated main server.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName server.domain.tld
    
    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/domain
    ServerName www.domain.tld
    ...
    &lt;/VirtualHost&gt;

    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/otherdomain
    ServerName www.otherdomain.tld
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    The main server can never catch a request, because all IP addresses
    of our machine are in use for IP-based virtual hosts
    (only <SAMP>localhost</SAMP> requests can hit the main server).
    </BLOCKQUOTE>
    <P>
    
<LI><STRONG>Setup 3:</STRONG>
    The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
    and <SAMP>111.22.33.55</SAMP>)
    which resolve to the names <SAMP>server.domain.tld</SAMP> and
    <SAMP>www-cache.domain.tld</SAMP> respectively.
    The hostname <SAMP>www.domain.tld</SAMP> is an alias (CNAME)
    for <SAMP>server.domain.tld</SAMP> and will represent the
    main server.
    <SAMP>www-cache.domain.tld</SAMP> will become our proxy-cache 
    listening on port 8080, while the web server itself uses the default
    port 80.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    Listen 111.22.33.44:80
    Listen 111.22.33.55:8080
    ServerName server.domain.tld
    
    &lt;VirtualHost 111.22.33.44:80&gt;
    DocumentRoot /www/domain
    ServerName www.domain.tld
    ...
    &lt;/VirtualHost&gt;

    &lt;VirtualHost 111.22.33.55:8080&gt;
    ServerName www-cache.domain.tld
    ...
      &lt;Directory proxy:&gt;
      order deny,allow
      deny from all
      allow from 111.22.33
      &lt;/Directory&gt;
    &lt;/VirtualHost&gt;
    </PRE>
    The main server can never catch a request, because all IP addresses
    (apart from <SAMP>localhost</SAMP>) of our machine are in use for IP-based
    virtual hosts. The web server can only be reached on the first address
    through port 80 and the proxy only on the second address through port 8080.
    </BLOCKQUOTE>
</UL>
<HR>

<H3><A NAME="name">Name-based vhosts only</A></H3>

<UL>

<LI><STRONG>Setup 1:</STRONG>
    The server machine has one IP address (<SAMP>111.22.33.44</SAMP>)
    which resolves to the name <SAMP>server.domain.tld</SAMP>.
    There are two aliases (CNAMEs) <SAMP>www.domain.tld</SAMP> and
    <SAMP>www.sub.domain.tld</SAMP> for the address <SAMP>111.22.33.44</SAMP>.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName server.domain.tld

    NameVirtualHost 111.22.33.44 

    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/domain
    ServerName www.domain.tld
    ...
    &lt;/VirtualHost&gt;
    
    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain
    ServerName www.sub.domain.tld
    ...
    &lt;/VirtualHost&gt; 
    </PRE>
    Apart from <SAMP>localhost</SAMP> there are no unspecified
    addresses/ports, therefore the main server only serves
    <SAMP>localhost</SAMP> requests. Due to the fact
    that <SAMP>www.domain.tld</SAMP> has the highest priority
    it can be seen as the <CITE>default</CITE> or
    <CITE>primary</CITE> server.
    </BLOCKQUOTE>
    <P>
   
<LI><STRONG>Setup 2:</STRONG>
    The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
    and <SAMP>111.22.33.55</SAMP>)
    which resolve to the names <SAMP>server1.domain.tld</SAMP> and
    <SAMP>server2.domain.tld</SAMP> respectively.
    The alias <SAMP>www.domain.tld</SAMP> should be used for the
    main server which should also catch any unspecified addresses. 
    We want to use a virtual host for the alias
    <SAMP>www.otherdomain.tld</SAMP> and one virtual host should
    catch any request to hostnames of the form
    <SAMP>*.sub.domain.tld</SAMP> with <SAMP>www.sub.domain.tld</SAMP>
    as its server name. The address <SAMP>111.22.33.55</SAMP> should be
    used for the virtual hosts.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName www.domain.tld
    DocumentRoot /www/domain

    NameVirtualHost 111.22.33.55

    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/otherdomain
    ServerName www.otherdomain.tld
    ...
    &lt;/VirtualHost&gt;
   
    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/subdomain
    ServerName www.sub.domain.tld
    ServerAlias *.sub.domain.tld
    ...
    &lt;/VirtualHost&gt; 
    </PRE>
    Any request to an address other than <SAMP>111.22.33.55</SAMP>
    will be served from the main server. A request to
    <SAMP>111.22.33.55</SAMP> with an unknown or no <CODE>Host:</CODE>
    header will be served from <SAMP>www.otherdomain.tld</SAMP>.
    </BLOCKQUOTE>
</UL>

<HR>

<H3><A NAME="mixed">Mixed name-/IP-based vhosts</A></H3>

<UL>

<LI><STRONG>Setup:</STRONG>
    The server machine has three IP addresses (<SAMP>111.22.33.44</SAMP>,
    <SAMP>111.22.33.55</SAMP> and <SAMP>111.22.33.66</SAMP>)
    which resolve to the names <SAMP>server.domain.tld</SAMP>,
    <SAMP>www.otherdomain1.tld</SAMP> and <SAMP>www.otherdomain2.tld</SAMP>
    respectively.
    The address <SAMP>111.22.33.44</SAMP> should we used for a couple
    of name-based vhosts and the other addresses for IP-based vhosts. 
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName server.domain.tld

    NameVirtualHost 111.22.33.44

    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/domain
    ServerName www.domain.tld
    ...
    &lt;/VirtualHost&gt;
   
    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain1
    ServerName www.sub1.domain.tld
    ...
    &lt;/VirtualHost&gt; 
    
    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain2
    ServerName www.sub2.domain.tld
    ...
    &lt;/VirtualHost&gt; 
 
    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/otherdomain1
    ServerName www.otherdomain1.tld
    ...
    &lt;/VirtualHost&gt; 
    
    &lt;VirtualHost 111.22.33.66&gt;
    DocumentRoot /www/otherdomain2
    ServerName www.otherdomain2.tld
    ...
    &lt;/VirtualHost&gt;     
    </PRE></BLOCKQUOTE>

</UL>

<HR>

<H3><A NAME="port">Port-based vhosts</A></H3>

<UL>

<LI><STRONG>Setup:</STRONG>
    The server machine has one IP address (<SAMP>111.22.33.44</SAMP>)
    which resolves to the name <SAMP>www.domain.tld</SAMP>.
    If we don't have the option to get another address or alias
    for our server we can use port-based vhosts if we need
    a virtual host with a different configuration.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Listen 80
    Listen 8080
    ServerName www.domain.tld
    DocumentRoot /www/domain

    &lt;VirtualHost 111.22.33.44:8080&gt;
    DocumentRoot /www/domain2
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    A request to <SAMP>www.domain.tld</SAMP> on port 80 is served
    from the main server and a request to port 8080 is served from
    the virtual host.
    </BLOCKQUOTE>
</UL> 
   
<HR>

<H3><A NAME="default">Using <CODE>_default_</CODE> vhosts</A></H3>

<UL>

<LI><STRONG>Setup 1:</STRONG> 
    Catching <EM>every</EM> request to any unspecified IP address and port,
    <EM>i.e.</EM>, an address/port combination that is not used for any other
    virtual host.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    &lt;VirtualHost _default_:*&gt;
    DocumentRoot /www/default
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    Using such a default vhost with a wildcard port effectively
    prevents any request going to the main server.<BR>
    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 <CODE>Host:</CODE> header it is
    always served from the primary name-based vhost (the
    vhost for that address/port appearing first in the configuration
    file).<BR>
    You can use
    <A HREF="../mod/mod_alias.html#aliasmatch"><CODE>AliasMatch</CODE></A>
    or
    <A HREF="../mod/mod_rewrite.html#RewriteRule"><CODE>RewriteRule</CODE></A>
    to rewrite any request to a single information page (or script).
    </BLOCKQUOTE>
    <P>
    
<LI><STRONG>Setup 2:</STRONG> 
    Same as setup 1, but the server listens on several ports and
    we want to use a second <CODE>_default_</CODE> vhost for port 80.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    &lt;VirtualHost _default_:80&gt;
    DocumentRoot /www/default80
    ...
    &lt;/VirtualHost&gt;
    
    &lt;VirtualHost _default_:*&gt;
    DocumentRoot /www/default
    ...
    &lt;/VirtualHost&gt;    
    </PRE>
    The default vhost for port 80 (which <EM>must</EM> 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.
    </BLOCKQUOTE>    
    <P>
    
<LI><STRONG>Setup 3:</STRONG> 
    We want to have a default vhost for port 80, but no other default vhosts.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    &lt;VirtualHost _default_:80&gt;
    DocumentRoot /www/default
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    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.
    </BLOCKQUOTE>

</UL>

<HR>

<H3><A NAME="migrate">Migrating a name-based vhost to an IP-based vhost</A></H3>

<UL>

<LI><STRONG>Setup:</STRONG>
    The name-based vhost with the hostname
    <SAMP>www.otherdomain.tld</SAMP> (from our <A HREF="#name">name-based</A>
    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.<BR>
    The solution is easy, because we can simply add the new IP address
    (<SAMP>111.22.33.66</SAMP>) to the <CODE>VirtualHost</CODE> directive.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName www.domain.tld
    DocumentRoot /www/domain

    NameVirtualHost 111.22.33.55

    &lt;VirtualHost 111.22.33.55 111.22.33.66&gt;
    DocumentRoot /www/otherdomain
    ServerName www.otherdomain.tld
    ...
    &lt;/VirtualHost&gt;
   
    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/subdomain
    ServerName www.sub.domain.tld
    ServerAlias *.sub.domain.tld
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    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).
    </BLOCKQUOTE>

</UL>

<HR>

<H3><A NAME="serverpath">Using the <CODE>ServerPath</CODE> directive</A></H3>

<UL>

<LI><STRONG>Setup:</STRONG>
    We have a server with two name-based vhosts. In order to match the correct
    virtual host a client must send the correct <CODE>Host:</CODE> 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.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    NameVirtualHost 111.22.33.44

    &lt;VirtualHost 111.22.33.44&gt;
    # primary vhost
    DocumentRoot /www/subdomain
    RewriteEngine On
    RewriteRule ^/.* /www/subdomain/index.html
    ...
    &lt;/VirtualHost&gt;

    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain/sub1
    ServerName www.sub1.domain.tld
    ServerPath /sub1/
    RewriteEngine On
    RewriteRule ^(/sub1/.*) /www/subdomain$1 
    ...
    &lt;/VirtualHost&gt;

    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain/sub2
    ServerName www.sub2.domain.tld
    ServerPath /sub2/
    RewriteEngine On
    RewriteRule ^(/sub2/.*) /www/subdomain$1 
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    Due to the <A HREF="../mod/core.html#serverpath"><CODE>ServerPath</CODE></A>
    directive a request to the
    URL <SAMP>http://www.sub1.domain.tld/sub1/</SAMP> is <EM>always</EM>
    served from the sub1-vhost. <BR>
    A request to the URL <SAMP>http://www.sub1.domain.tld/</SAMP>
    is only served from the sub1-vhost if the client sent a correct
    <CODE>Host:</CODE> header.
    If no <CODE>Host:</CODE> header is sent the client gets the
    information page from the primary host.<BR>
    Please note that there is one oddity: A request to
    <SAMP>http://www.sub2.domain.tld/sub1/</SAMP> is also served from
    the sub1-vhost if the client sent no <CODE>Host:</CODE> header. <BR>
    The <CODE>RewriteRule</CODE> directives are used to make sure that
    a client which sent a correct <CODE>Host:</CODE> header can use
    both URL variants, <EM>i.e.</EM>, with or without URL prefix.
    </BLOCKQUOTE>

</UL> 

<HR>

<H3 ALIGN="CENTER">
 Apache HTTP Server Version 1.3
</H3>

<A HREF="./"><IMG SRC="../images/index.gif" ALT="Index"></A>
<A HREF="../"><IMG SRC="../images/home.gif" ALT="Home"></A>

</BODY>
</HTML>
