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

A technical prerequisite needed for name-based virtual hosts is a web browser with HTTP/1.1 support (commonplace today) to include the target hostname in the request. This allows a server hosting multiple sites behind one IP address to deliver the correct site's content. More specifically it means setting the Host HTTP header, which is mandatory in HTTP/1.1.[2]

For instance, a server could be receiving requests for two domains, www.example.com and www.example.net, both of which resolve to the same IP address. For www.example.com, the server would send the HTML file from the directory /var/www/user/Joe/site/, while requests for www.example.net would make the server serve pages from /var/www/user/Mary/site/. Equally two subdomains of the same domain may be hosted together. For instance, a blog server may host both blog1.example.com and blog2.example.com.

When several name-based virtual hosts on the same web server share the same IP, do they necessarily share the same port?



No, the port number is completely separate. Think of the hostname/IP address as an office's main telephone number, and the port number as the extension you dial after you're connected to the main number. You can have multiple different server processes listening on the same IP address, each on a different port number, just like there would be multiple employees at the company each answering their own extension.

To take this metaphor even further, each of those employees would answer the phone identifying themselves with a different company name depending on which company you said you were calling (i.e. which of the multiple virtual hostnames you used to connect). This actually happens with answering services, they can see the number that you dialed before they answer, and when they pick up the phone they answer with the name of the firm that owns that number.

  • Thanks. I am unclear still. If two name-based virtual hosts have the same IP, can they share the same port? Or can two name-based virtual hosts have the same IP and port? – Tim Feb 10 at 23:59
  • 1
    Yes and yes. The two name-based virtual hosts on the same IP and same port are disambiguated by the web server when it sees the Host: header in the HTTP request where the client says which host it is trying to reach (or by the SSL SNI if the connection was over HTTPS). Remember that every name-based virtual server without a port in its URL is really port 80 (or port 443 for HTTPS), so by default every name-based virtual server on a particular Web server is already sharing a port. See the Apache docs for virtual hosts at httpd.apache.org/docs/2.4/vhosts/name-based.html – Velo Traveler Feb 11 at 0:30

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.