Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP: Why do you need to specify hostname?

Tags:

http

I'm starting to learn sockets, and I'm trying to figure out why you need to specify the hostname in the request. If I am already connected to "www.google.com", then what is the point of "Host: www.google.com\r\n" in the request? Doesn't the server already know its own name?

like image 540
john smith Avatar asked Nov 26 '11 10:11

john smith


People also ask

Why is hostname needed?

The hostname is used to distinguish devices within a local network. In addition, computers can be found by others through the hostname, which enables data exchange within a network, for example. Hostnames are used on the internet as part of the fully qualified domain name.

What is hostname in HTTP request?

HTTP 1.1 requests often include a Host: header, which contains the hostname from the client request. This is because a server may use a single IP address or interface to accept requests for multiple DNS hostnames. The Host: header identifies the server requested by the client.

Is the hostname the same as the server name?

host name just means the name of the host. server is also a host ...... Not necessarily the same. Multiple servers (in the sense of binaries that respond to network communication, e.g. a web server, a mail server) can run on the same host (in the sense of a physical or virtual machine).

What guidelines should be followed when choosing a hostname for a device?

The first character must be an alphabetic character and the last character must not be a minus sign or period. It was also recommended that the host name be no longer than 24 characters in length. Subsequently, in “Requirements for Internet Hosts – Application and Support“, the host name rules were updated.


2 Answers

Sometimes the server does know its hostname, but it isn't always a 1:1 relationship. Many DNS hostnames can all point to the same IP, thus, 1 IP can have many DNS hostnames. (Also, a single DNS hostname can map to many IP addresses, but this is less important in this question.)

The Host: line in an HTTP request allows a web server to know which hostname you requested, and serve based on that. This allows one machine at an IP address to serve many domains.

For example, if a webserver sees Host: foo.com, it might serve one website, but Host: bar.com might result in a completely different result being returned. There is no other piece of data available to the webserver with this information, and it relies on the web client to inform it.

like image 173
Thanatos Avatar answered Nov 07 '22 12:11

Thanatos


A given server can have many different DNS names. Think shared hosting sites for example.

like image 29
Mat Avatar answered Nov 07 '22 14:11

Mat