The integration of Apache and multiple tomcat

Source: Internet
Author: User
Tags tomcat server apache tomcat

The problem of Apache and Tomcat integration is often encountered in web development, but many articles on the internet are copied by others, I have been configured for a HALF-DAY has not been configured successfully. However, I found a classic configuration description on the IBM Web page, and by that I finally got the combination of Apache and multiple tomcat. Interested can go to this site to see: http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/index.html

Here's what I'm going to take from this page. Three ways to connect Apache HTTP server to Tomcat to consolidate Apache HTTP server and Tomcat can improve the processing performance of static files, use the WEB server to do the load and fault-tolerant, seamless upgrade applications. This article describes three ways to integrate Apache and Tomcat.

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.

Next we'll introduce three ways to integrate Apache and Tomcat together.

Jk

This is the most common way you can find a lot of web pages about configuring JK on the Web, and of course the most complete or the official documents provided. JK itself has two versions are 1 and 2 respectively, the current 1 latest version is 1.2.19, and version 2 has been abandoned, there will be no new version of the launch, so we recommend that you use version 1.

JK communicates with the Tomcat server through the AJP protocol, and the port of the Tomcat default AJP Connector is 8009. 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, as shown in the following figure:


Figure 1: Page jkstatus for monitoring and management

In this diagram we can see that the current JK with two connections to the 8109 and 8209 ports, currently S2 This connection is stopped, and S1 this connection since the last reboot has processed more than 470,000 requests, traffic reached 6.2 G, the maximum number of concurrent 13 and so on. We can also use the Jkstatus management function to switch JK to different Tomcat, such as the S2 enabled, and deactivate S1, this is very useful in updating the application, and the entire switching process for the user is transparent, also achieve the goal of seamless upgrade. About JK configuration article online already very much, here we no longer detail the entire configuration process, but I want to talk about the idea of configuration, as long as the idea of configuration, JK is a very flexible component.

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

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

workers.properties
Connection definition file to Tomcat server

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 configuration of httpd.conf for JK


# (httpd.conf)
# load MOD_JK module
LoadModule jk_module modules/mod_jk.so # Configure
mod_jk

# Jkworkersfile conf/workers.properties
jkmountfile conf/uriworkermap.properties
JkLogFile logs/mod_jk.log
Jkloglevel warn

Next we create two new files in Apache's conf directory, respectively, Workers.properties, Uriworkermap.properties. The contents of these two documents are probably as follows


# #
workers.properties
#


list

the workers by name worker.list=dlog4j, status

# localhost server 1< c6/>#------------------------
worker.s1.port=8109
worker.s1.host=localhost
worker.s1.type=ajp13

# localhost server 2
#------------------------
worker.s2.port=8209
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

The above workers.properties configuration is the configuration used by the screen capture page in front of us. 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 file below to see what the configuration means.


/*=dlog4j
/jkstatus=status

!/*.gif=dlog4j
!/*.jpg=dlog4j
!/*.png=dlog4j
!/*.css=dlog4j
!/*.js=dlog4j
!/*.htm=dlog4j
!/*.html=dlog4j

I believe you have understood the half: all requests are handled by the dlog4j worker, but with a few exceptions,/jkstatus request is handled by the status worker. What do you mean by the exclamation point in front of each row of data in this configuration? Exclamation point indicates that the next URI should not be processed by JK, that is, Apache directly handles all pictures, CSS files, JS files, and static HTML text files.

Through the configuration of Workers.properties and uriworkermap.properties, there can be a variety of combinations to meet the requirements we made earlier on a Web site. You might as well try.







Http_proxy

This is using Apache's own mod_proxy module to connect Tomcat with proxy technology. Make sure that you are using the 2.2.x version of the Apache server before you configure it. Because the 2.2.x version of this module is rewritten, greatly enhance its functionality and stability.

The Http_proxy mode is a proxy based on the HTTP protocol, so it requires Tomcat to provide the HTTP service, which means that Tomcat's HTTP Connector must be enabled. One of the simplest configurations is as follows


Proxypass/images!
Proxypass/css!
Proxypass/js!
proxypass/http://localhost:8080/

In this configuration, we have all http://localhost request Agent to http://localhost:8080/, this is the address of Tomcat, in addition to images, CSS, JS several directories except. We can also use Mod_proxy to do load balancing, and then look at the following configuration


Proxypass/images!
Proxypass/css! 
Proxypass/js!

proxypass/balancer://example/
<proxy balancer://example/>
balancermember http://server1:8080/
Balancermember http://server2:8080/
balancermember http://server3:8080/
</Proxy>

The configuration is much simpler than JK, and it can also monitor the state of the cluster's operation through a single page and make some simple maintenance settings.


Figure 2: Monitoring cluster running status







Ajp_proxy

Ajp_proxy connection mode is in fact the same as the Http_proxy way, are provided by the Mod_proxy function. The same is true for configuration, where you only need to replace http://with ajp://, which is also connected to the port where Tomcat's AJP Connector is located. The configuration of the example above can be changed to:


Proxypass/images!
Proxypass/css! 
Proxypass/js!

proxypass/balancer://example/
<proxy balancer://example/>
balancermember ajp://server1:8080/
Balancermember ajp://server2:8080/
balancermember ajp://server3:8080/
</Proxy>

Using proxy connection, you need to load the required modules on Apache, mod_proxy related modules are mod_proxy.so, mod_proxy_connect.so, mod_proxy_http.so, Mod_proxy_ Ftp.so, mod_proxy_ajp.so, where mod_proxy_ajp.so is only available in Apache 2.2.x. If you are using the Http_proxy method, you need to load mod_proxy.so and mod_proxy_http.so, and if it is ajp_proxy you need to load the two modules mod_proxy.so and mod_proxy_ajp.so.







Comparison of the three

Compared to JK connection mode, the latter two in the configuration is relatively simple, flexibility is not inferior. But in terms of stability is not as time-tested as JK, after all, the Apache 2.2.3 launch time is not long, the use of this link to the site is not much, therefore, if it is applied to the key Internet sites, or recommend the use of JK link.



Resources for Apache Http Server.

Get Apache Tomcat.

JK document.



About the author

Liu Dong, has been using J2EE/J2ME to engage in mobile internet development. You can contact him via the Java Free People website: http://www.dlog.cn/javayou, and his email address is javayou@gmail.com.

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.