The Apache of Load balancer software and the integration with Tomcat

Source: Internet
Author: User

Apache can be used not only as a Web server, but also as a load balancer server or as a reverse proxy server.

Some text and content comes from the web.

First, Apache is not designed to be a load balancer server, and if you need to use Apache's load balancer, you need to manually open the Apache mod_proxy module.

Using Apache for load balancing, there are some basic configurations

1). Basic Configuration

Suppose there is a domain name: www.a.com's website to use Apache as a load balancer device, you first need to enable several of the Apache's required modules:

httpd.conf Code

LoadModule proxy_modulemodules/mod_proxy.so

LoadModule proxy_balancer_modulemodules/mod_proxy_balancer.so

LoadModule proxy_http_modulemodules/mod_proxy_http.so

Mod_proxy provides proxy server functionality, Mod_proxy_balancer provides load balancing capabilities, Mod_proxy_http allows the proxy server to support the HTTP protocol. If you replace mod_proxy_http with other protocol modules (such as MOD_PROXY_FTP), you can support load balancing for other protocols.

Then you add the following configuration:

httpd.conf Code

Proxyrequests OFF

<proxy balancer://mycluster>

balancermemberhttp://node-a.myserver.com:8080

balancermemberhttp://node-b.myserver.com:8080

</Proxy>

proxypass/balancer://mycluster/

# Warning: The following configuration is only for debugging, never to be added to the production environment!!!

<Location/balancer-manager>

SetHandler Balancer-manager

Order Deny,allow

Deny from all

Allow from localhost

</Location>

Note: node-a.myserver.com,node-b.myserver.com is the domain name of the other two servers, not the domain name of the current server

From the above proxyrequests off, it can be seen that the load balancer is actually a reverse proxy, but its proxy forwarding address is not a specific server, but a balancer://protocol:

The Proxypass/balancer://mycluster protocol address can be defined casually. Then, set the contents of the Balancer protocol in the <Proxy> section. The balancermember instruction can add a real server address in a Load balancer group.

The following <Location/balancer-manager> is used to monitor load balancing work, and can be added when debugging (no use in production environment!). ), and then visit http://localhost/balancer-manager/to see a load-balanced work condition.

OK, restart the server after the change, access your Apache server address (www.a.com), you can see the effect of load balancing.

Error message:

Visit Web page Tip internal serveral Error, view Error.log file

Error.log Code

[Warn] Proxy:no protocol handler wasvalid for the Url/admin/login_form. If you is using a DSO version ofmod_proxy, make sure the proxy submodules is included in the configurationusing Loadmodu Le.

The reason is configuration: # Proxypass/balancer://mycluster may be missing one/

2). Load Proportional Distribution

Open the Balancer-manager interface and you can see that the request is evenly distributed.

What if you don't want to distribute the average? Add the Loadfactor parameter to Balancermember and the value range is 1-100. For example, you have three servers, the load distribution ratio is 7:2:1, just set:

httpd.conf Code

Copy the code code as follows:

Proxyrequests OFF

<proxy balancer://mycluster>

balancermemberhttp://node-a.myserver.com:8080 loadfactor=7

balancermemberhttp://node-b.myserver.com:8080 loadfactor=2

balancermemberhttp://node-c.myserver.com:8080 loadfactor=1

</Proxy>

Proxypass/balancer://mycluster

3). Load Allocation algorithm

By default, load balancing tries to match the number of requests accepted by each server to a predetermined scale. If you want to change the algorithm, you can use the Lbmethod property. Such as:

httpd.conf Code

Proxyrequests OFF

<proxy balancer://mycluster>

balancermemberhttp://node-a.myserver.com:8080 loadfactor=7

balancermemberhttp://node-b.myserver.com:8080 loadfactor=2

Balancermember http://node-c.myserver.com:8080loadfactor=1

</Proxy>

Proxypass/balancer://mycluster

Proxyset lbmethod=bytraffic

Possible values for Lbmethod are:

Lbmethod=byrequests by Request number equalization (default)

