https://youtu.be/MmMEbLCt_-c?si=lB3tcuN_ZK7WLXri

Once the DNS directs traffic to the correct server, the web server configuration determines how incoming requests are handled. Web servers like Apache, Nginx, or IIS host multiple websites or applications on a single server through virtual hosting, allowing them to differentiate between domains, subdomains, or separate websites with distinct content.

Subdomains vs. Virtual Hosts

At the core of virtual hosting is the ability to distinguish between multiple websites sharing the same IP address by leveraging the HTTP Host header.

Note: If a virtual host lacks a DNS record, it can still be accessed by modifying the local /etc/hosts (or Windows equivalent) file to manually map the domain name to an IP address.

Configuration Example

Virtual hosts can be configured to use completely different domains. For example, in Apache:

Apache

# Example of name-based virtual host configuration in Apache
<VirtualHost *:80>
    ServerName www.example1.com
    DocumentRoot /var/www/example1
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example2.org
    DocumentRoot /var/www/example2
</VirtualHost>

<VirtualHost *:80>
    ServerName www.another-example.net
    DocumentRoot /var/www/another-example
</VirtualHost>

Server VHost Lookup Process

The Host header functions as a switch, enabling the web server to dynamically determine which website to serve.

  1. Browser Requests a Website: The user enters a domain, initiating an HTTP request to the IP address associated with that domain.
  2. Host Header Reveals the Domain: The browser includes the domain name in the request's Host header.
  3. Web Server Determines the Virtual Host: The server receives the request, examines the Host header, and consults its configuration to find a matching entry.
  4. Serving the Right Content: The server retrieves the corresponding files from the specific document root and sends them back as the HTTP response.

Types of Virtual Hosting