Understanding Virtual Domains

Virtual domains can seem confusing at first, but actual it's pretty simple. Here's the basic idea: you have ONE web server, at ONE IP address, but you host multiple websites, each with different content.

How can that work? Well, part of the secret is Apache itself, but Apache gets a big help from the way that clients (web browsers) use the http protocol to request web pages. We can do some testing with telnet to show examples: (you type the stuff in bold)



bash-2.05a$ telnet pcunix.com 80
Trying 64.226.42.29...
Connected to pcunix.com.
Escape character is '^]'.
GET http://pcunix.com/index.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>301 Moved Permanently</TITLE>
</HEAD><BODY>
<H1>Moved Permanently</H1>
The document has moved <A HREF="http://aplawrence.com/index.html">here</A>.<P>
<HR>
<ADDRESS>Apache/1.3.22 Server at pcunix.com Port 80</ADDRESS>
</BODY></HTML>
Connection closed by foreign host.
 

This is what your web browser really sees if it connects to http://pcunix.com. What you see is something different, because your browser pays attention to the "301 Moved Permanently" and gives you "aplawrence.com" instead (for why I do that, see Solving Google Page Rank problems with a 301 Redirect).

Now lets ask for a different virtual domain. Notice that we are still connecting to pcunix.com:

bash-2.05a$ telnet pcunix.com 80
Trying 64.226.42.29...
Connected to pcunix.com.
Escape character is '^]'.
GET http://aplawrence.com/index.html
<html>
<head>

<STYLE TYPE="text/css">
<!--
BODY { background-color: white; }
H2 { color:#000066; font-family: arial, helvetica, sans-serif; }
H3 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4.narrow { color:#000066; font-family: arial, helvetica, sans-serif; margin-left:2em}
P, OL, UL, DL, BLOCKQUOTE { color: #000000; font-family: arial, helvetica, sans-serif;  }

... (lines deleted) ...

<br clear=left>

</BODY>
</HTML>
Connection closed by foreign host.
 

That's different: Apache gave us a different page. Now again:

bash-2.05a$ telnet pcunix.com 80
Trying 64.226.42.29...
Connected to pcunix.com.
Escape character is '^]'.
GET http://www.zenex.com/index.html
<html>
<head>

<STYLE TYPE="text/css">
<!--
BODY { background-color: white; }
H2 { color:#000066; font-family: arial, helvetica, sans-serif; }
H3 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4.narrow { color:#000066; font-family: arial, helvetica, sans-serif; margin-left:2em}
P, OL, UL, DL, BLOCKQUOTE { color: #000000; font-family: arial, helvetica, sans-serif;  }

... (lines deleted) ...

<br clear=left>

</BODY>
</HTML>
Connection closed by foreign host.
 

This time I deliberately made a mistake and asked for a domain that doesn't exist (as far as I know, anyway - and it certainly doesn't exist on my site!). Apache gave me the aplawrence.com page. We'll see why later, but let's ask for the right domain, zenez.com (at the time of this writing Zenez.com was hosted here as a virtual domain but might not be when you read this):

bash-2.05a$ telnet pcunix.com 80
Trying 64.226.42.29...
Connected to pcunix.com.
Escape character is '^]'.
GET http://www.zenez.com/index.html
<HTML>
<HEAD>
<TITLE>ZENEZ Main Home Page</TITLE>
</HEAD>
<BODY bgcolor="#000000" text="#ffffff" link="#FFFF00" vlink="#FF0000">
<A HREF="http://www.mysql.com">MySQL is a trademark of MySQL AB, Inc. in the United States and other countries.</A><BR>
</H6>

... lines deleted  ...

</center>
</BODY>
</HTML>

What happpens if we ask for a domain that does exist?

bash-2.05a$ telnet pcunix.com 80
Trying 64.226.42.29...
Connected to pcunix.com.
Escape character is '^]'.
GET http://microsoft.com/index.html
<html>
<head>

<STYLE TYPE="text/css">
<!--
BODY { background-color: white; }
H2 { color:#000066; font-family: arial, helvetica, sans-serif; }
H3 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4.narrow { color:#000066; font-family: arial, helvetica, sans-serif; margin-left:2em}
P, OL, UL, DL, BLOCKQUOTE { color: #000000; font-family: arial, helvetica, sans-serif;  }

... (lines deleted) ...

<br clear=left>

</BODY>
</HTML>
 

It's giving you pages from my site, not Microsoft.

Of course your browser wouldn't connect to one site and then request a page from a different site: I'm just doing that to demonstrate what goes on behind the scenes.

Apache

Well, we see know that the client software has to ask for the virtual domain it wants. How does Apache know what to do? That's fairly simple, and only involves changing some configuration files. On my site, that starts with httpd.conf in the /www/conf directory, which has

Include conf/vhosts.conf
 

in it. The vhosts.conf looks something like this: (I've eliminated uninteresting lines)

<VirtualHost zenez.com>
DocumentRoot /usr/aplawfiles/www/virthosts/zenez.com/htdocs
ServerName zenez.com
ServerAlias www.zenez.com
<Directory "/usr/aplawfiles/www/virthosts/zenez.com/htdocs">
  Options All
  AllowOverride All
  Order allow,deny
  Allow from all
</Directory>
</VirtualHost>

<VirtualHost pcunix.com>
DocumentRoot /usr/aplawfiles/www/virthosts/pcunix.com/htdocs
ServerName pcunix.com
ServerAlias www.pcunix.com
<Directory "/usr/aplawfiles/www/virthosts/pcunix.com/htdocs">
  Options All
  AllowOverride All
  Order allow,deny
  Allow from all
</Directory>
</VirtualHost>

<VirtualHost aplawrence.com>
DocumentRoot /usr/aplawfiles/www/virthosts/vps.pcunix.com/htdocs
ServerName aplawrence.com
ServerAlias www.aplawrence.com
<Directory "/usr/aplawfiles/www/virthosts/vps.pcunix.com/htdocs">
  Options All
  AllowOverride All
  Order allow,deny
  Allow from all
</Directory>
</VirtualHost>

Notice that the only real difference for each virtual domain is the DocumentRoot: where the actual pages are.

So that's how Apache knows where to go when it sees "GET http://aplawrence.com" vs. "GET http://www.zenez.com". It just goes the the DocumentRoot that its configuration tells it to, and gives you the pages you want.



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Understanding Virtual Domains




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





I think there is a world market for maybe five computers. (Thomas Watson, Chairman of IBM, 1943)

FORTRAN's tragic fate has been its wide acceptance, mentally chaining thousands and thousands of programmers to our past mistakes. (Edsger W. Dijkstra)








This post tagged: