tomcat和apache连接器mod_jk (Tomcat Connectors)

时间:2021-12-10 14:03:21
需要做好的准备工作为:安装好apache,j2sdk, jakarta-tomcat
 
目的就是做一个可以连接几台(jsp和servlet)的解析器:
 
mod_jk下载地址
tar zxf jakarta-tomcat-connectors-1.2.15-src.tar.gz
cd jakarta-tomcat-connectors-1.2.15-src/jk/native
./configure  --with-apxs=/usr/local/apache/bin/apxs
make
make install

(动态的安装了mod_jk,安装程序作了:
cp .libs/mod_jk.so /usr/local/apache/modules/mod_jk.so
cp .libs/mod_jk.lai /usr/local/apache/modules/mod_jk.la
cp .libs/mod_jk.a /usr/local/apache/modules/mod_jk.a
)

Apache related parameters :

Parameter Description
--with-apxs[=FILE]

FILE is the location of the apxs tool. Default is finding apxs in PATH. It builds a shared Apache module. It detects automaticly the Apache version.

--with-apache=DIR

DIR is the path where apache sources are located. The apache sources should have been configured before configuring mod_jk. DIR is something like: /home/apache/apache_1.3.33 It builds a static Apache module.

--enable-maitainer-mode

Build mod_jk enabling maintainer or DEBUG mode.

--enable-prefork

Build mod_jk without all the pthread code, better support apache prefork mode.

JNI related parameters :

Parameter Description
--enable-jni

Build the JNI worker and so the build process will require some informations about your Java Environment. Most of the options are autodetected.

--with-java-home=DIR

DIR is the patch to the JDK root directory. Something like: /opt/java/jdk12

--with-os-type=SUBDIR

SUBDIR is the os-type subdirectory, configure should guess it correctly.

--with-arch-type=SUBDIR

SUBDIR is the arch subdirectory, configure should guess it correctly.

--with-java-platform=VAL

VAL is the Java platform 1 is 1.1.x and 2 is for 1.2 anf higher, configure should guess it correctly.

Examples of configure use :

?/P>

Apache 2.x build
[user@host] ~ $ ./configure
--with-apxs=/usr/sbin/apxs

[user@host] ~ $ make
[user@host] ~ $ su -c 'make install'

?/P>

Apache 2.x build with JNI support
[user@host] ~ $ ./configure
--with-apxs=/opt/apache2/bin/apxs \

--with-java-home=${JAVA_HOME} --with-java-platform=2 \
--enable-jni

 
下面开始配置apache的httpd.conf和Workers.properties(源码中的conf文件夹中有例子)

 
1.在http.conf中配置:
Module Name: mod_jk.c
Content handlers:  yes
Configuration Phase Participation:  Create Server ConfigMerge Server Configs
Request Phase Participation:  Translate Path
Module Directives:

JkWorkersFile - the name of a worker file for the Jakarta servlet containers

JkMountFile - the name of a mount file for the Jakarta servlet uri mapping

JkAutoMount - automatic mount points to a Tomcat worker

JkMount - A mount point from a context to a Tomcat worker

JkUnMount - A no mount point from a context to a Tomcat worker

JkMountCopy - Should the base server mounts be copied to the virtual server

JkLogFile - Full path to the Jakarta Tomcat module log file

JkShmFile - Full path to the Jakarta Tomcat module shared memory file

JkShmSize - Size of the shared memory file in KBytes

JkLogLevel - The Jakarta Tomcat module log level, can be debug, info, error or emerg

JkLogStampFormat - The Jakarta Tomcat module log format, follow strftime synthax

JkRequestLogFormat - The Jakarta mod_jk module request log format string

JkAutoAlias - The Jakarta mod_jk module automatic context apache alias directory

JkHTTPSIndicator - Name of the Apache environment that contains SSL indication

JkCERTSIndicator - Name of the Apache environment that contains SSL client certificates

JkCIPHERIndicator - Name of the Apache environment that contains SSL client cipher

JkSESSIONIndicator - Name of the Apache environment that contains SSL session

JkKEYSIZEIndicator - Name of the Apache environment that contains SSL key size in use

JkExtractSSL - Turns on SSL processing and information gathering by mod_jk

JkOptions - Set one of more options to configure the mod_jk module

JkEnvVar - Adds a name of environment variable that should be sent to servlet-engine

JkWorkerProperty - Set workers.properties formated directive

Current Configuration:
配置如下:
#加载mod_jk
LoadModule jk_module modules/mod_jk.so

#配置
<IfModule mod_jk.c>
JkWorkersFile conf/workers.properties  #设置mod_jk的配置
JkLogFile logs/mod_jk.log              #设置日志位置
JkLogLevel info                        #设置日志级别
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
</IfModule>

#文件类型匹配
<IfModule mod_jk.c>
JkMount /*.jsp loadbalancer #将所有的jsp请求交给loadbalancer[mod_jk的负JkMount /*.do loadbalancer  #载均衡器]
JkMount /servlet/* loadbalancer
JkMount /application/* loadbalancer
</IfModule>

#设置默认索引页
DirectoryIndex index.jsp
-------------下面是对虚拟主机的配置:
在apache的虚拟主机配置中,将一些需要指定的http请求交给Tomcat处理,加入如下配置:
<VirtualHost *:80>
JkMount /*.jsp loadbalancer #将所有的jsp请求交给loadbalancer[mod_jk的负载均衡器]
<Directory “/path/to/your/web/root/”>
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
# 防止http TRACE 跨站攻击
</Directory>
<Location "/WEB-INF/">
    AllowOverride None
    deny from all
</Location>
#保护你的WEB-INF文件夹
</VirtualHost> 
 
 
2.Workers.properties文件的配置,配置两个worker达到均衡负载:
 
建立文本文件workers.properties,放在apache的conf文件夹下。
文件内容如下:
http://tomcat.apache.org/connectors-doc/config/workers.html

The generic workers.properties directive is in the form:

worker.<worker name>.<directive>=<value>

# workers.properties
#
 
# In Unix, we use forward slashes:
ps=\
 
# list the workers by name
 
worker.list=tomcat1, tomcat2, loadbalancer
 
# ------------------------
# First tomcat server
# ------------------------
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
 
# Specify the size of the open connection cache.
#worker.tomcat1.cachesize
 
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat1.lbfactor=100
 
 
# ------------------------
# Second tomcat server
# ------------------------
worker.tomcat2.port=8009
worker.tomcat2.host=192.168.1.112
worker.tomcat2.type=ajp13
 
# Specify the size of the open connection cache.
#worker.tomcat2.cachesize
 
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.tomcat2.lbfactor=100
 
 
# ------------------------
# Load Balancer worker
# ------------------------
 
#
# The loadbalancer (type lb) worker performs weighted round-robin
# load balancing with sticky sessions.
# Note:
# ----> If a worker dies, the load balancer will check its state
#         once in a while. Until then all work is redirected to peer
#         worker.
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat1, tomcat2
 
#
# END workers.properties
#

Mandatory directives are the one that each worker must contain. Without them the worker will be unavailable or will misbehave. 
 

Directive Default Description
type

ajp13  

Apache JServ Protocol version 1.3 (hereafterajp13

ajp14

lb

jni

lb

jni).

Type of the worker (can be one of ajp13, ajp14, jni, lb or status). The type of the worker defines the directives that can be applied to the worker.

AJP13 worker is the preferred worker type that JK uses for communication between web server and Tomcat. This type of worker uses sockets as communication channel. For detailed description of the AJP13 protocol stack browse to AJPv13 protocol specification  http://tomcat.apache.org/connectors-doc/common/ajpv13a.html

Works.properties中定义了tomcat1和tomcat2两个tomcat,要在tomcat的server.xml中分别做如下修改:
<Engine name="Catalina" defaultHost="localhost" debug="0"  jvmRoute="tomcat1">
用jvmRoute指定和works.properties中对应的名称。

下面是对server.xml的说明:
Each workers.properties directive consists of three words separated by dot. The first word is always worker. The second word is the worker name that can be any name. The worker name reflects the name of the jvmRoute defined in Tomcat's server.xml configuration file.

The name of the worker can contain only the alphanumeric characters [a-z][A-Z][0-9] and is case insensitive.

在/usr/java/jakarta-tomcat/webapps/examples下面建test.jsp
文件,测试服务是否正常启动
Worker1 is index.jsp
<html>
<body bgcolor="red">
Session ID:<%=request.getSession().getId()%>
</body>
</html>

Worker2 is index.jsp
</html>
<html>
<body bgcolor="blue">
Session ID:<%=request.getSession().getId()%>
</body>
</html>
http://blog.sina.com.cn/s/blog_47292007010003le.html