Lbmethod=bytraffic according to the flow balance

Lbmethod=bybusyness in a busy balance (always assigned to the server with the fewest number of active requests)

See the Apache documentation for the principles of the various algorithms.

2. Thermal backup (Hot Standby)

The implementation of hot backup is simple, just add the Status=+h property, you can designate a server as a backup server:

httpd.conf Code

Proxyrequests OFF

<proxy balancer://mycluster>

balancermemberhttp://node-a.myserver.com:8080

balancermemberhttp://node-b.myserver.com:8080 status=+h

</Proxy>

Proxypass/balancer://mycluster

As you can see from the Balancer-manager interface, the request always flows to node-a, and once the node-a hangs, Apache detects the error and diverts the request to node-b. Apache will check the status of node-a every few minutes and continue to use Node-a if node-a resumes.

The load Balancing group for Tomcat and Apache

Here are 2 tomcat plus an Apache load balancer set for reference.

Preparatory work:

1. Using the Tomcat-free version, in a different directory, Tomcat uses a different port?

2. Install the JDK;

3. Download and install Apache_2.2.11-win32-x86-no_ssl.msi

is Apache using Apache_2.2.11-win32-x86-no_ssl.msi? If the configuration of the version below 2.2Apache load balancer is different, because this 2.2.11 and 2.2.8 version integrates load Balancing tools such as JK2, So it's a lot easier to configure?

Install Apache as a Windows service running on port 80, you can see the Apache2.2 service in the list of system services after the installation is successful? After the service is started, enter http://localhost in the browser to test, if you can see a "itworks!" The page on behalf of Apache has been working properly?

Locate the conf/httpd.conf file in the Apache installation directory, and remove the comment (#) before the following text to allow Apache to load the proxy module automatically at boot time?

Loadmoduleproxy_module modules/mod_proxy.so

Loadmoduleproxy_ajp_module modules/mod_proxy_ajp.so

Loadmoduleproxy_balancer_module modules/mod_proxy_balancer.so

Loadmoduleproxy_connect_module modules/mod_proxy_connect.so

Loadmoduleproxy_ftp_module modules/mod_proxy_ftp.so

Loadmoduleproxy_http_module modules/mod_proxy_http.so

Pull the document down to find the <IfModuledir_module> node, after directoryindexindex.html add index.jsp, this step is only to be configured after Tomcat can see the kitten home, you can not do? Continue to the drop-down document to find the include conf/extra/httpd-vhosts.conf, and remove the previous comment characters?

Then open conf/extra/httpd-vhosts.conf, configure the virtual site, and at the bottom add

<VirtualHost*:80>

ServerAdmin Admin Mailbox

Servernamelocalhost

Serveraliaslocalhost

Proxypass/balancer://sy/stickysession=jsessionid Nofailover=on

proxypassreverse/balancer://sy/

Errorlog "Logs/sy-error.log"

Customlog "Logs/sy-access.log" common

</VirtualHost>

Then go back to the httpd.conf and add the bottom of the document

Proxyrequestsoff

<proxybalancer://sy>

balancermemberajp://127.0.0.1:8009 loadfactor=1 ROUTE=JVM1

balancermemberajp://127.0.0.1:9009 loadfactor=1 route=jvm2

</proxy>

Proxyrequestsoff is to tell Apache to use a reverse proxy, the IP address and port uniquely determine the Tomcat node and the configured AJP accept port? Loadfactor is a load factor, Apache will be the load factor to the back end of the tomcat node forwarding requests, the larger the load factor, the corresponding Tomcat server will handle the more requests, such as two Tomcat are 1,apache 1:1 of the ratio of forwarding, if 2 and 1 are forwarded by 2:1? This makes the configuration more flexible, for example, you can increase the proportion of the performance of the server processing work, if you take more than one server, only need to modify the IP address and port on it? The route parameter corresponds to the engine path in the subsequent Tomcat load Balancer configuration (Jvmroute)

The Apache of Load balancer software and the integration with Tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.