How is a subdomain passed to the webserver?

Solution 1:

Domains, Subdomains, Sub-Sub domains, and Sub(x)-sub-subdomains (made the last two up!), are treated in exactly the same way by DNS to an end user and a webserver.

For example, the DNS server can return different results for domain.com, www.domain.com or mysite.domain.com.

Now, as you are probably aware, there are many more domains than IPs in the world.

Because of this, servers use something known as "Host Headers" in order to serve the correct content.

It does not matter what you actually request, your machine treats everything the same - it will looks up on DNS what IP it should connect to, and once connected, it will send the host header as part of the request. The webserver then sends back the content it was configured to send back.

A good tool for testing/learning/diagnosis is Wfetch, it can be downloaded here.

Here is an example of the headers sent whilst accessing superuser.com

alt text

Here is the configuation for host headers in IIS (a webserver):

alt text

(Image from here)

If you were interested, Serverfault.com and Superuser.com are on the same IP and this is the technique that is used in order to separate the sites and serve different content.

For more reading on the subject, you may want to read the Wikipedia article on Virtual Hosting.

(I know you understand the basics, but I thought it would be good to say a little more so that anyone Google-ing this may learn a little if they want to!)

Edit

So, As per your example, I would guess that someone as big as blogspot most likely has a wildcard DNS entry (A wildcard entry returns the same result for any query) that points to a load balancer, the load balancer then will send it out to many different webservers who probably do not have a separate site per host header, but instead a single site/script that the address typed (host headers or not) gets parsed on, and pulls the correct content from their CDN there and then.

Although, it would also be possible (but unlikely) that they simply have a few thousand entries per ip, the ip leads to a few servers and they simply have an entry for each site that has a matching host header.

Solution 2:

Indeed, if two domains have the same IP address, the server relies on the browser to specify the requested domain. Like:

GET / HTTP/1.1
Host: data.stackexchange.com

Solution 3:

Subdomains isn't the right word. Usually a subdomain is domain on its own so sub.example.com is passed to the DNS first, if no specific record is found for this subdomain, the request is passed to the IP of the top domain. (example.com) On that server the request is made again and the server will act accordingly.

In other words, a request to example.com doesn't really differ from a request to sub.example.com.

Solution 4:

Subdomain.example.com doesn't resolve to 11.22.33.44 - for the purpose of your question it typically has it's own DNS A record and resolves to a different address.

It is possible to allow the subdomain resolve to the same address as the parent domain and still server up different pages, but if you do that you must host both sites on the same web server and need account for it on your server's configuration. The way you account for this depends on what kind of web server you're running.