6

https://en.wikipedia.org/wiki/Virtual_hosting says

Name-based virtual hosts use multiple host names for the same IP address.

Does a "host name" correspond to an IP address or a pair of IP address and a port?

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

If that matters, consider only in virtual hosting.

Thanks.

3 Answers 3

23

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.

4
  • Thanks. (1) Can you tell me how to make a host name that can be resolve to more than one IP addresses? (2) When specifying a "host name", do we still need to specify a port? Is a host name specified with different ports resolved to the same IP address? Can a host name specified with different ports invoke different servers?
    – Tim
    Feb 11, 2019 at 16:37
  • 3
    @Tim, if you have more questions feel free to create new question(s) in to the platform pressing "Ask question" Feb 11, 2019 at 16:56
  • 2
    @Tim: Resolving of a host name is entirely, fully, completely independent of a port number. As such, there is neither a need nor a possibility to specify a port when resolving a name. (aka "name lookup").
    – MSalters
    Feb 11, 2019 at 17:39
  • 2
    @MSalters almost... SRV records usually specify the port of the service as well as the IP. One record can resolve to multiple ports on the same IP.
    – viraptor
    Feb 12, 2019 at 0:09
9

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".
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.