How to use Apache reverse proxy as Load Balancer

Earlier we have learned about how we can configure apache as reverse proxy. As discussed earlier, Reverse proxy is a kind of a proxy that takes http or https request & transfers/distributes them to one or more backend servers. Reverse proxy is useful in many ways, like

It can hide the origin serve, thus making it more secure & immune to attacks,

It can act as a load balancer,

Reverse proxy can also be used to encrypting/decrypting webserver traffic, thus taking some load off from the backend servers.

It can also be used for caching static as well as dynamic contents, which also reduces load off the web servers.

We have covered up the redirection part in our earlier tutorial, in this tutorial we are going the discuss how we can use apache reverse proxy as load balancer.

In this tutorial, we will be using three instances of Apache tomcat server & will than use apache reverse proxy as load balancer to distribute & redirect the requests to these three tomcat servers.

(Recommended Read: Creating a Wildfly cluster for load-balancing in Linux)

 

Pre-requisites

Install Apache on the server meant to be used as reverse proxy with the following command,

$ sudo yum install httpd

For detailed installation of Apache webserver, refer to our article ‘Step by Step guide to configure APACHE server ‘

On the three backend servers, install Apache tomcat. Read our detailed tutorial on how to install Apache tomcat to setup the backend servers.

 

Apache modules required

1- mod_proxy – it is the main module responsible for redirecting the connections,

2- mod_proxy_http – add the support for proxying HTTP connections,

3- mod_proxy_balancer and mod_lbmethod_byrequests – Both these modules are required if you are planning to use reverse proxy as load balance as well.

Check if the following modules are installed & working with the following command,

$ httpd -M

This command will generate the list of modules that are currently working . If these modules are not among the list, than we need to enable them by making the following entry in httpd.conf,

$ sudo vim /etc/httpd/conf/httpd.conf

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_http_module modules/mod_proxy_h

LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

Now save the file & exit, than restart the apache service to implement the changes made,

$ sudo systemctl restart httpd

 

Configuring Backend servers

Once we have apache tomcat installed, we don’t need to make any changes to the app server. We will just need the URL for the application hosted on all the tomcat servers, i.e.

http://192.168.1.110:8080/test/

http://192.168.1.120:8080/test/

http://192.168.1.130:8080/test/

Note:- For testing purposes, we can host three different pages on all the tomcat instances so as to identify which webpage is coming from which server.

 

Configuring the reverse proxy

To configure the apache reverse proxy as load balancer, we need to add some configurations to main apache configuration file ,’ httpd.conf ‘,

$ sudo vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>

<Proxy balancer://cluster>

BalancerMember http://192.168.1.110:8080/test

BalancerMember http://192.168.1.120:8080/test

BalancerMember http://192.168.1.130:8080/test

</Proxy>

ProxyPreserveHost On

ProxyPass / balancer://cluster/

ProxyPassReverse / balancer://cluster/

</VirtualHost>

here ‘<Proxy balancer://cluster>’ is the part where we mention the all the tomcat instances & ‘ProxyPass’ is the part which handles the redirection. After making changes to file, save it & restart the apache service to implement the changes.

$ sudo systemctl restart httpd

 

Checking the load balancing

To check the reverse proxy & load balancing, open the web browser & enter the following URL,

http://192.168.1.100

Where, 192.168.1.100 is the IP address of the reverse proxy server. Now open the URL from two other machines or browsers, all the three opened webpages should be coming from the different application servers. We can make sure by checking the server/access logs of tomcat servers or we can also host 3 different pages on the application servers as mentioned above.

This was our tutorial on how to use apache reverse proxy as load balancer. Please don mention your questions and queries using the comment box below.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of you hard earned money:

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

5 Responses

  1. mmiat says:

    ProxyPass /test/ balancer://AppClusterTest/
    and
    ProxyPass /test balancer://AppClusterTest/ (without final slash)
    is the same thing?
    thanks

  2. vikram says:

    thankyou i have one doubt by this configuration it seem that request goes in round robin fashion.please suggest solution if i want to configure load balancing between tomcats installed in different servers.

  1. December 20, 2017

    […] How to use Apache reverse proxy as Load Balancer […]

Leave a Reply

Your email address will not be published. Required fields are marked *