Article updated on

Proxy Balancer Session for Apache in Ubuntu, Example

This example shows how to make a simple balancing for multiple instances using Apache balancer. Three tomcats in session configuration will be used. The Apache Balancer will always send all requests to the same server as long as it has the same session cookie. If one of the tomcats is shut down it will redirect to another cluster member server without sending any error messages to the client.

The context name will be TestWeb. You can download the war file used as example here.

When one of the cluster member servers are shutdown the tomcat session is lost. If you need session for authentication you may need to log in again. To make tomcat share sessions among different instances check this reference. If you are using clustering perhaps you may consider using another load balancing scheduler algorithm. Check the Apache reference  http://httpd.apache.org/docs/2.4/mod/mod_proxy_balancer.html and for mod_proxy configuration see http://httpd.apache.org/docs/2.4/mod/mod_proxy.html

Install apache

sudo apt-get install apache2

 

Install apache modules

sudo apt-get install libapache2-mod-proxy-html
sudo apt-get install libxml2-dev

 

Enable apache modules

sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod headers

 

Install Balance Strategy

From console:

 sudo nano /etc/apache2/sites-available/000-default.conf

Paste this and configure accordingly.

<VirtualHost *:80>
        ProxyRequests off
        ServerName MyServer
        Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
        CustomLog /var/log/apache2/mybalancer combined
        <Proxy "balancer://mycluster">
                 BalancerMember http://10.0.135.25:8080/TestWeb/ route=1 timeout=6 retry=20
                 BalancerMember http://10.0.135.98:8080/TestWeb/ route=2 timeout=6 retry=20
                 BalancerMember http://10.0.135.95:8080/TestWeb/ route=3 timeout=6 retry=20
            ProxySet stickysession=ROUTEID
        </Proxy>
        <Location /balancer-manager>
                SetHandler balancer-manager
        </Location>
        ProxyPass /balancer-manager !
        ProxyPass /TestWeb/ "balancer://mycluster/" maxattempts=6 timeout=60
        ProxyPassReverse /TestWeb/ "balancer://mycluster"
        RewriteEngine on
        RewriteRule   ^/TestWeb$          /TestWeb/ [R,L]
</VirtualHost>

 

The Configuration

CustomLog creates its own log file

Each BalancerMember has a 6 seconds timeout limit. Change your configuration accordingly.

Header add Set-Cookie "ROUTEID= Uses the headers mod to add a new header to the request. This headier will be used for apache to redirect property.

ProxyPassReverse is used so that the client will receive the balancer's IP not the balancer member's.

RewriteRule is used in case somebody types /TestWeb without the last slash

 

Restart the Apache Server

sudo service apache2 restart

 

Check the Balancer

* http://your.apache.installation.ip/balancer-manager

Apache will run on port 80 while the Tomcats will use 8080 (default ports)

img/0/93/img.gif

* All the changes performed in the balancer will be lost when you restart the server.

Check that is Balancing property

* http://your.apache.installation.ip/TestWeb

img/0/93/img2.gif

Try Starting/Shutting Down your application servers accordingly.

Common Errors

  • Most of the errors that happen when Apache starts are produced because not all modules have been loaded.
  • "502 Proxy Error. The proxy server received an invalid response from an upstream server." This happens when the tomcat port is open but tomcat is not sending any data which happens when tomcat is re-deploying or starting. This may also be caused by a firewall. You may need to adjust your configuration. Check the reference http://httpd.apache.org/docs/2.4/mod/mod_proxy.html

Useful Commands

Check Apache Version

apache2ctl -v

Check Installed Modules

apache2ctl -M

Notes

  • Even If the application is not deployed Apache balancer with redirect to that server causing an error.
  • Apache VersiĆ³n 2.4 has been used in this example.