7

I have always been confused by the idea that multiple websites sharing the same IP address. I mean if there is a webserver out there that has an IP address. I can simply key in this IP address with the right port such as 80 and the destination gateway knows which server to forward this incoming request. This case, port 80 is the webserver and the website is served out to the client.

However, I do not understand how two websites can be hosted on the webserver and one IP address. Let's say the two websites with names of abc.com and xyz.com are hosted on the same webserver with one IP address. The DNS server that I use such as one from my ISP can resolve the names with the IP adress just fine, pointing my web browser to the correct webserver, but my confusion is how the webserver know which website to serve out?

I always thought that the only way to differentiate between different websites on the same webserver is their respective port numbers. In other words, if the incoming request is on port 80, it's abc.com and if it is port 81 it is xyz.com. Obviously, I am not putting in xyz.com:81 on my web browser so it uses port 80 by default. Then why do I get the actual xyz.com website in return and not abc.com?

3 Answers 3

13

The web server software looks at the hostname in the HTTP request and uses that to determine which website to serve. For example, Apache has the NameVirtualHost configuration option which controls this behaviour. You can find a detailed explanation of how this process works in its documentation: https://httpd.apache.org/docs/2.2/vhosts/name-based.html

6
  1. Web client, usually a browser, opens a TCP socket to the server.
  2. The server software accepts the connection without knowing the specific site requested, and waits for an HTTP request to happen.
  3. The client sends the request, mainly composed of HTTP headers. One of these headers is the Host: example.com header, at this point the server is aware of the right website this request is meant to, and executes the request according to the corresponding host settings... Be it a server block in nginx, or a virtual host definition in apache..
0

Without going into the actual mechanism in details, basically the DNS server that resolves the abc.com and xyz.com domains to one IP address that both domains share will give this shared IP address back to the web browser. The web browser then makes an HTTP request to this IP address and eventually reaches the webserver hosting these two (and perhaps many more) websites. Moreover, since this is an HTTP request, there is additional information than just the IP address when this request reaches the webserver, namely a header packet with the actual name abc.com or xyz.com or whatever other domains that this single webserver hosts. Using this HTTP header packet, the webserver knows exactly what website to serve out. HTTP encapsulates the data packets which include some ASCII text information in addition to the IP address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.