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( and as an example. If you are going to do a nslookup, you will realise that they both point to the same host, ( because blogspot supports virtual hosting.
$ nslookup

Non-authoritative answer:        canonical name =

$ nslookup
Address:        canonical name =

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 80
Connected to (
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.1 302 Found
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=/;
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">
<H1>302 Moved</H1>
The document has moved
<A HREF="">here</A>.
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 to get information from
$ telnet 80 | grep "<title>"
GET / HTTP/1.1

<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
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv: 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



Post a Comment

<< Home