Host Headers in IIS
You can host more than one website on a single web server using IIS. However, you must have a unique combination of host header name, IP address, and port number in order to do this. Let me try to explain to you what the host header is, how it works, and some common things most people don't understand about host headers.
HTTP message always includes the host header
The HTTP protocol is used by both the client and the web server to communicate. The HTTP message is nothing but this data exchanged between the server and the client. Each message has a header and a body. The HTML code of the webpage returned from the server to the client in response to a GET request may be present in the body section. The host header4 may contain many bits of information, including Referrer, Content-Length, Host, and much more.
Communication of the client with the web server
You have to learn how the server communicates with the client in order to understand where the HTTP message is checked. The user types the name of the domain and the port number in the browser. Let us suppose that you want to visit www.kambui.com . So you will have to enter the address www.kambui.com in the address bar of the browser. The browser will automatically use the HTTP protocol and the default port no. 80, which is the port for HTTP.
Next, the browser will resolve the domain name that you entered in the address bar of your browser. This is essential for establishing a connection to the IP address and the port number of that domain. The resolution will be done using either the hosts file or via the DNS server of the domain. Once the resolution is over, the client (your PC) will establish a connection with the host web server of Kambui.com and will send a request message to the server. This request will contain the host header, and will look something like,
GET /index.htm HTTP/1.1
Host: www.kambui.com
The server of kambui.com will receive the HTTP message and will check it. If it finds a host header (the host header may not be present at all), then the IIS will find out whether any host header has been configured in the IIS that is the same as the host header mentioned in the HTTP message. If a match is found, then the home page (index.htm or index.asp) will be displayed from the home folder of Kambui.com.
Once this is done, IIS will respond to the request.
IIS is not responsible for name resolution
You must have already noticed that the host header is not checked until the web server and the client establish a connection amongst themselves. So, when you are configuring IIS to make use of host headers, you must configure name resolution too. IIS cannot resolve names. Instead, you will have to use a DNS service for that, or make do with the hosts file, in case of a small network.
Behind the curtain
You will have to bind each website set up within IIS to an IP address, port number and host header name. The configuration information for each website will be stored in the metabase property ServerBindings, in a string of format IP:Port:Hostname.
An example of such a string would be:
192.168.0.10:80:www.kambui.com (You can omit the host header name and the IP).
IIS examines whether there is a website configured with the IP address and the port number on which the request came in, in order to determine which website would take care of the request. The website must also match the host header value used in the HTTP message.If there is a website with a ServerBindings property that exactly matches the request, then the request will be routed to that website.
Let us suppose there is no website that is configured to listen to the received request. In that case, IIS will examine whether there is a website configured to listen universally to all IP addresses (called All Unassigned in IIS manager), the port that was used to send the request, and with a host header name that matches with the name sent in the request. If the IIS finds a match, it will reroute the request to that website. The IIS will also check, lastly, whether there is a website with a blank host header, which can take care of the request.
Wildcards in host header
One question we frequently encounter is whether IIS supports wildcard host headers. And whether *.mydomain.com can be captured and redirected to one website.
The answer to this is both yes and no. It is yes, because the *.mydomain.com can be redirected to one web site. It is no, because, the DNS is the element at work here, not the IIS.
The steps for doing it are as follows:
1. Configure a website with no host header in IIS MMC, and assign an IP address to it. (If there is one IP address in the box, then you can skip this step). Now the website will be associated with the specific IP and will respond to all HTTP requests sent to the IP.
2. Now make sure that your name resolution is working properly and the reply is being sent with the correct IP address. If you use Microsoft DNS service, then you won't be able to create a A record (supposing that the domain zone has been already created in the DNS MMC). Then you will need to follow the steps given below:
1) Go to the folder %windir%\system32\dns\
2) Open the zone file (mydomain.com.dns, for example) with Notepad.
3) Add an entry in the file, such as
* A IP.IP.IP.IP
4) Save the file
5) Reload the data of the zone in DNS MMC
Remember that now all * will respond to the IP given earlier by you. For example: abc.mydomain.com, www.mydomain.com, K2k.mydomain.com, and so on.
Use the ping utility to make sure that your setup is working.
Type the command: Ping (anything).mydomain.com and you should see a reply coming from IP.IP.IP.IP
Now, in your browser, enter the address http://(anything).mydomain.com/
You will see the same web page that you set up.