I have a situation where I have a load balancer set up with Apache2 and mod-balancer that is going to load balance two Apache2 web servers. I'd like my load balancer to be able to serve multiple sites but currently every virtual host I create seems to serve the same site.
On the load balancer (lb1) I have the default
virtual host enabled so that when you browse to the load balancers IP address you see the standard Apache2 message.
The following is the virtual host layout that I use for all subsequent domains that the load balancer can serve (substitute mydomain.com
for the actual domain name of course):
<VirtualHost *>
ServerName www.mydomain.com
ServerAlias mydomain.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Proxy balancer://mydomain.com.cluster>
BalancerMember http://web1.mycluster.com:80 route=web1 retry=5
BalancerMember http://web2.mycluster.com:80 route=web2 retry=5
ProxySet lbmethod=byrequests
</Proxy>
ProxyPass / balancer://mydomain.com.cluster/ stickysession=BALANCEID n$
ProxyPassReverse / http://web1.mycluster.com/
ProxyPassReverse / http://web2.mycluster.com/
</VirtualHost>
This is what a virtual host on the web server (web1
& web2
) end looks like. Both web servers have the same virtual hosts enabled. The only difference between the virtual hosts is the reference to either web1
or web2
respectively (I show the virtual host of web1
here):
<VirtualHost *>
ServerName www.mydomain.com
ServerAlias mydomain.com
RewriteEngine On
RewriteRule .* - [CO=BALANCEID:balancer.web1:.mydomain.com]
DocumentRoot /mnt/share/mydomain.com/www/public_html
<Directory />
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This seems to work for mydomain.com
. If I create another virtual host with the same layout e.g. for myotherdomain.com
then browsing to myotherdomain.com
will actually serve mydomain.com
.
What I'm I doing wrong?
You probably need a NameVirtualHost directive somewhere, otherwise requests are matched by the server IP address only and the first VirtualHost that matches this IP will 'win' (so the first always wins).
At least, it worked that way years ago, when I had more to do with Apache.
Jaccek Konieczny was right and I finally found out what the issue was. I'd like to elaborate on it a bit. It appears that it had to do with how I configured my virtual hosts and it was always picking the default one on either one of the load balanced servers. This is how I end up setting my virtual hosts:
For the default vhost:
Then for the other virtual hosts:
All the web server nodes are configured as they would have been standalone machines except for the BALANCEID cookie being set.