-5

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.

Tim
  • 1,467
  • 3
  • 25
  • 38
  • 2
    You already answer your own question: http 1.1 made the Host header mandatory because without that a web server can only see to which ip-address a client has connected and not which host name was used. – HBruijn Feb 13 '19 at 22:24
  • Hostception ... Your question makes my head hurt trying to parse it. – user9517 Feb 13 '19 at 22:25
  • @HBruijn Is a web server able to know the hostname which was resolved to the IP address of the web server? – Tim Feb 13 '19 at 22:27
  • Sure, that's what the `Host` header is for. – ceejayoz Feb 13 '19 at 22:27
  • 2
    When was the hostname resolved to the IP address? By whom? Where? – Michael Hampton Feb 13 '19 at 22:27
  • @ceejayoz Really? read https://serverfault.com/a/953570/16981 – Tim Feb 13 '19 at 22:29
  • 1
    @Tim As you were told there, "The webserver doesn't resolve hostnames". Your webserver receives HTTP requests. Those HTTP requests include the `Host` header, which acts as a hint to the webserver which virtualhost to use. – ceejayoz Feb 13 '19 at 22:31
  • 7
    @Tim, you have asked a bunch of questions in the last day more or less covering the same topic. Is there some actual problem you are trying to solve that is prompting all these questions? You might get a lot better results if you **asked your question in the context of a specific problem** instead of all these vague protocol-related questions. You seem to want to know something, but at the moment, we can't figure out what you are trying to learn, or why. – Zoredache Feb 13 '19 at 22:58
  • Possible duplicate of [How does ServerName and ServerAlias work?](https://serverfault.com/questions/520195/how-does-servername-and-serveralias-work) – Jenny D Feb 15 '19 at 09:27

1 Answers1

3

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).

Massimo
  • 68,714
  • 56
  • 196
  • 319
  • Thanks. "Web servers don't care about how a client found them". Furthermore, Is a web server able to know the hostname which was resolved to the IP address of the web server? – Tim Feb 13 '19 at 22:33
  • 1
    @Tim The client sends that to the webserver. In the Host header. As the answer states. – ceejayoz Feb 13 '19 at 22:34
  • @ceejayoz Does the client necessarily put the hostname that it uses for DNS resolving in the Host header of its request? – Tim Feb 13 '19 at 22:35
  • 1
    Duuuuude, c'mon. The user says "show me google.com!" The browser goes "OK, what does google.com resolve to? OK, xxx.xxx.xxx.xxx. I'll send a GET request to xxx.xxx.xxx.xxx with google.com as the Host header". The webserver says "Oh look, a request to xxx.xxx.xxx.xxx with the Host header google.com... I know where to send that!" It's literally that simple. Stop over-complicating it. – ceejayoz Feb 13 '19 at 22:37
  • 1
    @Tim, the client can put anything it wants in the HTTP request. Web browsers usually just ask for what you typed in the address bar, but there are tools that let you craft any HTTP request you want. You can even do it yourself if the server doesn't require SSL, just connect to the web server on TCP port 80 and issue a GET command. – Massimo Feb 13 '19 at 22:40
  • Thanks. Is it correct that a web server is unable to know the hostname which was resolved to the IP address of the web server? – Tim Feb 13 '19 at 22:42
  • 2
    Generally speaking, yes. The web server doesn't know (or care) about how the client found it, it only knows what is explicitly stated in the HTTP request. See my example about using the `hosts` file to make the same request to a different server: the server can't possibly know that I used a `hosts` file instead of the DNS service. – Massimo Feb 13 '19 at 22:45
  • Don't forget SNI! – Boris the Spider Feb 14 '19 at 07:58