Last updated on August 12th, 2022 at 10:30 am
In this tutorial we are going to setup load balancing using Apache. Couple of modules in Apache needs to enabled for this. Before we get started let me walk you through the server details and environment in which I am running this load balancer
I have an URI /myapp defined in Server 1 which gets forwarded to Server 2. The set up is done in such a way that if Server 2 fail to respond to URL /myapp then the request will be served by Server 1.
Server 1 Environment
Server version: Apache/2.4.29 (Ubuntu)
Server built: 2022-06-23T12:51:37
DISTRIB_DESCRIPTION=”Ubuntu 18.04.6 LTS”
Server 1 : Will have the proxy configuration with /myapp configured
Server 2 : Default Apache WebServer / Any web server listening on Port 80 or any port of your choice.(It can be any OS). My server 2 IP address is 172.31.69.173 and port is 80
Server 1 Proxy Balancer
As discussed Server 1 is the main server from which the requests are routed. So we are enabling the proxy configurations on this server.
Make sure to enable all these modules using a2enmod
Just run for example if you are enabling proxy_http module
$ sudo a2enmod proxy_http
Repeat same command (replacing the module names) for enabling other modules.
More details on
Now that we have enabled the modules. Let us create a file named loadbalancer.conf under /etc/apache2/conf-available
Add these lines to the loadbalancer.conf file (Modify IP address and ports as per your requirement)
# Reverse Proxy /myapp to an internal web service, with fail-over to a hot standby <proxy balancer://appcluster> # the hot standby on Server 1 BalancerMember http://127.0.0.1:80 status=+H # Forward request to Server 2 BalancerMember http://172.31.69.173:80 </proxy> <location /myapp> ProxyPass balancer://appcluster ProxyPassReverse http://127.0.0.1:80 ProxyPassReverse http://172.31.69.173:80 </location>
Once created run the commands below to enable to configuration and restart Apache
[email protected]/etc/apache2/conf-available## a2enconf loadbalancer.conf Enabling conf loadbalancer. To activate the new configuration, you need to run: systemctl reload apache2
This config tells Apache to proxy requests for /myapp to a web service on http://172.31.69.173:80 (You don’t have to explicitly give 80 as it will be by default, if you want Apache to hit a different port other than 80 then provide that port number)
If that service becomes unavailable (ie: you take it down for maintenance) then it will automatically send requests to http://127.0.0.1:80 (that is the proxy loadbalancer itself). The “status=+H” defines that member as a Hot Standby. When the default service is back on-line mod_proxy_balancer will pick that up within about 60 seconds or so and revert back to forwarding all requests to it.
The ProxyPassReverse directives are unrelated to the proxy balancing, but are usually required if you want to handle redirects/etc properly.
See the example on how the request flow happens with screenshots
Step 1, Hit the URI <server>/myapp
As you can see when I hit /myapp it went straight to Server 2 that is configured. In this case the above page is deployed on 172.31.69.173
Step 2, I stopped Apache on Server 2 and this is the page it loads when I hit the URI again <server>/myapp
Since Server 2 is down Apache loaded page from Server 1 which is the hot standby as per our configuration.
Step 3 : Start Apache again on Server 2 and see the same page as shown in Step 1