Introduction to mod_jk and other three Connection Methods for Apache HTTP Server and tomcat

Source: Internet
Author: User

First, let's explain why Apache and tomcat need to be connected. In fact, Tomcat itself already provides the HTTP service. The default port of this service is 8080. After Tomcat is installed, you can directly use the application run by Tomcat through port 8080, you can also change the port to 80.

Since Tomcat itself can provide such services, why should we introduce Apache or some other specialized HTTP servers? The reasons are as follows:

1. Improve the processing performance of static files

2. Use Web servers for load balancing and fault tolerance

3. seamless application upgrade

These three points are very important to a Web site. We hope that our website will not only be fast but also stable, users cannot access the server because a tomcat instance is down or a program is upgraded. The best HTTP server that can complete these functions is the Apache HTTP Server, it is the most closely and reliable combination with Tomcat.

Next we will introduce three methods to integrate Apache and tomcat. (Mod_jk)

JK

This is the most common method. You can find a lot of web pages about JK configuration on the Internet. Of course, the most comprehensive is the official documentation. JK itself has two versions: 1 and 2. The latest version of 1 is 1.2.19, and Version 2 has been abandoned. Later versions will not be released, therefore, we recommend that you use version 1.

JK communicates with the Tomcat server through the AJP protocol. The default port of the Tomcat AJP Connector is 8009. JK provides a page for monitoring and management of jkstatus. jkstatus can be used to monitor the current running status of JK and set the connection to Tomcat, as shown in:

Figure 1: Monitoring and Management page jkstatus

In this figure, we can see that JK is configured with two connections to ports 8109 and 8209 respectively. Currently, the connection S2 is stopped, the connection S1 has processed more than 0.47 million requests since the last restart, with the traffic reaching 6.2 GB and the maximum concurrency being 13. We can also use the jkstatus management function to switch JK to different Tomcat servers. For example, we can enable S2 and disable S1, which is very useful when updating applications, in addition, the entire switchover process is transparent to users, achieving seamless upgrade. There have been a lot of articles about JK configuration on the Internet. Here we will not detail the entire configuration process, but I want to talk about the configuration ideas, as long as I understand the configuration ideas, JK is a very flexible component.

JK configuration has three most critical files:

Httpd. conf

The configuration file of the Apache server, used to load the JK module and specify the JK configuration file information.

Workers. Properties

Connection definition file to Tomcat server

Uriworkermap. Properties

Uri ing file, used to specify which URLs are processed by Tomcat, you can also directly in httpd. conf configures these Uris, but the advantage of independent configuration is that the JK module regularly updates the content of this file, so that we do not need to restart the Apache server when modifying the configuration.

The second and third configuration file names can be customized. The following is a typical httpd. conf configuration for JK.

# (Httpd. conf)
# Load the 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 will create two new files under the conf directory of Apache: workers. properties and uriworkermap. properties. The contents of these two files are roughly as follows:

#
# workers.properties
#
# list the workers by name
worker.list=DLOG4J, status
# localhost server 1
# ------------------------
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 preceding workers. properties configuration is the configuration used for the page in front of the screen capture. First, we have configured two worker types, ajp13, S1 and S2, respectively. They point to the same server and run on two Tomcat servers with different ports 8109 and 8209. Next, we configure a worker of the Type LB (that is, the meaning of load balancing). Its name is dlog4j, which is a logical worker, it is used to manage the two physical connections S1 and S2. Finally, a worker of the Status type is configured, which is used to monitor JK. It is not enough to have these three workers. We also need to tell JK which workers are available, so there is the configuration of worker. List = dlog4j and status.

The next step is the URI ing configuration. We need to specify which links are processed by Tomcat and which are directly processed by Apache. You can see the following file to understand the meaning of the configuration.

/*=DLOG4J
/jkstatus=status
!/*.gif=DLOG4J
!/*.jpg=DLOG4J
!/*.png=DLOG4J
!/*.css=DLOG4J
!/*.js=DLOG4J
!/*.htm=DLOG4J
!/*.html=DLOG4J

 

I believe you have understood that all the requests are handled by the worker dlog4j, but with several exceptions, the worker/jkstatus requests are processed by the worker status. In addition, what is the exclamation point before each row of data in this configuration? The exclamation point indicates that the URI next is not processed by JK, that is, Apache directly processes all images, 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 previous requirements for a web site. Try it!

Http_proxy

This is to use the mod_proxy module provided by Apache to connect to Tomcat Using proxy technology. Before configuration, make sure that the Apache server version 2.2.x is used. This module is rewritten in version 2.2.x, greatly enhancing its functionality and stability.

The http_proxy mode is an HTTP-based proxy. Therefore, Tomcat must provide HTTP services, that is, Tomcat's HTTP ctor must be enabled. The simplest configuration is as follows:

ProxyPass /images !
ProxyPass /css !
ProxyPass /js !
ProxyPass / http://localhost:8080/

In this configuration, We proxy all http: // localhost requests to http: // localhost: 8080/, which is the Tomcat access address, except for images, CSS, and JS directories. We can also use mod_proxy for 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 running status of the cluster through a page, and make some simple maintenance settings.

Figure 2: monitor the cluster running status

Ajp_proxy

The ajp_proxy connection method is actually the same as the http_proxy method, and is provided by mod_proxy. The same is true for configurations. You only need to change http: // to AJP: // and connect to the port of Tomcat AJP Connector. The configuration in the preceding example can be changed:

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 the proxy connection method, you need to load the required modules on Apache. mod_proxy-related modules include mod_proxy.so, mod_proxy_connect.so, mod_proxy_http.so, mod_proxy_ftp.so, and callback, which are only available in Apache 2.2.x. If http_proxy is used, mod_proxy.so and mod_proxy_http.so must be loaded. If ajp_proxy is used, mod_proxy.so and mod_proxy_ajp.so modules must be loaded.

Comparison

Compared with JK's connection method, the last two methods are relatively simple in configuration, and the flexibility is not inferior at all. However, the stability is not as long as JK has been tested. After all, Apache 2.2.3 has not been launched for a long time, and there are not many websites using this connection method. Therefore, if it is applied to key Internet websites, we recommend that you use the JK connection method.

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.