Cloud Zone is brought to you in partnership with:

Adam is an Evangelist for Windows Azure, working for Microsoft. By day, you can likely find him somewhere in the midwest, driving to yet another whiteboarding/deep-thinking session, ready to figure out how the cloud can save your family from certain doom, and make you rich and successful in the process. Before he started evangelizing, Adam was a Senior Developer Lead for Microsoft in Redmond, working on Office 365, BPOS, and Office Live. He misses Redmond, and the excitement of the mother ship, but the call of bitter cold and lots of snow in Chicago was too much for him, and he had to return. Lucky for you! When he's not evangelizing, he likes to spend time with his wife and kids, telling them how the cloud will benefit them as well. Adam is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

Wildcard Subdomains in Windows Azure

03.13.2012
| 4797 views |
  • submit to reddit
I was meeting with a small company today that has developed a SaaS solution around task and project management. It's a very cool application, and as is the fashion these days, uses subdomains to determine the end user's company as requests come to the browser. So, if I were to sign up for the application, my home url would be http://adamhoffman.getdonedone.com, whereas if you signed up (and had the unlikely name of Bill Ion), your home page would be http://billion.getdonedone.com. This is handled in ASP.NET application code on their Win2K8 servers hosted at Rackspace. The question quickly became "hey, we can do something similar in Azure, right?"

The answer is yes, and no, and yes. Let me explain.

My instant assumption was "yes", and ultimately, it is true that you can accomplish this behavior. It does, however, require that wherever you host your DNS that points to your application supports the concept of "wildcard DNS records", and more specifically, supports "wildcard CNAMEs". As it turns out, Go Daddy's DNS services don't support this, so we'll have to look elsewhere to get this functionality.

The next question you might be asking yourself now is why do we care about wildcard CNAME versus just wildcard DNS? Specifically, the answer lies in the way that Azure provides you high reliability. As a very quick DNS primer, you need to understand the difference between A records and CNAME records. Go read the link if you want the details, but the short answer is "A records point to IP addresses, CNAME records are aliases of other domain name records. OK, but why does this matter?

Well, as it turns out, when you want to put your fancy brand on your Azure based website, you can only do that by way of a CNAME record. The reason that you can't use an A record to point to your site is that your site doesn't have an IP address. Well, it does, but it's not something that you can get access to, and even if you could, it's not persistent, and will likely change. In order to avoid this problem, what you get is really a domain name that is a part of "cloudapp.net". For example, the website that I'm about to demo for you that supports wildcard subdomains is addressable by way of "wildcardsubdomain.cloudapp.net", but not by IP address. Well, OK, yes, smarty pants. It is addressable by IP as well. For example, at this moment I can also get to the same site at http://65.52.208.222, but I shouldn't rely on it. Who knows - by the time you read this, it might not work at all. If you want to know how I figured that out, check out nslookup.

The reason that's all true is that Azure relies on CNAME so that it can switch your IP around, but keep your site addressable. Why does it do it? It's mad network stuff related to load balancers, and the Azure Fabric controller, and it's done to ensure that your site has the maximum stability and uptime.

OK, back to the problem at hand - getting our dynamic subdomains passed through to our application code to allow for multitenant applications split by the third level domain. How will we handle it? Here we go.

  1. First, we'll find a DNS service that supports wildcard CNAMEs.
  2. Next we'll configure that DNS service to point to our application.
  3. Finally, we'll write the application code that understands the incoming requests so that we can fork our clients appropriately.
  4. After finally, we'll take a look at the URL Routing features of ASP.NET and see if we can extend our example to integrate nicely with it.

