Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set up a load balancer for multiple virtual hosts (apache)

I am trying to set up a load balancer for a couple of virtual hosts on my apache server. These virtual hosts are added by adding the following lines for the file "C:\Windows\System32\drivers\etc\hosts":

127.0.0.1       localhost
127.0.0.1       vhosta
127.0.0.1       vhostb
127.0.0.1       vhostc
127.0.0.1       load-balancer
::1             localhost

Then I've added the following lines for the file "C:\xampp\apache\conf\extra\httpd-vhosts.conf":

<VirtualHost *:80>
  DocumentRoot c:/xampp/htdocs
  ServerName localhost
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot c:/vhosts/vhosta
  ServerName vhosta
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot c:/vhosts/vhostb
  ServerName vhostb
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot c:/vhosts/vhostc
  ServerName vhostc
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot c:/vhosts/load-balancer
  ServerName load-balancer
</VirtualHost>

And of course I've created the folders in C:/vhosts/ and added an index.php file to each one (with an echo statement inside).

Now, I can access the virtual hosts through my browser by visiting "http://vhosta" etc.

But what I need, is to make a load balancer that chooses to execute either "http://vhosta", "http://vhostb" or "http://vhostc".

How can I achieve this? And have I done everything correct so far?

Any help will be greatly appreciated! Thanks in advance!

(i am using xampp on windows 8.1 btw.)

like image 730
Langkiller Avatar asked Feb 10 '23 20:02

Langkiller


1 Answers

There many ways to accomplish this, but what you are trying to do it won't work. The /etc/hosts is a basic way of IP to host name mapping. If you want round-robin resolution you will have to use DNS server. Also, it doesn't make much sense to load balance on the same machine, except for learning and configuration testing.

These are some of the options you have.

1) Using mod_proxy_balancer. You need to enable mod_proxy and mod_proxy_balancer modules. Also, you need to pick one of the scheduler algorithms. Options are: mod_lbmethod_byrequests, mod_lbmethod_bytraffic, mod_lbmethod_bybusyness and mod_lbmethod_heartbeat.

http://httpd.apache.org/docs/2.4/mod/mod_proxy_balancer.html

<VirtualHost *:80>
  ...

  ServerName load-balancer

  <Proxy balancer://mybalancers>
    BalancerMember http://vhosta:80
    BalancerMember http://vhostb:80
    BalancerMember http://vhostc:80
  </Proxy>
  ProxyPass / balancer://mybalancers
  ProxyPassReverse / balancer://mybalancers

  ...
</VirtualHost>

2) Using DNS round-robin option. You need to point multiple IPs to the same host name. With this option, when you make a request to your load-balancer host, DNS server will give you next IP (in a round-robin fashion).

DNS configuration

load-balancer        IN            A       10.0.0.1
load-balancer        IN            A       10.0.0.2
load-balancer        IN            A       10.0.0.3

Virtual hosts for apache servers

<VirtualHost 10.0.0.1:80>
  DocumentRoot c:/vhosts/vhosta
  ServerName load-balancer
</VirtualHost>

<VirtualHost 10.0.0.2:80>
  DocumentRoot c:/vhosts/vhostb
  ServerName load-balancer
</VirtualHost>

<VirtualHost 10.0.0.3:80>
  DocumentRoot c:/vhosts/vhostc
  ServerName load-balancer
</VirtualHost>

And one more thing related to hosts file. If you want to map a loopback IP to hostname, feel free use full range, from 127.0.0.0 to 127.255.255.255. I'm pretty sure this is mapped in Windows, but I have no ways to test it. To test, just ping 127.1.2.3, and see what you get back.

http://en.wikipedia.org/wiki/Loopback

This is how you can organize your /etc/hosts file if you need multiple IPs for testing.

127.0.0.1       localhost
127.0.0.2       vhosta
127.0.0.3       vhostb
127.0.0.4       vhostc
127.0.0.5       load-balancer
like image 83
Boris Avatar answered Apr 25 '23 06:04

Boris