Skip to main content

Virtual hosting types; name-based steps

Short answer

Types: IP-based (separate IPs), port-based (different ports), name-based (HTTP Host header). Major steps for name-based: create DNS records, DocumentRoot, <VirtualHost *:80> blocks with ServerName, then restart Apache.

Explain different types of web virtual hosting mechanism with an example. Write down the major steps while configuring named based virtual hosting.

Types of web virtual hosting

Web virtual hosting means running many web sites (domains) on one physical server. There are three mechanisms.

TypeDistinguished byIPs neededExample
IP-basedDestination IP addressOne IP per site192.168.1.10 -> site A, 192.168.1.11 -> site B
Port-basedTCP port numberOne IP, many portswww.a.com:80, www.a.com:8080
Name-basedHost header of the HTTP requestOne IP for all siteswww.a.com and www.b.com on 192.168.1.10

1. IP-based virtual hosting. The server has several IP addresses (several NICs or IP aliases on one NIC). Apache looks at the IP the request arrived on and serves the matching site. Works with every browser and with SSL/TLS, but wastes IP addresses.

<VirtualHost 192.168.1.10:80>
    ServerName www.a.com
    DocumentRoot /var/www/a
</VirtualHost>
<VirtualHost 192.168.1.11:80>
    ServerName www.b.com
    DocumentRoot /var/www/b
</VirtualHost>

2. Port-based virtual hosting. One IP address, each site listens on a different port. Users must type the port (http://www.a.com:8080), so it is used mainly for testing or internal sites.

Listen 80
Listen 8080
<VirtualHost *:80>
    DocumentRoot /var/www/a
</VirtualHost>
<VirtualHost *:8080>
    DocumentRoot /var/www/b
</VirtualHost>

3. Name-based virtual hosting. Many domain names resolve to the same IP. The browser sends the site name in the HTTP/1.1 Host: header, and the server selects the site by it. It saves IP addresses and is the most common method.

 Client --GET / HTTP/1.1--> 192.168.1.10:80
          Host: www.a.com        |
                          +------+------+
                     /var/www/a    /var/www/b

Major steps to configure name-based virtual hosting (Apache)

  1. Register or set up DNS: create A records so www.a.com and www.b.com both point to the server IP (e.g. 192.168.1.10). On a lab network, edit /etc/hosts.
  2. Create document roots and index pages: mkdir -p /var/www/a /var/www/b and put index.html in each.
  3. Enable name-based hosting (Apache 2.2: NameVirtualHost *:80; Apache 2.4 does this automatically).
  4. Write a <VirtualHost> block for each site with ServerName, ServerAlias, DocumentRoot and log files.
<VirtualHost *:80>
    ServerName www.a.com
    ServerAlias a.com
    DocumentRoot /var/www/a
    ErrorLog logs/a-error.log
    CustomLog logs/a-access.log combined
</VirtualHost>
<VirtualHost *:80>
    ServerName www.b.com
    DocumentRoot /var/www/b
</VirtualHost>
  1. Check syntax: apachectl configtest (or httpd -t).
  2. Restart/reload the service: systemctl restart httpd (or apache2).
  3. Test from a browser or curl -H "Host: www.b.com" http://192.168.1.10/. The first block is the default site for unknown names.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Internet and Intranet chapter 4 questions →