Difference between revisions of "Configure Apache load balancer with mod proxy"

From LogicalDOC Community Wiki
Jump to navigationJump to search
(Configure Tomcat)
Line 80: Line 80:
  
 
The proxyName will cause servlets inside this web application to think that all proxied requests were directed to ldproxy.org on port 80
 
The proxyName will cause servlets inside this web application to think that all proxied requests were directed to ldproxy.org on port 80
 +
 +
== Done ==
 +
 +
That's basically it. Now you can set your DNS entry of www.yourcompany.com to your Load-Balancer's IP address and enjoy the comfort and security of a redundant LogicalDOC installation. If one of the public LogicalDOC servers is failing, mod_proxy on your load-balancer will automatically detect this and stop serving requests to that server.
 +
 +
You can test this by stopping Tomcat on one of the machines. Your load-balancer Apache webserver error_log will show something like
 +
 +
<source lang="text">
 +
[Tue Jul 28 18:17:35 2009] [error] proxy: AJP: failed to make connection to backend: public1.yourcompany.com
 +
[Tue Jul 28 18:17:36 2009] [error] ap_proxy_connect_backend disabling worker for (public1.yourcompany.com)
 +
</source>
 +
 +
Also you could access the balancer-manager with http://www.yourcompany.com/balancer-manager to manually disable a worker. The balancer-manager also offers you an easy way to set different load factors for your servers.
  
 
== Additional information ==
 
== Additional information ==

Revision as of 13:31, 27 June 2019

Server setup

The layout may look something like this (we will refer to these names through the rest of the guide).

Install Apache modules in Ubuntu

Use the following commands:

$ sudo a2enmod proxy
$ sudo a2enmod proxy_http
$ sudo a2enmod proxy_balancer
$ sudo a2enmod lbmethod_byrequests
$ sudo a2enmod headers

Run all the above commands then restart Apache to obtain the effect of changes we have made.

$ sudo service apache2 restart

Define Apache Load-balancer

This server will handle all HTTP requests from site visitors. As you might see, this means even though you run a load balanced system, using only a single load balancer means you still have a SPOF (single point of failure). It is also possible to configure an environment where yet another server will act as the fail-over load-balancer if the first one fails, but this is outside the scope of this guide.

To set up our load-balancer, we use the Apache web-server and its modules mod_proxy, mod_proxy_http and mod_proxy_balancer. These are part of most of the Apache web-server distributions.

First, create a virtual host handling the requests for your domain: ldproxy.org

<VirtualHost *:80>

  ServerName ldproxy.org

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html

  ProxyRequests Off
  ProxyPreserveHost On

  Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
  <Proxy balancer://mycluster>
        BalancerMember "http://eva00:9080" route=1
        BalancerMember "http://192.168.2.15:8082" route=2

        ProxySet lbmethod=byrequests
        ProxySet stickysession=ROUTEID
    </Proxy>

  ProxyPass / balancer://mycluster/
  ProxyPassReverse / balancer://mycluster/

  ErrorLog ${APACHE_LOG_DIR}/proxy-error.log
  CustomLog ${APACHE_LOG_DIR}/proxy-access.log combined

</VirtualHost>

The load balancer nodes are two LogicalDOC servers that are accessed by IP and by hostname (eva00), of course the Apache server must be able to resolve the hostname.

Furthermore we can note that the Apache server adds a Cookie ROUTEID to make sessions sticky and redirect all subsequent requests to the same LogicalDOC node of the cluster

The setting ProxySet lbmethod=byrequests distribute the requests among the various workers to ensure that each gets their configured share of the number of requests.

Configure Tomcat

You need to change the configuration of the Tomcat in LogicalDOC. Generally it is a matter of modifying the file server.xml located in the /tomcat/conf folder

Tomcat config: LOGICALDOC_HOME/tomcat/conf/server.xml

In this file we are going to add the proxyName and proxyPort attributes to the Connector element

    <Connector port="8082" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8445"
               URIEncoding="UTF-8" server="Undisclosed/8.41" proxyName="ldproxy.org" proxyPort="80" />

The proxyName will cause servlets inside this web application to think that all proxied requests were directed to ldproxy.org on port 80

Done

That's basically it. Now you can set your DNS entry of www.yourcompany.com to your Load-Balancer's IP address and enjoy the comfort and security of a redundant LogicalDOC installation. If one of the public LogicalDOC servers is failing, mod_proxy on your load-balancer will automatically detect this and stop serving requests to that server.

You can test this by stopping Tomcat on one of the machines. Your load-balancer Apache webserver error_log will show something like

[Tue Jul 28 18:17:35 2009] [error] proxy: AJP: failed to make connection to backend: public1.yourcompany.com
[Tue Jul 28 18:17:36 2009] [error] ap_proxy_connect_backend disabling worker for (public1.yourcompany.com)

Also you could access the balancer-manager with http://www.yourcompany.com/balancer-manager to manually disable a worker. The balancer-manager also offers you an easy way to set different load factors for your servers.

Additional information