When specifying a "host name", do we still need to specify a port?

Solution 1:

Host names do not correspond to an {ipaddress,port} tuple. A host name is only the name of a server, which should be resolvable to one or more IP addresses. Ports have nothing to do with host names at all.

Solution 2:

A "virtual host" is simply a feature of a piece of software which takes advantage of extra context in a request to act differently.

An important thing to note is that TCP/IP itself does not know anything about host names; their main purpose is as a way to find IP addresses.

The classic example is an HTTP Server using name-based virtual hosting, which works like this:

  • The user requests a URL. The domain name is looked up in DNS, to find an IP address.
  • A TCP connection is opened to a particular IP address and port. (For HTTP, this defaults to port 80; for HTTPS, port 443).
  • The client sends a request on that connection which includes whatever information is specified by the protocol being used.
    • In HTTP 1.1 this includes the "Host" header, which is the domain name the user looked up to find the IP address.
    • For a secure connection, the TLS handshake can include a "Server Name Indication" field, so that this is available before certificates are agreed.
  • The server software listening on the IP address and port now has all three pieces of information: IP address, port number, and host name; it can use these to determine which configuration to apply to the request. This configuration is the "virtual host".

Solution 3:

Host names are handled by DNS (or other name resolution like a hostfile). Webservers listen on IPs/Sockets, but when running virtual hosts they also look at the request header for what FQDN was used to request the page.

When a web server running virtual hosts responds to a request, it looks at the request header to see if the request is from a host it knows about, then serves up the correct page. i.e. if i have a server with virtual host for initech.xyz, DNS will point it to the IP of my web server, which is listening on the default http/s ports (80/443). Based on the configuration you can turn on/off different ports that each virtual host responds to, but from an IP/port perspective, if the port is enabled, it's open.

Also DNS can have multiple IPs resolving to the same name, and vice versa.