Is a web server able to know the hostname which was resolved to the IP address of the web server? [closed]

For a web server, name based virtual hosting uses hostname specified in the Host header in a HTTP request received by the server.

Is a web server able to know the hostname which was resolved to the IP address of the web server?

If yes, does that mean a web server may as well use the hostname which has been resolved to the IP address of the web server, for virtual hosting?

How can a web server know the hostname which has been resolved to the IP address of the web server?

Thanks.

Answer

Web servers don’t care about how a client found them, they only care about what (if any) host name is contained in HTTP requests.

The HTTP protocol provides means (called “host headers”) to ask a web server “give me the web site for www.host1.com” or “now give me the one for www.host2.com“, and this is what the web server uses to select which web content to answer with, regardless of how the actual network connection has been established.

Please note that there is no relationship at all between HTTP host headers and DNS; usually a client (such as a web browser) will ask the DNS for which IP address corresponds to www.host1.com, then it will connect to that IP address, and then it will make a HTTP request asking the web server for www.host1.com; but the two things have no direct relationship and there can be several different scenarios, such as clients which can craft custom HTTP requests, or hostname-to-IP mappings performed by a client’s local hosts file, or even proxies (or reverse proxies) that can make requests on behalf of someone else and tweak them while in transit.


Case in point: exactly today I was testing a web site which needs to be called with a specific host name; in order to test it from my web browser, I had to map its IP address to that host name in my computer’s hosts file, since the real DNS entry for the web site pointed elsewhere. This way, when I typed www.mysite.com in my browser, it connected to that server’s IP address and then actually asked it for www.mysite.com. DNS was completely bypassed (and rightly so, because I wanted to connect to the test site, not to the real one).

Attribution
Source : Link , Question Author : Tim , Answer Author : Massimo

Leave a Comment