How to serve multiple virtual hosts externally

Hello

I have configured Apache 2.2 to have two different virtual hosts.

The simplified httpd-vhosts.conf file is this:

Code:
<VirtualHost *:80>
     ServerName forumsite
     DocumentRoot "/Library/WebServer/forumsite"
</VirtualHost>

<VirtualHost *:80>
     ServerName shoppingsite
     DocumentRoot "/Library/WebServer/shoppingsite"
</VirtualHost>

And I 've included these lines in hosts file:

Code:
127.0.0.1 forumsite
127.0.0.1 shoppingsite

With this configuration, things work perfectly on the same computer where Apache is installed i.e. under localhost:

when I browse to http://forumsite it displays the forumsite
when I browse to http://shoppingsite it displays the shoppingsite


So far so good. But when I try to access from another computer:

when I browse to http://address_of_apache_server

ıt serves solely the forumsıte and I have no way to access shoppıngsıte externally

How to externally access the second vırtual host
 
I think it should be
/etc/hosts
Code:
127.0.0.1 localhost, forumsite, shoppingsite

But I have 0 experience in this field [yet], so this is my guess
 
killasmurf86 said:
I think it should be
/etc/hosts
Code:
127.0.0.1 localhost, forumsite, shoppingsite

But I have 0 experience in this field [yet], so this is my guess
Good idea, but 127.0.0.1 localhost was already included at the start of the file and it'ssomething like

127.0.0.1 localhost
....
127.0.0.1 forumsite
127.0.0.1 shoppingsite

which is basically and expanded version of

127.0.0.1 localhost, forumsite, shoppingsite

They're basically equivalent and makes no difference from this respect. Actually it's the httpd-vhosts.conf file where the order of hosts makes a difference and I comment on it below at Wiedmann's reply.
 
Wiedmann said:
A request to an IP always goes to the first (default) namebased vhost.

You must add such entries also on the other PC (of course with the IP's from the Apache box), and using the hostnames for the requests and not an IP.

You're 100% correct on the first sentence as I've tried changing the order of vhosts at the httpd-vhosts.conf file and the Apache served the host which came the first at httpd-vhosts.conf, that's right.

But however, on your second sentence it doesn't look like the best of ideas, I have to say. Because in that case, it seems that I'll have to edit hosts file of each client computer, which is not so desirable.

Well actually I 've started to run my own (local) DNS server, that is BIND (I coulnd't find enough time to talk about that in my first post) and I thought DNS server would somehow assign the
correct virtual (namebased) host but I suppose it won't be that easy. all a DNS server does is just direct the name request to an IP and in this case Apache has no way to know which host to serve.

So, there should be a way of serving the correct virtual host using a combination of DNS server and/or apache configuration files, I guess.
 
But however, on your second sentence it doesn't look like the best of ideas, I have to say. Because in that case, it seems that I'll have to edit hosts file of each client computer, which is not so desirable.
Well, that's the only way. Each client must use the hostnames and thus must be able to resolve the hostnames to the ip of the server.

Of course, if you are using local hosts files, or a DNS server (which is knowing the ip for the hostnames (ServerNames)) is your choice. Both is doing the same.
 
aurora72 said:
Well actually I 've started to run my own (local) DNS server, that is BIND (I coulnd't find enough time to talk about that in my first post) and I thought DNS server would somehow assign the
correct virtual (namebased) host but I suppose it won't be that easy. all a DNS server does is just direct the name request to an IP and in this case Apache has no way to know which host to serve.

So, there should be a way of serving the correct virtual host using a combination of DNS server and/or apache configuration files, I guess.

HTTP 1.1 uses virtual hosts by looking at the host header. I'll try to explain it:

If you request "http://myhost" in your browser the client will try to resolve "myhost" in DNS. This returns an IP address, lets say 1.1.1.1. The browser then connects to 1.1.1.1 port 80 and sends this request:
Code:
GET / HTTP/1.1
Host: myhost

Now, if "otherhost" is also on that server a DNS request will, again, resolve to 1.1.1.1. Browser connects to 1.1.1.1 port 80 and sends this request:
Code:
GET / HTTP/1.1
Host: otherhost

Because both websites are running on the same IP address the only way for the webserver to know which website to serve is to look at the host: header. That host: header is send by the browser aka client.
 
SirDice said:
....
Because both websites are running on the same IP address the only way for the webserver to know which website to serve is to look at the host: header. That host: header is send by the browser aka client.

Looks like a good idea, i.e. checking with the header part of the request. I might try implenting a solution using it.
 
aurora72 said:
Looks like a good idea, i.e. checking with the header part of the request. I might try implenting a solution using it.

Not really needed. Since both apache and most modern browsers support HTTP 1.1 this is already implemented.
 
Back
Top