Using JK to connect Apache with Tomcat to achieve load balancing

Source: Internet
Author: User
Tags tomcat server

Consolidating Apache Http server and Tomcat can improve processing performance for static files, leveraging WEB servers for load balancing and fault-tolerant, seamless upgrade applications.

Let's start by introducing why you want to connect Apache to Tomcat. In fact, Tomcat itself already provides the HTTP service, which has a default port of 8080, and you can change the port to 80 by installing Tomcat and using 8080 ports to run the application directly from Tomcat.

Since Tomcat itself can provide such services, why do we have to introduce Apache or some other dedicated HTTP server? There are several reasons for these:

1. Improve the processing performance of static files

2. Use Web server to do load balancing and fault tolerance

3. Seamless upgrade of applications

These three points are very important to a Web site, we want our site to be not only fast, but also stable, not because of a Tomcat downtime or upgrade program to cause users can not access, but to complete these functions, the best HTTP server is only Apache HTTP Server, it is the closest and most reliable combination of Tomcat.

JK is the most commonly used Apache and Tomcat connection, the latest version is JK-1.2.37 released, JK is through the AJP protocol with the Tomcat server communication, tomcat default AJP Connector port is 8 009. JK itself provides a monitoring and management of the page jkstatus, through Jkstatus can monitor the current working status of JK and the connection to Tomcat settings, Access can be http://IP/jkstatus.

1, the installation of JK:

Download tomcat-connectors-1.2.37-src.tar.gz

After decompression, enter the native directory under tomcat-connectors-1.2.37 and start compiling the installation:

[Root@centos6 tomcat-connectors-1.2.37]#./configure--prefix=/usr/local/tomcat-connector--with-apxs=/usr/local/ Apache2/bin/apxs
[root@centos6 tomcat-connectors-1.2.37]# make
[ROOT@CENTOS6 tomcat-connectors-1.2.37]# Make install

2, the configuration of JK

The most critical of JK's configuration is three files, respectively

workers.properties

Connection definition file to Tomcat server

httpd.conf

Apache server configuration file to load the JK module and specify the JK profile information

uriworkermap.properties

URI mapping file, which specifies which URLs are handled by Tomcat, and you can configure these URIs directly in httpd.conf, but the advantage of being independent of these configurations is that the JK module periodically updates the contents of the file, allowing us to modify the configuration without restarting the Apache server.

The second to third configuration file name can be customized. The following is a typical httpd.conf to JK configuration: # (httpd.conf) # load MOD_JK module LoadModule jk_module modules/mod_jk.so # conf Igure mod_jk # <ifmodule jk_module> jkworkersfile conf/workers.properties jkmountfile. Properties Jklogfile Logs/mod_jk.log Jkloglevel warn </IfModule>

modification of Tomcat configuration

The HTTP protocol used by Tomcat by default, and the service configuration file is Conf/server.xml, where the configuration of the Connection Agreement is, <connector port= "8080" protocol= "http/1.1" Connectiont imeout= "20000" redirectport= "8443"/>

To use the JK Connector, you need to modify the protocol configuration for AJP, the following is my configuration: <connector port= "8009" maxthreads= "minsparethreads=" "maxsparethreads=" "Enablelookups=" false "redirectport=" 8443 "acceptcount=" debug= "0" isableuploadtimeout= "true" Urien coding= "GBK" protocol= "ajp/1.3"/>

Next we create two new files in Apache's conf directory, respectively, Workers.properties, Uriworkermap.properties. The contents of these two documents are approximately as follows: #    # workers.properties    #    # list  the workers by name       worker.list=dlog4j, status        # localhost server 1    # ------------------------     worker.s1.port=8009    worker.s1.host=localhost    worker.s1.type=ajp13        # localhost server 2    # ------------------------     worker.s2.port=8010    worker.s2.host=localhost    worker.s2.type=ajp13     worker.s2.stopped=1       worker. DLOG4J.TYPE=LB    worker.retries=3    worker. Dlog4j.balanced_workers=s1, s2    worker. Dlog4j.sticky_session=1    worker.status.type=status  

Description

First we configured two types of ajp13 for S1 and S2, which point to Tomcat running on two different ports 8109 and 8209 on the same server. We then configured a worker with a type of lb (that is, the meaning of load balancing), its name is dlog4j, a logical worker that manages the two physical connections S1 and S2 that were previously configured. Finally, a worker of type status is configured, which is the module used to monitor the JK itself. With these three worker is not enough, we also need to tell JK, which worker is available, so there is worker.list = dlog4j, status this line configuration.

The next step is the mapping configuration of the URI, we need to specify which links are handled by Tomcat, and which are handled directly by Apache, and look at the following file and you can see the meaning of the configuration/*=dlog4j/jkstatus=status!/*.gi f=dlog4j!/*.jpg=dlog4j!/*.png=dlog4j!/*.css=dlog4j!/*.js=dlog4j!/*.htm=dlog4j

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.