Re: Testing virtual hosts on a virtual machine

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Florent Georges wrote:
  Hi,

  I am using a virtual box (Ubuntu server 9.10 with VMware Fusion)
to test a web server.  No problem to install Apache on this
Ubuntu box, of course.  And I can access the default page after
an install by using http://xxx.xxx.xx.xx/ in my browser (on the
host machine.)

  But the web server will use named virtual hosts.  I guess that
won't work as the browser won't send the correct domain name.

  Is there any tool to test an Apache instance with virtual hosts
on a virtual machine?  Any best practice or advice to follow in
that configuration?


Because the information available when searching for this on Google or on Wikipedia, and even on the Apache website is rather confusing, let me try to give you an explanation of how this stuff really works.

It is a bit long, but I believe that if you understand the explanation below, you will never have trouble again with Virtual Hosts.
And also a lot less trouble with browsers and http in general.
Still with me ?


When you ask a browser to access "http://myhost.company.com";, what really happens ?

1) the browser asks the local operating system to "translate" the name "myhost.company.com" into an IP address.
The operating system, to do that, works in 2 steps :

1.1) the OS looks into the local "hosts" file, to see if it finds a line like
aaa.bbbb.ccc.ddd  myhost.company.com
If it does, then it returns the IP address "aaa.bbbb.ccc.ddd" to the browser, as a translation for the name "myhost.company.com", and it is finished.

1.2) if the above did not work, then the OS will ask, over the network, to a DNS server to do the same translation. The DNS server will look up its own tables, and in the end it can either answer with an IP address, or with "not found". The OS will then pass back this same answer to the browser, and it is finished. (Of course, this supposes that the OS has a working DNS server to talk to, otherwise it will return an error to the browser right away).

2) Now the browser has an answer, which is either a "host not found", or an IP address.

2.1) If it was a "host not found", the browser tells the user and that's it.

2.2) If the browser has received an IP address however, then the process continues.

3) the browser establishes a TCP/IP connection with the received IP address, on port 80.
That either works, or doesn't.

3.1) If it doesn't work, the browser sends an error message to the user : cannot connect to "myhost.company.com", and again that's it. (It may fail to work, for example because there is no host at that IP address, or because there is a host, but there is no program there listening to connections on port 80; or for many other reasons).

3.2) If it works, the process continues.
(That it works, implies that on the target server there is a process which actually listens to connection requests on port 80, and accepts them. That will generally be a webserver like Apache).

4) the browser, over this now established TCP connection to the server at that IP address:port 80, sends a HTTP request to that server. This HTTP request consists of minimum 2 lines of text, as follows :
GET / HTTP/1.1
Host: myhost.company.com

The first line indicates, in the middle, what resource the browser wants. In this case, it just wants the default home page, so the resource URL is "/". The second line indicates which "virtual host" the browser wants to talk to, on the target server (with whom it already has a TCP connection). After sending that request, the browser starts waiting for a server answer over that same TCP connection.

5) the receiving Apache HTTP server receives the above request, and looks at the "Host:" line. Now the receiving Apache HTTP server is going to try to "match" this hostname, with the name of one of the VirtualHost's that are defined in its configuration. It either finds a match, or it does not.

5.1) if it does find a match, then it will process this request using the "personality" of the VirtualHost that is defined for that name.

5.2) if it does not find a match, then it will process this request using the personality of the first defined VirtualHost, from top to bottom of the configuration file. (It does not matter in that case if the hostname matches or not, it will use this first VirtualHost as the "default host".)


To give you a practical example :

Suppose you have a server with an IP address of 192.168.100.100.
On that server, you install Apache.
In Apache, you define a new (additional) <VirtualHost>, and you give it the configuration lines

ServerName www.google.com            # (really, for the example)
DocumentRoot /var/www/some-new-dir
DirectoryIndex index.html

In that new directory /var/www/some-new-dir, place a html page named "index.html", containing a "Hello, I'm Google !" message.

And you restart Apache.

Then, on your local workstation, edit the "hosts" file (under Windows, this is at c:/windows/system32/drivers/etc/hosts; under Unix/Linux, it is at /etc/hosts).
Add the following line to it :

192.168.100.100 www.google.com  # (really, for the example)
(change the IP address to the real IP of the host running Apache)

Then on your local workstation, call up the browser, and enter the URL "http://www.google.com";.

What happens, is what I described above, in the following sequence :

1), 1.1), 2), 2.2), 3), 3.2), 4), 5), 5.1)

Now go back to edit the local hosts file, and comment out the line that you added before.
Close the browser, re-open it, and ask again for "http://www.google.com";.
Obviously, you do not get the same page.  Why ?

Because this time, what happened is
1), 1.1), 1.2), 2), 2.2), 3), 3.2), 4), 5), 5.1)
small difference, big effect.

In all the above, there are 2 essential elements :
- the local browser must know which IP address corresponds to the name of the virtual host
- the Apache server must know that it has a virtual server with that name

It does not matter whether the Apache host is a physical machine or a virtual machine. As long as your IP networking setup, and the hostname resolving mechanism (known as "the resolver") are working, it will work.
There are no "tricks" involved. It is pure logic at every step.



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxx
  "   from the digest: users-digest-unsubscribe@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx


[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux