"Reprint" Apache HTTP Server and Tomcat three ways to connect the introduction

Source: Internet
Author: User

This article is reproduced from IBM developer

Let's start by introducing why we want to connect Apache with Tomcat. In fact, Tomcat itself has provided the HTTP service, the default port of the service is 8080, after the installation of Tomcat through the 8080 port can be directly used by Tomcat running applications, you can also change the port to 80.

Now that Tomcat itself can provide such a service, why should we introduce Apache or some other specialized HTTP server? There are several reasons:

1. Improve the processing performance of static files

2. Using a WEB server for load balancing and fault tolerance

3. Seamless upgrade of applications

This three point for a Web site is very important, we hope that our site is not only fast, but also stable, not because of a Tomcat outage or upgrade program caused the user can not access, and to complete these features of the best HTTP server is only Apache HTTP Server, it is the most closely and reliably combined with Tomcat.

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

Jk

This is the most common way, you can find many Web pages on the Internet to configure JK, of course, the most complete is the official documentation. JK itself has two versions are 1 and 2, the current 1 latest version is 1.2.19, and version 2 has been obsolete, and no longer have a new version of the launch, it is recommended that you use version 1.

JK is communicating with the Tomcat server via the AJP protocol, and the tomcat default AJP Connector port is 8009. JK itself provides a monitoring and management page Jkstatus, through which jkstatus can monitor JK's current working status and set the connection to Tomcat, as shown in:

Figure 1: Monitoring and managing the page Jkstatus

In this figure we can see the current JK with two connections to 8109 and 8209 ports, the current S2 This connection is a stop state, and S1 this connection since the last restart has processed more than 470,000 requests, traffic reached 6.2 G, the largest number of concurrency 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, which is very useful when updating the application, and the entire switching process for the user is transparent, also achieved a seamless upgrade. About JK configuration articles online has been very much, here we no longer detailed introduction of the entire configuration process, but I want to talk about the configuration of the idea, as long as the configuration of the idea, JK is a very flexible component.

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

httpd.conf
Apache server configuration file for loading JK modules and specifying JK profile information

Workers.properties
Connection definition file to the TOMCAT server

Uriworkermap.properties
URI mapping file, which specifies which URLs are handled by TOMCAT, you can also 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 so that we do not need to restart the Apache server when we modify the configuration.

Where the second to third profile name can be customized. The following is a typical httpd.conf to JK configuration

123456789101112 # (httpd.conf)# 加载 mod_jk 模块LoadModule jk_module modules/mod_jk.so## Configure mod_jk#JkWorkersFile conf/workers.propertiesJkMountFile conf/uriworkermap.propertiesJkLogFile logs/mod_jk.logJkLogLevel warn

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

12345678910111213141516171819202122232425262728 ## workers.properties## list the workers by nameworker.list=DLOG4J, status# localhost server 1# ------------------------worker.s1.port=8109worker.s1.host=localhostworker.s1.type=ajp13# localhost server 2# ------------------------worker.s2.port=8209worker.s2.host=localhostworker.s2.type=ajp13worker.s2.stopped=1worker.DLOG4J.type=lbworker.retries=3worker.DLOG4J.balanced_workers=s1, s2worker.DLOG4J.sticky_session=1worker.status.type=status

The above workers.properties configuration is the one we used for the screen capture page in front of us. First we configured two workers of type ajp13 to be S1 and S2, which point to the same server running on two different ports 8109 and 8209 Tomcat. Next we configured a worker of type lb (that is, the meaning of load balancing), whose name is dlog4j, a logical worker that manages the two physical connections S1 and S2 that were previously configured. Finally, a worker of the type status is configured, which is the module used to monitor the JK itself. With these three workers is not enough, we also need to tell JK, which worker is available, so there is worker.list = dlog4j, status this line configuration.

Next is the URI mapping configuration, we need to specify which links are handled by Tomcat, and which are processed directly by Apache, see the following file you can understand the meaning of the configuration

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

I'm sure you understand. More than half: All requests are handled by the dlog4j worker, but with a few exceptions, the/jkstatus request is handled by the status worker. What does the exclamation mark in front of each row of data in this configuration mean? The exclamation point indicates that the next URI should not be processed by JK, that is, Apache directly handles 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 requirements of a Web site we have previously proposed. You might as well try it!

Http_proxy

This is the use of Apache's own mod_proxy module to connect Tomcat using proxy technology. Make sure 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 has been rewritten, greatly enhanced its functionality and stability.

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

1234 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 access address of Tomcat, except for images, CSS, JS and several other directories. We can also use Mod_proxy to do load balancing, and then look at the following configuration

12345678910 ProxyPass /images !ProxyPass /css ! ProxyPass /js !ProxyPass / balancer://example/<Proxybalancer://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 be used to monitor the status of the cluster running through a single page, and make some simple maintenance settings.

Figure 2: Monitor cluster run status

Ajp_proxy

Ajp_proxy connection is in fact the same way as http_proxy, are provided by the Mod_proxy function. The same is true for configuration, just replace http://To ajp://, and the port where Tomcat AJP Connector is connected. The configuration of the above example can be changed to:

12345678910 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, of which mod_proxy_ajp.so only in Apache 2.2.x. If you are using Http_proxy, you will need to load mod_proxy.so and mod_proxy_http.so, and if it is ajp_proxy you will need to load the two modules mod_proxy.so and mod_proxy_ajp.so.

Comparison of the three

Relative to the JK connection, the latter two are relatively simple in configuration, flexibility is not inferior. But in terms of stability is not like JK such a proven, after all, Apache 2.2.3 launch of the time is not long, the use of this connection method of the site is not many, so if it is applied to the key Internet site, it is recommended to use JK connection method.

"Reprint" Apache HTTP Server and Tomcat three ways to connect the introduction

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.