Test: Load balancing and reverse proxy with Apache

Hello,

I just quickly try load balancing on Apache, it works well. It’s free and there is no need for other software or plug-ins. It works on all platforms and it works with ssl without changing the existing settings.

Unfortunately I do not have time to look in depth at the moment. There must have errors and it should be optimized, but for those who want to try, it works.

I am assuming that you already have an Apache server (Linux, Windows…) and web apps configured in reverse proxy.

For each subdomain (each vitualhost), enter the following code in the Apache configuration file:

Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

To inject a cookie tracking in each request. This cookie will allow each session (each browser I think) will always follow the same path/app, even if there are several sessions that use the same IP.

<Proxy "balancer://mycluster"> BalancerMember "http://127.0.0.1:8035" route=app1 BalancerMember "http://127.0.0.1:8030" route=app2 ProxySet lbmethod=bybusyness ProxySet stickysession=ROUTEID </Proxy>

Here, we see that two xojo web apps (http://127.0.0.1:8035 and http://127.0.0.1:8030) share the same address (the same subdomain of the virtualhost). Of course, you can run more applications. Requests redirected to the first app will have an “app1” cookie, and those that are redirected to the second app will have an “app2” cookie.
We indicate also that the load balancing method will be “bybusiness”. I’ve had good results with this method, but can-be that others are better.
Finally, we indicate that once a browser was directed to a web app, it must always be directed to this app for the following requests, by following the ROUTEID cookie (app1 ou app2).

ProxyPassMatch ^(/.*)$ balancer://mycluster$1

This last line of code replaces the proxypass directives, so all requests go to the load balancing.

Thats all. Not forget to activate the modules by uncommenting:

LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Do not use this code in production, this is just a proof of concept. There must be errors. but improve it and please publish the code in comment…
For those seeking an already robust code, there are excellent articles of John on Nginx and HAproxy.

sorry for my bad English…

Of course, don’t forget the usual reverse proxy parameters, as:

ProxyTimeout 200

(the Xojo push/ping requests being 180 seconds)

Apache also provides a visual manager for the load balancer:

In the “busy” column, you can see the number of connected sessions. In this screenshot, there are 3 clients connected to the app 1 and 2 clients connected to the app 2.

To enable this display, enter the following code:

ProxyPass /balancer-manager !
<Location /balancer-manager>
   		SetHandler balancer-manager
		require ip xxxx.xxxx.xxxx.xxxx
</Location>

and uncomment:

LoadModule status_module modules/mod_status.so

You can then view the manager to the specified IP address and at http: //mysubdomain/balancer-manager

By clicking the links, you can edit the load balancing settings and even each app settings. Of course, there are many more parameters in the Apache configuration file. And the parameters changed in the manager are deleted on the next reboot.The manager is especially useful for viewing and modifying a parameter in emergency, stop redirecting to an app for updating, etc.

It remains to make the most difficult: if an application is frozen/busy, automatically directing all new sessions to another application. Quickly.

Even for current users: If an application is frozen/busy, and a user is already connected to this app, he will remain stuck on this application because of the cookie. Even if he refreshes the page, Apache still try to direct it to the same application. We should ask the user to remove the cookie …But as new sessions, perhaps it is redirected to the busy application… It’s a problem.

There must be a tuning. I played around with the timeouts and the number of Failover Attempts, but I have not found a solution yet.

By cons, if an application is fully stopped (not frozen / busy, but the service is stopped or the process destroyed),then Apache handles it immediately. All new sessions are automatically redirected to other applications. And for current sessions: the cookie is automatically changed. If the user refreshes the page, it is redirected to another application without delay.