Web site hosting and cheap domain registration services
  

Apache HTTP Server Version 1.3

Virtual Host examples for common setups

Base configuration

Additional features


Simple name-based vhosting

  • Compatibility: This syntax was added in Apache 1.3.13.
  • Setup: The server machine has a primary name server.domain.tld. There are two aliases (CNAMEs) www.domain.tld and www.sub.domain.tld for the address server.domain.tld.

    Server configuration:

        ...
        Port 80
        ServerName server.domain.tld
    
        NameVirtualHost *
    
        <VirtualHost *>
        DocumentRoot /www/domain
        ServerName www.domain.tld
        ...
        </VirtualHost>
        
        <VirtualHost *>
        DocumentRoot /www/subdomain
        ServerName www.sub.domain.tld
        ...
        </VirtualHost> 
       
    

    The asterisks match all addresses, so the main server serves no requests. Due to the fact that www.domain.tld is first in the configuration file, it has the highest priority and can be seen as the default or primary server.


More complicated name-based vhosts

  • Setup 1: The server machine has one IP address (111.22.33.44) which resolves to the name server.domain.tld. There are two aliases (CNAMEs) www.domain.tld and www.sub.domain.tld for the address 111.22.33.44.

    Server configuration:

        ...
        Port 80
        ServerName server.domain.tld
    
        NameVirtualHost 111.22.33.44 
    
        <VirtualHost 111.22.33.44>
        DocumentRoot /www/domain
        ServerName www.domain.tld
        ...
        </VirtualHost>
        
        <VirtualHost 111.22.33.44>
        DocumentRoot /www/subdomain
        ServerName www.sub.domain.tld
        ...
        </VirtualHost> 
       
    

    Apart from localhost there are no unspecified addresses/ports, therefore the main server only serves localhost requests. Due to the fact that www.domain.tld has the highest priority it can be seen as the default or primary server.

  • Setup 2: The server machine has two IP addresses (111.22.33.44 and 111.22.33.55) which resolve to the names server1.domain.tld and server2.domain.tld respectively. The alias www.domain.tld should be used for the main server which should also catch any unspecified addresses. We want to use a virtual host for the alias www.otherdomain.tld and another virtual host, with server name www.sub.domain.tld, should catch any request to hostnames of the form *.sub.domain.tld. The address 111.22.33.55 should be used for the virtual hosts.

    Server configuration:

        ...
        Port 80
        ServerName www.domain.tld
        DocumentRoot /www/domain
    
        NameVirtualHost 111.22.33.55
    
        <VirtualHost 111.22.33.55>
        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> 
       
    

    Any request to an address other than 111.22.33.55 will be served from the main server. A request to 111.22.33.55 with an unknown or no Host: header will be served from www.otherdomain.tld.

  • Setup 3: The server machine has two IP addresses (192.168.1.1 and 111.22.33.55). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name server1.domain.tld resolves to the external address (111.22.33.55), but inside the network, that same name resolves to the internal address (192.168.1.1).

    The server can be made to respond to internal and external requests with the same content, with just one VirtualHost section.

    Server configuration:

        ...
        NameVirtualHost 192.168.1.1
        NameVirtualHost 111.22.33.55
    
        <VirtualHost 192.168.1.1 111.22.33.55>
        DocumentRoot /www/server1
        ServerName server1.domain.tld
        ServerAlias server1
        ...
        </VirtualHost>
       
    

    Now requests from both networks will be served from the same VirtualHost

  • Setup 4: You have multiple domains going to the same IP and also want to serve multiple ports. By defining the ports in the "NameVirtualHost" tag, you can allow this to work. If you try using <VirtualHost name:port> without the NameVirtualHost name:port or you try to use the Port directive, your configuration will not work.

    Server configuration:

        ...   
        NameVirtualHost 111.22.33.44:80
        NameVirtualHost 111.22.33.44:8080
    
        <VirtualHost 111.22.33.44:80>
        ServerName www.domain.tld
        DocumentRoot /www/domain-80
        </VirtualHost>
    
        <VirtualHost 111.22.33.44:8080>
        ServerName www.domain.tld
        DocumentRoot /www/domain-8080
        </VirtualHost>
    
        <VirtualHost 111.22.33.44:80>
        ServerName www.otherdomain.tld
        DocumentRoot /www/otherdomain-80
        </VirtualHost>
    
        <VirtualHost 111.22.33.44:8080>
        ServerName www.otherdomain.tld
        DocumentRoot /www/otherdomain-8080
        </VirtualHost>
    
       
    

