Friday, October 17, 2008

How Virtual Hosting Works

Probably most of the people know about virtual host support by web server, but not many know how it works from the web server view point. Why not I use my two blog addresses(http://chihungchan.blogspot.com/ and http://model4maths.blogspot.com/) as an example. If you are going to do a nslookup, you will realise that they both point to the same host, blogspot.l.google.com (209.85.175.191) because blogspot supports virtual hosting.
$ nslookup chihungchan.blogspot.com
Server:         203.166.128.168
Address:        203.166.128.168#53

Non-authoritative answer:
chihungchan.blogspot.com        canonical name = blogspot.l.google.com.
Name:   blogspot.l.google.com
Address: 209.85.175.191


$ nslookup model4maths.blogspot.com
Server:         203.166.128.168
Address:        203.166.128.168#53

model4maths.blogspot.com        canonical name = blogspot.l.google.com.
Name:   blogspot.l.google.com
Address: 209.85.175.191

If you were to telnet to the host at port 80 and try to get the home page with barebone HTTP protocol, you will not get what you want.

$ telnet chihungchan.blogspot.com 80
Trying 209.85.175.191...
Connected to chihungchan.blogspot.com (209.85.175.191).
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.1 302 Found
Location: http://www.google.com.sg/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=eb19920a5651e1f6:TM=1224256716:LM=1224256716:S=TUKFXgZR8MBbM
KAf; expires=Sun, 17-Oct-2010 15:18:36 GMT; path=/; domain=.google.com
Date: Fri, 17 Oct 2008 15:18:36 GMT
Server: gws
Content-Length: 222

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.sg/">here</A>.
</BODY></HTML>
According to the specification of HTTP/1.1, you will need to specify the http header for Host: to identify which host (or virtual host) you wish to get information from. Below shows how I telnet to chihungchan.blogspot.com to get information from model4maths.blogspot.com
$ telnet chihungchan.blogspot.com 80 | grep "<title>"
GET / HTTP/1.1
Host: model4maths.blogspot.com

<title>Model Approach for Primary School Maths</title>
Connection closed by foreign host.

All the modern browsers and command line utilities will automatically add in the "Host:" http header when you request information. You may want to download LiveHTTP Headers addon to your firefox browser to see what is under the hood. Below are request and the response to this blog

GET / HTTP/1.1
Host: chihungchan.blogspot.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: .......

HTTP/1.x 200 OK
Content-Type: text/html; charset=UTF-8
Last-Modified: Fri, 17 Oct 2008 15:01:20 GMT
Cache-Control: max-age=0 private
Etag: "f5b850fe-6429-414d-a845-2ff9cb6c82f7"
Content-Encoding: gzip
Transfer-Encoding: chunked
Date: Fri, 17 Oct 2008 15:40:04 GMT
Server: GFE/1.3

Labels:

0 Comments:

Post a Comment

<< Home