• 17/10/2003

VirtualHost Examples

  • 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 
    
        
        DocumentRoot /www/domain
        ServerName www.domain.tld
        ...
        
        
        
        DocumentRoot /www/subdomain
        ServerName www.sub.domain.tld
        ...
         
        

    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
      
          
          DocumentRoot /www/otherdomain
          ServerName www.otherdomain.tld
          ...
          
         
          
          DocumentRoot /www/subdomain
          ServerName www.sub.domain.tld
          ServerAlias *.sub.domain.tld
          ...
           
          

      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
      
          
          DocumentRoot /www/server1
          ServerName server1.domain.tld
          ServerAlias server1
          ...
          
          

      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 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
      
          
          ServerName www.domain.tld
          DocumentRoot /www/domain-80
          
      
          
          ServerName www.domain.tld
          DocumentRoot /www/domain-8080
          
      
          
          ServerName www.otherdomain.tld
          DocumentRoot /www/otherdomain-80
          
      
          
          ServerName www.otherdomain.tld
          DocumentRoot /www/otherdomain-8080
          
      
          
  • Close Bitnami banner
    Bitnami