Enterprise Architecture & Integration, SOA, ESB, Web Services & Cloud Integration

Enterprise Architecture & Integration, SOA, ESB, Web Services & Cloud Integration

Wednesday 20 June 2012

Configure apache (2.4.2) load balancer with virtual hosts

My boss asked me couple of days ago to configure apache load balancer. It has been some time since I have done one. I could still remember most of the things, so started making changes to httpd configuration files (thanks to so many site/blogs including apache official site offering help on load balancer configuration). The journey is so far nice. Here is my initial load balancer configuration (all important lines, if not all, are shown here) -

My server needs to be configured with two virtual hosts listening on 8081 and 9081 respectively.

Listen 8081
Listen 9081

ProxyRequests off
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so

#LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule rewrite_module modules/mod_rewrite.so

LoadModule headers_module             modules/mod_headers.so
LoadModule status_module             modules/mod_status.so

LoadModule lbmethod_byrequests_module     modules/mod_lbmethod_byrequests.so
 

<Proxy balancer://mycluster8081>
  BalancerMember http://myserver1:6001
  BalancerMember http://myserver1:7001
  BalancerMember http://myserver2:6001
  BalancerMember http://myserver2:7001

 </Proxy>

<Proxy balancer://mycluster9081>
  BalancerMember http://myserver3:6001
  BalancerMember http://myserver3:7001
  BalancerMember http://myserver4:6001
  BalancerMember http://myserver4:7001
</Proxy>

<Location /manager>
    SetHandler balancer-manager
</Location>

<VirtualHost *:8081>
    ProxyPass /myportal balancer://mycluster8081/myportal
    ProxyPassReverse /myportal balancer://mycluster8081/myportal
</VirtualHost>

<VirtualHost *:9081>
    ProxyPass /myportal balancer://mycluster9081/myportal
    ProxyPassReverse /myportal balancer://mycluster9081/myportal
</VirtualHost>


I restarted the server and I encountered my first issue - the server refused to start after spitting an error "AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??". After spending time on investigation, I have added the following module (which has been added in 2.4.x, it didn't exist in 2.2.x) -

LoadModule slotmem_shm_module modules/mod_slotmem_shm.so

Now, I got the second problem, bigger than the first one - "(22)Invalid argument: AH01186: worker slotmem_grab failed". After googling, I have found that this is an existing issue with 2.4.x. The technical details can be obtained from here https://issues.apache.org/bugzilla/show_bug.cgi?id=52402

What was working in 2.2.x is not working now in 2.4.x, but Apache keeps adding more features and improving its existing features too. So, this happens. However, as I am running against time, I can't wait till someone fixes this issue, so tried searching for a workaround. After several attempts by changing the configuration, there seems to be a workaround available - move all proxy balancer stuffs into respective VirtualHost configuration. Please see below the modified configuration -

Listen 8081
Listen 9081

ProxyRequests off
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so

#LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule rewrite_module modules/mod_rewrite.so

LoadModule headers_module             modules/mod_headers.so
LoadModule status_module             modules/mod_status.so

LoadModule lbmethod_byrequests_module     modules/mod_lbmethod_byrequests.so LoadModule slotmem_shm_module modules/mod_slotmem_shm.so


<Location /manager>
    SetHandler balancer-manager
</Location>

<VirtualHost *:8081>
<Proxy balancer://mycluster8081>
   BalancerMember http://myserver1:6001
  BalancerMember http://myserver1:7001
  BalancerMember http://myserver2:6001
  BalancerMember http://myserver2:7001
</Proxy>

    ProxyPass /myportal balancer://mycluster8081/myportal
    ProxyPassReverse /myportal balancer://mycluster8081/myportal
</VirtualHost>

<VirtualHost *:9081>
<Proxy balancer://mycluster9081>
BalancerMember http://myserver3:6001
  BalancerMember http://myserver3:7001
  BalancerMember http://myserver4:6001
  BalancerMember http://myserver4:7001
</Proxy>

    ProxyPass /myportal balancer://mycluster9081/myportal
    ProxyPassReverse /myportal balancer://mycluster9081/myportal
</VirtualHost>

At least this workaround will keep you move forward till you get the official fix. (At least, a new patch is not an option in my case). Hope this workaround works for you. If not, please respond to me.