So far when accessing the default website we have done so using the URL http://localhost and the default port for HTTP which is port 80. Bindings are the mechanism that associates access points with a website. You can view the bindings for a site by right-clicking on the site in the IIS Manager in the "Connections" column and select "Edit Bindings..." as shown in figure 1.
|Figure 1: Opening the bindings dialog|
Selecting this option opens the dialog shown in figure 2.
|Figure 2: The bindings dialog|
Before we explain the details and intricacies of how bindings work we'll go through a few basic configurations to soften the learning curve. We'll start with adding a new binding so that we can make requests to the default website on port 8080.
To add a new binding we click on the "Add..." button of the "Site Bindings" dialog shown on figure 2. This brings up the dialog in figure 3 where we fill in the parameters for the new binding. We only want to add a new port so we keep all the same settings as the existing binding but change the port from 80 to 8080.
|Figure 3: Adding a new binding to port 8080|
After we press "OK" the "Site Bindings" dialog looks like the one on figure 4.
|Figure 4: Binding to port 8080|
If we go back to the main screen (figure 5) we can see that in the "Browse" section in the right-hand column we now have two options: one is the previoulsy available option for port 80 and the other one is the one we just added to access the site on port 8080. Clicking on this will display the website as it did before but the port used will now be 8080.
|Figure 5: IIS Manager with the new binding displayed|
For this example we will need to use the hosts file to associate a domain name with IP address 127.0.0.1. Normally the domain name would be registered in the DNS system but that would create its own challenges and we just want to be able to experiment easily. If you don't know what is or how to update the hosts file you can have a look at this tutorial.
Add the following entry to the hosts file:File: hosts
Once this is added to the hosts file you can test it works properly by doing
ping website1 in a command prompt as shown on
|Figure 6: ping website1|
Before proceeding we need to give a word of advice about web browsers in general. They do a lot of performance optimizations and in particular cache a lot of the data including DNS data. In this example we are changing the DNS data and it may well be that the browser doesn't react to any changes you make. Also Internet Explorer seems to be a very bad choice to test with as it simply seems to ignore changes we make to the hosts file. For these reasons I recommend you use a tool like cUrl which will allow you to make HTTP requests directly and be in full control of what you send to your website without browser interference. You can also use Fiddler to be sure of what is being exchanged between the browser and the server. I have also had more success using Google Chrome for testing than Internet Explorer.
We can now use cUrl to get the contents of our website as shown on figure 7. This works without changing any of the bindings because website1 is just the same as 127.0.0.1 and the existing binding already allows us to access the website that way.
|Figure 7: curl website1|
Let's now change the bindings for the default website as shown on figure 8. Rather than leaving the host name blank for the binding for port 80 we set it to "website1". This means that if making a request over port 80 that request has to include a "Host: website1" header to be routed to the default website. Leaving it blank meant that the Host header could be anything even empty.
|Figure 8: Changing the binding on port 80 to require website1 as host name|
If we now do
curl localhost the error page is returned but it still works if we do
curl website1 as shown
on figure 9.
|Figure 9: curl localhost and website1|
Keep in mind that this works because cUrl inserts the Host header automatically as part of the requests it creates.
generated the request:
GET / HTTP/1.1 User-Agent: curl/7.33.0 Host: website1 Accept: */*
Note that if we use port 8080, both
curl localhost:8080 and
curl website1:8080 would work since for that port we
have left the host name blank and so it can be anything.
blog comments powered by Disqus