首先虽然这个mod_jk已经过时,但还是放出来大家一起学习一下,文章主要分三部分内容:
1.第一部分:说明主要配置过程
2.第二部分:贴出我的httpd.conf文件
3.第三部分:对mod_jk代码讲解(来源百度)
第一部分:配置
1. 准备环境:
操作系统:windows7
httpd-2.2.21-win32-x86-no_ssl.msi
apache-tomcat-6.0.7
apache-tomcat-5.0.7
tomcat-connectors-1.2.32-windows-i386-httpd-2.2.x
jdk1.5
2. 下载APACHE
这里下载的是APACHE2.2.21版本
3. 下载JK(Tomcat Connector)
Jk是apache和tomcat的连接器,也可以做负载均衡器,主要是apache通过jk找到tomcat。
下载地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/
Jk的版本要与apache的版本对应如:mod_jk-1.2.32-httpd-2.2.x.zip其匹配的Apache为2.2.版本以上可用
4. 下载TOMCAT
apache-tomcat-6.0.7.rar
5. 配置修改过程
1) Apache配置
将Tomcat Connector文件mod_jk.so拷贝到Apache安装目录的modules目录下。
在Apache安装目录下找到conf/httpd.conf文件,使用文本编辑器打开:
伪静态修改
将注释放开
找到AllowOverride None 将其修改为 All 内容如下:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
Tomcat Connector关联项增加
在LoadModules末尾处增加一下内容:
#加载mod_jk连接
LoadModule jk_module modules/mod_jk.so
### 配置 mod_jk
JkWorkersFile "conf\workers.properties" #加载集群中的workers
#此处是指定分配给tomcat的请求 例如*.do *.jsp
JkMount /*.jsp controller
JkLogFile logs/mod_jk.log #指定jk的日志输出文件
JkLogLevel warn #指定日志级别
找到IfModule dir_module 修改默认访问地址,需要根据具体项目实际情况填写,我这里使用的是index.jsp 修改如下:
<IfModule dir_module>
DirectoryIndex index.jsp
</IfModule>
找到Virtual hosts 去掉虚拟主机注释
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf 将 “#”去掉
Include conf/extra/httpd-vhosts.conf
加载代理(暂时未去掉)
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
2) Tomcat Connector配置
在Apache配置目录Apache2.2\conf创建workers.properties配置文件,该文件主要用于配置Apache与Tomcat的集成要用到的Tomcat实例和负载均衡分发控制器。
Workers.properties文件放置一下内容;
#下面是分发控制器 注意不要放tomcat实例
worker.list=lbcontroller
#Tomcat1实例配置 这里要和Tomcat配置文件Service.xml的jvmRoute保持一致
worker.test1.host=localhost
worker.test1.port=8009
worker.test1.type=ajp13
#分发权重 值越大负载越大
worker.tomcat1.lbfactor=1
#Tomcat2实例配置
worker.test2.host=localhost
worker.test2.port=9009
worker.test2.type=ajp13
#分发权重 值越大负载越大
worker.tomcat2.lbfactor=1
#负载均衡分发控制器
worker.lbcontroller.type=lb
worker.lbcontroller.balance_workers=test1,test2
Tomcat配置文件Service.xml主要注意两个地方,一个是Engine节点需要增加节点标识jvmRoute,一个是将原本注释掉的Session复制节点改为有效。具体如下:
<!--jvmRoute在各个Tomcat配置中不能重复且要与worker.properties文件中的名称一致-->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="test1">
<!--session复制内容-->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<!-- timeout="60000"-->
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<!--过滤的文件-->
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
<!--session复制内容-->
<!—Host节点增加一下内容表示站点根路径-->
<Context path="/sc" docBase="." privileged="true"/>
我们分别将两个Tomcat配置文件中的jvmRoute设置为tomcat1、tomcat2,Server节点 端口分别配置为8005和9005, Connector节点端口分别配置为8080和9090,AJPConnector端口分别配置为8009和9009,Connector端口配置参照单主机多站点场景。请注意两个Tomcat配置文件Host节点的域名配置必须一样,Server.xml中的jvmRoute名称必须和worker.properties中的tomcat实例名称一致,不然无法实现session_stricky。
*****************************************************************************
如果需要实现session 复制 需要在Tomcat 下conf/web.xml 中加上<distributable/>
<?xml version="1.0" encoding="ISO-8859-1"?>
省略N多代码。。。。。。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<distributable/>
</web-app>
*****************************************************************************
第二部分:httpd.conf文件
#
# This is the main Apache HTTP server configuration file. It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.2> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server's control files begin with "/" (or "drive:/" for Win32), the
# server will use that explicit path. If the filenames do *not* begin
# with "/", the value of ServerRoot is prepended -- so "logs/foo.log"
# with ServerRoot set to "D:/Program Files/Apache Software Foundation/Apache2.2" will be interpreted by the
# server as "D:/Program Files/Apache Software Foundation/Apache2.2/logs/foo.log".
#
# NOTE: Where filenames are specified, you must use forward slashes
# instead of backslashes (e.g., "c:/apache" instead of "c:\apache").
# If a drive letter is omitted, the drive on which httpd.exe is located
# will be used by default. It is recommended that you always supply
# an explicit drive letter in absolute paths to avoid confusion.
#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path. If you point
# ServerRoot at a non-local disk, be sure to point the LockFile directive
# at a local disk. If you wish to share the same ServerRoot for multiple
# httpd daemons, you will need to change at least LockFile and PidFile.
#
ServerRoot "D:/Program Files/Apache Software Foundation/Apache2.2"
#
# Listen: Allows you to bind Apache to specific IP addresses and/o