On the first count, it's not actually as straightforward as you might think. For example, I've used GoDaddy to procure several of my domains (yes, I am aware of the SOPA thing, and won't get political here). As the link here reveals, wildcard CNAMEs aren't an option in GoDaddy's DNS servers, so we'll need to look elsewhere. I won't attempt to put together an exhaustive list here, but one DNS service that does support this concept is Amazon's Route 53 DNS service. Another nice feature of this is that it has an API, which actually opens up another possibility if we so chose. Our solution here to route all subdomains to a single hosted web site. An alternative to our multi-tenant single application approach would be to actually specifically provision subdomains and point them to different hosted web sites. So, instead of

*.adamhoffman.net CNAME wildcardsubdomain.cloudapp.net (which has application code that sorts it out)

we could, instead go with

customer1.adamhoffman.net CNAME customer1.cloudapp.net (which is for the customer1 domain), and
customer2.adamhoffman.net CNAME customer2.cloudapp.net (which is for the customer2 domain), and
so on...

Yes, we could do that, and with the Route 53 API, we could even provision those new CNAME records as necessary. But that's not the route we're going to use. We'd like to use the former option, and have a single wildcard CNAME record that just routes everything to our super smart application.

OK, so next, we'll get ourselves an Amazon account to set up our DNS services on Route 53. Once you've signed up, create, in Amazon's terminology, a hosted zone for the domain that we'll support wildcards on. Of course, you need to own this domain.

Take a look now at the "delegation set" that Route 53 has assigned you. There are 4 DNS servers listed here, and these are the DNS servers that you need to tell your domain registrar about so that it makes sure that requests for your domain are handed off to these Route 53 DNS servers. In my case, my registrar for adamhoffman.net is Go Daddy, so I'll use the Go Daddy tools to point to these DNS servers.

With all of this done, there's only one final step. We need to tell the DNS servers that handle adamhoffman.net that the canonical name (CNAME) for *.adamhoffman.net is really wildcardsubdomain.cloudapp.net. Look at the CNAME record for *.adamhoffman.net in the following picture. That's the secret sauce.

Now, once you do this, all traffic for all subdomains of adamhoffman.net is routed to wildcardsubdomain.cloudapp.net. This is true not only of 3rd level subdomains, but also of 4th level (hello.world.adamhoffman.net) and all levels beyond it (this.is.really.cool.adamhoffman.net). Once we have this, it's really just a matter of inspecting the request to make multi-tenant decisions in our application code using Request.Url.Host which will contain the full domain name that was originally asked for in the request (not the cloudapp.net name), like this:

protected void Page_Load(object sender, EventArgs e)
{
	string[] domainparts = Request.Url.Host.Split(new char[] { '.' });
	if (domainparts.Length > 2)
	{
		// there must be a subdomain in front of the two last parts 
		// (i.e. xxx.adamhoffman.net or xxx.yyy.adamhoffman.net, etc.)
		System.Text.StringBuilder sbOut = new System.Text.StringBuilder("Subdomain Segments: <br/>");
		for (int i = 0; i < domainparts.Length - 2; i++)
		{
			sbOut.Append(domainparts[i] + "<br/>");
		}

		this.output.Text = sbOut.ToString();
	}
}

 

Now, some of you are saying, hey, can't I use the page routing features of ASP.NET or the MVC framework to add routes to this party as well? Again, yes and no. The out of the box routing functionality does not appear to support subdomain routing, but Maarten Balliauw has done some work to extend it and make this possible. While this is a potential nicety, it's not actually necessary to figure out what subdomain was the source of an original request, and therefore we won't cover that here.

To be sure, there are many DNS providers out there that support wildcard CNAMEs. Route 53 is one of them, and Go Daddy is not. If you've successfully done this with a different DNS provider, drop me a line and I'll add the details here.

Source code is here.

Interesting side note - it appears that you can actually use A records and rely on the IP of an Azure deployment as long as you don't delete and redeploy your, um, deployment... I still wouldn't recommend it, since you'll undoubtedly forget to update your DNS servers the one time you do actually delete and redeploy. Nonetheless, good reading on the subject is here.

Published at DZone with permission of Adam Hoffman, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)