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

From LogicalDOC Community Wiki
Jump to navigationJump to search
(Created page with " == Server setup == The layout may look something like this (we will refer to these names through the rest of the guide). == Define Apache Load-balancer == This server wi...")
 
(Define Apache Load-balancer)
Line 16: Line 16:
 
<source lang="text">
 
<source lang="text">
 
<VirtualHost *:80>
 
<VirtualHost *:80>
        ServerName www.yourcompany.com
 
        ServerAlias yourcompany.com
 
  
        DocumentRoot /Library/WebServer/Documents
+
# value is not decisive as it is used as a last resort host regardless.
 +
# However, you must set it for any further virtual host explicitly.
 +
  ServerName ldproxy.org
  
        ProxyRequests Off
+
ServerAdmin webmaster@localhost
 +
DocumentRoot /var/www/html
  
        <Proxy *>
+
  ProxyRequests Off
          Order deny,allow
+
  ProxyPreserveHost On
          Allow from all
 
        </Proxy>
 
  
        ProxyPass /balancer-manager !
+
  Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
        ProxyPass / balancer://mycluster/ stickysession=JSESSIONID nofailover=On
+
  <Proxy balancer://mycluster>
        ProxyPassReverse / ajp://public1.yourcompany.com:8009
+
        BalancerMember "http://eva00:9080" route=1
        ProxyPassReverse / ajp://public2.yourcompany.com:8009
+
        BalancerMember "http://192.168.2.11:8080" route=2
        <Proxy balancer://mycluster>
 
          BalancerMember ajp://public1.yourcompany.com:8009 route=public1
 
          BalancerMember ajp://public2.yourcompany.com:8009 route=public2
 
          ProxySet lbmethod=byrequests
 
        </Proxy>
 
  
        <Location /balancer-manager>
+
      # AllowOverride None
          SetHandler balancer-manager
+
      # Order allow,deny
        </Location>
+
      # allow from all
  
         <Directory "/Library/WebServer/Documents">
+
         ProxySet lbmethod=byrequests
  AllowOverride AuthConfig
+
        ProxySet stickysession=ROUTEID
</Directory>
+
    </Proxy>
  
 +
  ProxyPass / balancer://mycluster/
 +
  ProxyPassReverse / balancer://mycluster/
 +
 +
ErrorLog ${APACHE_LOG_DIR}/proxy-error.log
 +
CustomLog ${APACHE_LOG_DIR}/proxy-access.log combined
  
 
</VirtualHost>
 
</VirtualHost>
</source>
 
 
We exclude the path balancer-manager from the proxy, since we can manage our balanced members with the balancer-manager tool. The balancer-manager is part of the mod_proxy_balancer module. To secure its path, create the folder and secure it. The setup may slightly differ on your server, so use the following as a starting guide.
 
 
<source lang="bash">
 
mkdir /Library/WebServer/Documents/balancer-manager
 
htpasswd -c /etc/apache2/conf/security/users administrator
 
cd /Library/WebServer/Documents/balancer-manager
 
touch .htaccess
 
vi .htaccess
 
</source>
 
 
<source lang="text">
 
AuthName "secured"
 
AuthType Basic
 
AuthUserFile users
 
 
require valid-user
 
</source>
 
  
 
== Configure Tomcat Public1 / Public2 ==
 
== Configure Tomcat Public1 / Public2 ==

Revision as of 10:34, 27 June 2019

Server setup

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


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_ajp 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: www.yourcompany.com

<VirtualHost *:80>

	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
  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.11:8080" route=2

       # AllowOverride None
       # Order allow,deny
       # allow from all

        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>

== Configure Tomcat Public1 / Public2 ==

Let's look at the relevant configuration here to set up the load-balancer. Most likely you will also have an Apache web-server installed on this machines, as for accessing the author instance if located on one of this servers with a nice URL. Here we suggest to use a single Tomcat application server for hosting one public instance. Make sure the AJP Port is set correctly to what you have defined in the virtual host configuration of the load-balancer (8009 as the default value used here).

If you want to change the AJP Port of your application server, this can be done here.

Tomcat config: <span style="background-color: #F2CEF2;">LOGICALDOC_HOME/tomcat/conf/server.xml</span>

<source lang="text">
<Connector port="8009" protocol="AJP/1.3" (...)>

Now in the same file as we configure the AJP Port server.xml we need to configure the jvmRoute for sticky sessions working correctly. Use the name defined in the virtual host configuration on load-balancer, the route value here separately for the two servers.

<Engine name="Catalina" defaultHost="localhost" jvmRoute="publicXY">

Note: on the LogicalDOC node Tomcat's config you should change publicXY with public1 or public2 depending on the node

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.