Web Development: Introduction To Localhost and IIS

What is your “localhost”?

In computer networking, localhost (meaning this computer) is the standard hostname given to the address of the loopback network interface. This mechanism is useful for developers to test their software.

The default address for localhost is 127.0.0.1.  This special IP address, also called the Loopback Address,  is always defined to mean “the current machine”. It is not the same thing as the external IP address of your web server:

Localhost is configurable in the Hosts file (typically located at C:WindowsSystem32DriversEtcHosts). Due to multiple dependencies, it is usually not recommended to reconfigure it.

Instead, you can set up alternate domains in your Hosts file. For example, you could set

192.168.1.250   my.internal.website.com
192.168.1.249   your.internal.website.net

How Does “localhost” Relate To IIS ?

Internet Information Services (IIS) – is a web server software application created by Microsoft for use with Microsoft Windows.

If IIS is expecting a connection to IP address 192.168.1.250, then a connection to 127.0.0.1 will not match.  When IIS responds to an HTTP request, it uses 3 pieces of information to figure out what web site it should use to build the response:

  1. IP address – The browser uses the name in the address bar to perform a DNS query and get the actual IP address of the host. It sends the http request to that IP address.
  2. Port – By default this is 80 for non SSL requests and 443 for SSL.
  3. Host header – Part of an http request is a host header. This host header matches the domain name of the address requested by the user, including sub-domain or host names.
When running a browser on your machine you type: http://localhost (no port number), what does IIS do?  Per previous replies the browser converts localhost to 127.0.0.1 as the IP address and constructs an HTTP request for that IP address. The host header will be localhost.  In this scenario IIS will see that both Website1 and Website2 do not have matching ip addresses and so the default site will be used to process the request.
If you wish to have Website1 or Website2  respond to the request, disable the default site and change the IP address setting to for either Website1 or Website2.