How do websites share the same IP address?

5,985

Solution 1

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

Solution 2

  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..
Share:
5,985

Related videos on Youtube

PacketRider
Author by

PacketRider

Updated on September 18, 2022

Comments

  • PacketRider
    PacketRider over 1 year

    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?