IP-based vhosts

  • Setup 1: The server machine has two IP addresses (111.22.33.44 and 111.22.33.55) which resolve to the names server.domain.tld and www.otherdomain.tld respectively. The hostname www.domain.tld is an alias (CNAME) for server.domain.tld and will represent the main server.

    Server configuration:

        ...
        Port 80
        DocumentRoot /www/domain
        ServerName www.domain.tld
    
        <VirtualHost 111.22.33.55>
        DocumentRoot /www/otherdomain
        ServerName www.otherdomain.tld
        ...
        </VirtualHost>
       
    

    www.otherdomain.tld can only be reached through the address 111.22.33.55, while www.domain.tld can only be reached through 111.22.33.44 (which represents our main server).

  • Setup 2: Same as setup 1, but we don't want to have a dedicated main server.

    Server configuration:

        ...
        Port 80
        ServerName server.domain.tld
        
        <VirtualHost 111.22.33.44>
        DocumentRoot /www/domain
        ServerName www.domain.tld
        ...
        </VirtualHost>
    
        <VirtualHost 111.22.33.55>
        DocumentRoot /www/otherdomain
        ServerName www.otherdomain.tld
        ...
        </VirtualHost>
       
    

    The main server can never catch a request, because all IP addresses of our machine are in use for IP-based virtual hosts (only localhost requests can hit the main server).

  • Setup 3: The server machine has two IP addresses (111.22.33.44 and 111.22.33.55) which resolve to the names server.domain.tld and www-cache.domain.tld respectively. The hostname www.domain.tld is an alias (CNAME) for server.domain.tld and will represent the main server. www-cache.domain.tld will become our proxy-cache listening on port 8080, while the web server itself uses the default port 80.

    Server configuration:

        ...
        Port 80
        Listen 111.22.33.44:80
        Listen 111.22.33.55:8080
        ServerName server.domain.tld
        
        <VirtualHost 111.22.33.44:80>
        DocumentRoot /www/domain
        ServerName www.domain.tld
        ...
        </VirtualHost>
    
        <VirtualHost 111.22.33.55:8080>
        ServerName www-cache.domain.tld
        ...
          <Directory proxy:>
          Order Deny,Allow
          Deny from all
          Allow from 111.22.33
          </Directory>
        </VirtualHost>
       
    

    The main server can never catch a request, because all IP addresses (apart from localhost) 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.


Mixed name-/IP-based vhosts

  • Setup: The server machine has three IP addresses (111.22.33.44, 111.22.33.55 and 111.22.33.66) which resolve to the names server.domain.tld, www.otherdomain1.tld and www.otherdomain2.tld respectively. The address 111.22.33.44 should be used for a couple of name-based vhosts and the other addresses for IP-based vhosts.

    Server configuration:

        ...
        Port 80
        ServerName server.domain.tld
    
        NameVirtualHost 111.22.33.44
    
        <VirtualHost 111.22.33.44>
        DocumentRoot /www/domain
        ServerName www.domain.tld
        ...
        </VirtualHost>
       
        <VirtualHost 111.22.33.44>
        DocumentRoot /www/subdomain1
        ServerName www.sub1.domain.tld
        ...
        </VirtualHost> 
        
        <VirtualHost 111.22.33.44>
        DocumentRoot /www/subdomain2
        ServerName www.sub2.domain.tld
        ...
        </VirtualHost> 
     
        <VirtualHost 111.22.33.55>
        DocumentRoot /www/otherdomain1
        ServerName www.otherdomain1.tld
        ...
        </VirtualHost> 
        
        <VirtualHost 111.22.33.66>
        DocumentRoot /www/otherdomain2
        ServerName www.otherdomain2.tld
        ...
        </VirtualHost>     
       
    

Port-based vhosts

  • Setup: The server machine has one IP address (111.22.33.44) which resolves to the name www.domain.tld. 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.

    Server configuration:

        ...
        Listen 80
        Listen 8080
        ServerName www.domain.tld
        DocumentRoot /www/domain
    
        <VirtualHost 111.22.33.44:8080>
        DocumentRoot /www/domain2
        ...
        </VirtualHost>
       
    

    A request to www.domain.tld on port 80 is served from the main server and a request to port 8080 is served from the virtual host.

 

 

© 2005 Active-Venture.com Web Page Hosting Service

 Cheap domain registrar and domain transfer | Cheap domain registration or renewal | buy cheap web hosting services 

< Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.   >

 

 
 

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/