How Multiple-Site Hosted Web Servers Route Content

Introduction

When hosting multiple websites on a single server, web servers determine which site to serve based on the requested domain name. This process works differently for HTTP and HTTPS. For HTTP, the server uses the Host header in the HTTP request, while for HTTPS, the server relies on the Server Name Indication (SNI) field in the SSL/TLS handshake.

Routing for HTTP

In an HTTP request, the browser sends a Host header containing the domain name. The server uses this header to route the request to the appropriate virtual host configuration.

Example Configuration

Apache Virtual Hosts:

<VirtualHost *:80>
    ServerName www.example1.com
    DocumentRoot /var/www/example1
</VirtualHost>

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

NGINX Server Blocks:

server {
    listen 80;
    server_name www.example1.com;
    root /var/www/example1;
}

server {
    listen 80;
    server_name www.example2.com;
    root /var/www/example2;
}

Routing for HTTPS

For HTTPS, the web server cannot rely on the HTTP Host header because the request is encrypted. Instead, it uses the Server Name Indication (SNI) field in the TLS Client Hello packet to identify the domain name and present the correct SSL certificate.

Example Configuration

Apache Virtual Hosts with SSL:

<VirtualHost *:443>
    ServerName www.example1.com
    DocumentRoot /var/www/example1

    SSLEngine on
    SSLCertificateFile /path/to/example1.crt
    SSLCertificateKeyFile /path/to/example1.key
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example2.com
    DocumentRoot /var/www/example2

    SSLEngine on
    SSLCertificateFile /path/to/example2.crt
    SSLCertificateKeyFile /path/to/example2.key
</VirtualHost>

NGINX Server Blocks with SSL:

server {
    listen 443 ssl;
    server_name www.example1.com;
    root /var/www/example1;

    ssl_certificate /path/to/example1.crt;
    ssl_certificate_key /path/to/example1.key;
}

server {
    listen 443 ssl;
    server_name www.example2.com;
    root /var/www/example2;

    ssl_certificate /path/to/example2.crt;
    ssl_certificate_key /path/to/example2.key;
}

Conclusion

By leveraging the Host header for HTTP and the SNI field for HTTPS, web servers like Apache and NGINX can efficiently route requests to the appropriate website, even when hosting multiple sites on the same server. This setup is essential for modern web hosting scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *