The main function of Tomcat is to provide Servlet/JSP containers. Although it can also be used as an independent Java Web server, it can process static resources (such as HTML files or image files) at a high speed, and the Web server management functions provided are not as good as other professional HTTP servers, such as IIS and Apache servers.
Therefore, Tomcat is often integrated with other HTTP servers in practical applications. For HTTP servers that do not support Servlet/JSP, you can use the Tomcat server to run Servlet/JSP components.
When Tomcat is integrated with other HTTP servers, the working mode of the Tomcat server is usually the Servlet container outside the process. The Tomcat server communicates with other HTTP servers through specialized plug-ins. For more information about the working mode of Tomcat server, see section 1.4.
This chapter first discusses the general principle of Tomcat and HTTP server integration, and then introduces the detailed steps of Tomcat and Apache and IIS integration.
22.1 principle of Tomcat and HTTP server integration
The Tomcat server establishes a connection with the customer program through the Connector component. The Connector component is responsible for receiving customer requests and sending the response results of the Tomcat server to the customer. By default, Tomcat configures two connectors in server. xml:
<! -- Define a non-ssl Coyote HTTP/1.1
Connector on port 8080 -->
<Connection port = "8080"
MaxThreads = "150"
MinSpareThreads = "25"
MaxSpareThreads = "75"
EnableLookups = "false"
RedirectPort = "8443"
AcceptCount = "100"
Debug = "0"
ConnectionTimeout = "20000"
DisableUploadTimeout = "true"/>
<! -- Define a Coyote/JK2 AJP 1.3
Connector on port 8009 -->
<Connection port = "8009"
EnableLookups = "false"
RedirectPort = "8443" debug = "0"
Protocol = "AJP/1.3" type = "codeph" text = "/codeph"/>
The first connector listens to port 8080 and establishes an HTTP connection. This connector is used to access the Web application of the Tomcat server through a browser.
The second connector listens to port 8009 and is responsible for establishing connections with other HTTP servers. This connector is used when Tomcat is integrated with other HTTP servers.
The two methods for Web customers to access JSP components on the Tomcat server are 22-1.
Figure 22-1 two methods for Web customers to access JSP components on the Tomcat server
In Figure 22-1, Web Client 1 directly accesses the JSP component on the Tomcat server. Its access URL is http: // localhost: 8080/index. jsp. Web Client 2 accesses JSP components on the Tomcat server through the HTTP server. Assume that the HTTP port used by the HTTP server is the default port 80, then the URL accessed by Web Client 2 is http: // localhost: 80/index. jsp or http: // localhost/index. jsp.
Next, we will introduce how Tomcat communicates with the HTTP server.
22.1.1 JK plug-in
Tomcat provides a dedicated JK plug-in to communicate with the HTTP server. The JK plug-in should be placed on the HTTP server of the other party. When the HTTP server receives a customer request, it uses the JK plug-in to filter the URL. The JK plug-in determines whether to forward the customer request to the Tomcat server based on the pre-configured URL ing information.
Assume that all "/*. JSP URLs are all processed by the Tomcat server. In Figure 22-1, the JK plug-in forwards client requests to the Tomcat server, and the Tomcat server runs index. JSP, then send the response result to the HTTP server, and then the HTTP server sends the response result to Web Client 2.
Tomcat provides different JK plug-ins for different HTTP servers. This chapter uses the following JK plug-ins:
Integration with Apache HTTP Server in Windows: mod_jk_2.0.46.dll
Integration with Apache HTTP Server in Linux (redhet): mod_jk.so-ap2.0.46-rh72 .. 46-rh72
Integration with IIS server: isapi_redirect.dll
22.1.2 AJP Protocol
AJP is a custom protocol for communication between Tomcat and HTTP servers, providing high communication speed and efficiency. In the integration of Tomcat and HTTP server configuration, you do not have to worry about the details of the AJP protocol. For more information about AJP, see the following link:
Http://jakarta.apache.org/builds/jakarta-tomcat-connectors/jk2/doc/common/AJPv13.html
22.2 integration of Tomcat and Apache server in Windows
Apache HTTP Server is an open-source software provided by the Apache Software organization. It is an excellent professional web server and provides a variety of Web management functions for network administrators, including Directory Index, directory alias, content negotiation, configurable HTTP Error Report, setuid execution of CGI program, sub-process resource management, server-side image ing, URL rewriting, URL spelling check, and online manual.
The Apache HTTP server does not provide Servlet/JSP containers. Therefore, in practical applications, integrating Tomcat with Apache can create a practical and commercial Web platform. In Windows NT/2000, Tomcat and Apache server are integrated. For the software to be prepared, see Table 22-1.
Table 22-1 software to be prepared for Tomcat and Apache server integration in Windows NT/2000
Position of the software on the CD of the book
Apache HTTP Server software http://httpd.apache.org/download. cgi software/apache_2.0.47-win32-x86-no_ssl.msi Based on Windows NT/2000
JK plugin http://jakarta.apache.org/builds/jakarta-tomcat-connectors/jk lib/mod_jk_2.0.46.dll
1. Install the Apache HTTP Server
When you run the apache_2.0.47-win32-x86-no_ssl.msi, you start the installation program for the Apache HTTP Server, as long as you install according to the default settings. If the installation is successful, the Apache HTTP service is automatically added to Windows, as shown in 22-2.
Figure 22-2 add an Apache service to a Windows Service
Assume that the root directory of Apache is, and there is a configuration file httpd. conf under its conf sub-directory. If Apache is installed on the local machine and the default port 80 is used as the HTTP port, the following attributes are displayed in the httpd. conf file:
Listen 80
ServerName localhost: 80
In the "Start"> "program"> "Apache HTTP Server 2.0.47"> "Control Apache Server" menu of the operating system, Restart and Start are provided) and Stop the sub-menu of the Apache server.
Make sure that port 80 is not in use; otherwise, the Apache server cannot be started. After the Apache server is started, you can access the Apache test page to determine whether the installation is successful. Visit http: // localhost. If the webpage shown in 22-3 is displayed, Apache has been installed successfully.
Figure 22-3 Apache server test webpage
2. Add the JK plug-in to Apache
Add the JK plug-in to Apache and copy mod_jk_2.0.46.dll to the/modules directory.
3. Create the workers. properties file.
The workers. properties file is used to configure Tomcat information. Its storage location is/conf/workers. properties. The workers. properties file is provided in the sourcecode/chapter22/windows_apache directory of the related CD files in this book. Its content is as follows ("#" is followed by a comment ):
Workers. tomcat_home = C:/jakarta-tomcat
# Let the mod_jk module know about Tomcat
Workers. java_home = C:/j2sdk1.4.2
# Let the mod_jk module know about j2sdk
Ps =/
# Specify the file path delimiter
Worker. list = worker1
Worker. worker1.port = 8009
# Working port. Do not modify it if it is not occupied
Worker. worker1.host = localhost
# Tomcat server address
Worker. worker1.type = ajp13
# Type
Worker. worker1.lbfactor = 1
# Load Balancing Factor
For the attribute descriptions in the preceding file, see Table 22-2.
Table 22-2 properties of the workers. properties File
Description
Workers. tomcat_home specifies the root directory of the Tomcat server
Workers. java_home specifies the JDK root directory
Worker. list specifies the Tomcat server work list
Worker. worker1.port specifies the JK port used by the Tomcat server
Worker. worker1.host specifies the IP address of the Tomcat server
Worker. worker1.type specifies the communication protocol between the Tomcat server and Apache.
Worker. worker1.lbfactor specifies the Load balancing Factor ). This attribute makes sense only when the Load balancer is used.
4. modify the configuration file httpd. conf of Apache.
Open the/conf/httpd. conf file and add the following content to the end:
# Using mod_jk2.dll
Redirect dynamic callto Tomcat
LoadModule jk_module
Modules/mod_jk_2.0.46.dll
JkWorkersFile
"Conf/workers. properties"
JkLogFile "logs/mod_jk2.log"
JkLogLevel debug
JkMount/*. jsp worker1
JkMount/helloapp/* worker1
The above content is provided in the sourcecode/chapter22/windows_apache/httpd_modify.conf file on the CD supporting this book. It instructs the Apache server to load the JK plug-in and set relevant attributes for the JK plug-in, for descriptions of these attributes, see Table 22-3.
Table 22-3 attributes of the JK plug-in
Description
LoadModule specifies the loaded JK plug-in
JkWorkersFile specifies the job file of the JK plug-in.
JkLogFile specifies the log file used by the JK plug-in. In actual configuration, you can view this log file to track the running process of the JK plug-in. This is useful for troubleshooting errors.
JkLogLevel specifies the Log Level of the JK plug-in. Optional values include debug, info, and error.
JkMount specifies URL ing information processed by JK plug-in
JkMount is used to specify URL ing information, "JkMount /*. jsp worker1 "indicates "/*. jsp URLs are processed by the Tomcat server represented by worker1. JkMount/helloapp/* worker1 indicates that the URLs used to access the helloapp are processed by worker1.
5. Test Configuration
Restart the Tomcat server and Apache server and access http: // localhost/index. jsp through a browser. If the default homepage of Tomcat appears, the configuration is successful. If the helloapp application has been published on the Tomcat server, you can access http: // localhost/helloapp/index.htm. if you return to the index.htm page of The helloappapplication, the configuration is successful.
If the configuration is incorrect, you can view the log information generated by the JK plug-in, which helps you find the cause of the error. Set the log file storage location to/logs/mod_jk2.log in the Apache configuration file httpd. conf.
Load Balancing when Apache is integrated with multiple Tomcat servers
In practical applications, if the website has a high access volume, you can integrate multiple Tomcat servers with Apache to share the task of running Servlet/JSP components. The loadbalancer of the JK plug-in is responsible for allocating workloads to these Tomcat servers based on the pre-configured lbfactor (load balancing factor) in the workers. properties file to achieve load balancing.
Assume that Apache is integrated with two Tomcat servers. One Tomcat server and Apache are running on the same machine. The JK port used is 8009, And the other Tomcat server runs on the other machine, the host name is anotherhost, And the JK port used is 8009. The following describes how to integrate Apache with the two Tomcat servers.
(1) Copy mod_jk_2.0.46.dll to the/lib directory.
(2) create the following workers. properties file in the/conf directory (note the content in bold ):
Ps =/
# Specify the file path delimiter
Worker. list = worker1,
Worker2, loadbalancer
Worker. worker1.port = 8009
# Working port. Do not modify it if it is not occupied
Worker. worker1.host = localhost
# Tomcat server address
Worker. worker1.type = ajp13
# Type
Worker. worker1.lbfactor = 100
# Load Balancing Factor
Worker. worker2.port = 8009
# Working port. Do not modify it if it is not occupied
Worker. worker2.host = anotherhost
# Tomcat server address
Worker. worker2.type = ajp13
# Type
Worker. worker2.lbfactor = 100
# Load Balancing Factor
Worker. loadbalancer. type = lb
Worker. loadbalancer.
Balanced_workers = worker1, worker2
The preceding files create two workers: worker1 and worker2, which represent two Tomcat servers. They are allocated by worker. loadbalancer.
(3) modify the/CONF/httpd. conf file and add the following content to the end of the file:
# Using mod_jk2.dll
Redirect dynamic callto Tomcat
Loadmodule jk_module
Modules/mod_jk_2.0.46.dll
Jkworkersfile "CONF/workers. properties"
Jklogfile "logs/mod_jk2.log"
Jkloglevel debug
Jkmount/*. jsp loadbalancer
Jkmount/helloapp/* loadbalancer
When the customer requests "/*. JSP "or"/helloapp/* "url. This request is forwarded by loadbalancer. the lbfactor attribute assigned to worker1 and worker2 in the properties file to determine how to schedule them.
Only when loadbalancer is used can the lbfactor attribute of worker in the workers. properties file be meaningful. The larger the lbfactor value, the higher the workload assigned to the Tomcat server.
(4) modify the JK ports of the two Tomcat servers to make sure they match the configurations in the workers. properties file. In addition, after loadbalancer is used, the worker name must be consistent with the jvmroute attribute of the element in the Tomcat server. xml file.
Therefore, we should modify the sever. xml files of two Tomcat servers and set the jvmRoute attributes of their elements to worker1 and worker2 respectively. The following are the elements of the modified two Tomcat servers:
Tomcat server 1:
<Engine name = "Catalina"
DefaultHost = "localhost"
Debug = "0" jmvRoute = "worker1">
Tomcat server 2:
<Engine name = "Catalina"
DefaultHost = "localhost"
Debug = "0" jmvRoute = "worker2">
(5) After completing the preceding steps, start two Tomcat servers and Apache servers respectively, and then access http: // localhost/index. jsp. The default homepage of the Tomcat server appears. Because loadbalancer is used to schedule Tomcat servers at this time, you cannot determine which Tomcat server to access index. jsp. This is transparent to Web customers.
If both Tomcat servers run on the same machine during the above experiment, make sure they do not use the same port. The following three ports are configured in Tomcat's default server. xml:
<Server port = "8005"
Shutdown = "SHUTDOWN" debug = "0">
<! -- Define a non-SSL Coyote
HTTP/1.1 Connector on port 8080 -->
<Connector port = "8080"/>
<! -- Define a Coyote/JK2
AJP 1.3 Connector on port 8009 -->
<Connector port = "8009"/>
If both Tomcat servers are running on the same machine, modify at least three port numbers of one Tomcat server.
In addition, if Tomcat is integrated with other HTTP servers, Tomcat is mainly responsible for handling customer requests forwarded by the HTTP server and generally does not directly accept HTTP requests. To improve the running performance of Tomcat, you can disable the HTTP Connector of Tomcat by commenting out the configuration of the HTTP Connector of Tomcat in server. xml.