How a Single Computer Can Host and Serve Multiple Domains

When I first learned about hosting, I was taught there is a unique IP address against each domain we purchase and this IP address points to the hosting server. For example, if we register example1.com, it would be configured such that its bound with, say 127.1.2.3 IP address. And a user can access the website with IP address too, for example using http://127.1.2.3/. As the IP addresses are difficult to remember, that is why we use domain names, which are common language words and easy to remember for humans.

About server, we are taught that, a single server can host multiple domain. And we know that each domain is mapped to an IP address, and on internet, an IP address is unique. When we type the domain name into address bar of internet browser, the browser first fetch the hosting server IP address and then send the request to the computer identified by the IP address. Assume, if we are fetching the home page, it seems only send request to server home page contents is enough, assuming there is single domain. But the issue is, how the server computer identify which domain's home page is requested if the computer hosts multiple domains.

If our single computer is hosting 4 domains, we can configure them like this:

Domain                IP Address
example1.com      127.1.2.3
example2.com      127.1.2.3
example3.com      127.1.2.3
example4.com      127.1.2.3

When one computer host only one domain, it makes sense, how http://127.1.2.3/ can be used to access a domain. But if there are multiple domains hosted on single server, (assume domain name is just easy to remember thing, but mapped to an IP address), and what if user access the http://127.1.2.3/ by entering into his address bar in internet browser. How server can decide which domain's home page to serve to user, as the same server is hosting 4 domains.

In other words, if user types example1.com into browser, if browser just transform the domain name into IP address hosting computer using DNS and then sends request to that computer to serve the home page contents of its domain, how the server can identify, which domains' contents should be served to client.

Its answer lies in HTTP protocol understanding

When we type the domain name into the browsers address bar, browser not only search the hosting IP and ask it to serve the domain. Browser also inserts the domain-name into HTTP request packet. So when the computer with IP 127.1.2.3 receives the HTTP request, it checks the packet segment that hold the requested domain name. Here it read the domain name, and search in the list of all domains the servers hosts, when match is found, it sends that domain contents to the browser.

To understand how single server can serve multiple domains, lets see the contents of the packet that is sent to Google server when request google.com domain is made: (assume Google server is hosting multiple domains):



But what if the user tries to access the IP address directly by typing in the browser, if the server hosts multiple domains, which domain the server should send? It depends on the server. We can configure a server to display error message because a specific domain was not specified. But generally we configure the server to serve the default domain contents. 

Comments