Saturday, May 14, 2011

Multiple Sites in a single Cloud Project: Using Ports

One of the most frequently asked questions I get is: “How can I manage to run multiple websites in a single Windows Azure deployment?” And the answer is that there are multiple ways to achieve this goal. So this blog post and the next upcoming three posts will show four ways to host multiple websites.

The easiest way is to create two WebRoles and to make them accessible via different endpoints using different ports. The result in the browser address bar would look like this:

http://myproject.cloudapp.net for the first WebRole
http://myproject.cloudapp.net:8080 for the second WebRole

It’s important to know that you can access a WebRole by more than one endpoints but you cannot access multiple WebRoles by the same endpoint!

The step by step walkthrough is simple: When creating a new Windows Azure Cloud Project, add two WebRoles to the new project.

CreateNewCloudProject

Your solution explorer should look like this after creating the cloud solution:

image

And in this case Visual Studio already did all the work for you. When looking at the WebRoles’ properties pages you can see in the “Endpoints” tab that WebRole1 has an Endpoint mapped to port 80 and WebRole2 has an Endpoint mapped to port 8080.

PortWebRole1 for WebRole1PortWebRole2 for WebRole2

Of course, you may change the Public Port settings to whatever port number serves you best.

You might want to have a look at the CloudProject’s ServiceDefinition-file. This file becomes important in the other ways solving the multiple sites issue.
Inside the definition file there is a <WebRole> block for each web role you created in the project. This block contains in the <Endpoints> child the information about how the role can be accessed. You can find your port settings there in the XML attribute port.

  1: <ServiceDefinition name="MultiSitesByPort" xmlns="...">
  2:   <WebRole name="WebRole1">
  3:    ...
  4:     <Endpoints>
  5:       <InputEndpoint name="Endpoint1" protocol="http" port="80" />
  6:     </Endpoints>
  7:     ...
  8:   </WebRole>
  9: 
 10:   <WebRole name="WebRole2">
 11:    ...
 12:     <Endpoints>
 13:       <InputEndpoint name="Endpoint1" protocol="http" port="8080" />
 14:     </Endpoints>
 15:    ...
 16:   </WebRole>
 17: </ServiceDefinition>

No comments:

Post a Comment