0

Can anyone explain what is the benefit of the port portion of the "Host" header in HTTP/1.1? Is it so that we can have two or more websites with the same names (but different ports)?

2 Answers2

0

Most of the internet website traffic is done on standard port 80. For this case, a port number can (according to RFC should) be omitted. HTTPS traffic is usually done on port 443 in which case a browser will include it by default. Connection to any other port is a fairly special case (mostly for development or separation of services).

So yes, if you want to have two different sites with the same (sub)domain on a server, you would have to either distinguish them by port or some kind of cookie trickery. But the most common examples of using ports in HTTP communication is a multi-application server, where port determines which applications API should be used.

  • But why is this header even necessary? Having received and accepted the TCP connection, the web server obviously *already knows* which port is being used... – Massimo Jul 19 '16 at 12:01
  • The port number used as a target of TCP connection from browser and port number of destination server doesn't have to be same. If you use any HTTP proxy for example, you are always connecting to some defined port (let's say 8080) no mather what kind of traffic is going through. The proxy needs to know to which destination port it should relay the request. – BorysekOndrej Jul 19 '16 at 12:08
0

It is required by the RFCs, a point previously made here: https://stackoverflow.com/questions/3364144/is-port-number-required-in-http-host-header-parameter

The question was why, and the RFC's requirement does seem a bit contrary to normal layering. After all, the port is a property of the transport connection and not of the application (or session) layers.

However I would speculate that carrying this property forward into the application layer may have been seen to be desirable to allow multiple sites to be handled on a single IP address, and including the port specifier here does allow the freedom to create website software that separates the TCP communications process from the web application whilst still being able to use the incoming port number if required.

marctxk
  • 329
  • 1
  • 4