参考资料: 通常,将Apache与Tomcat整合主要出于以下几个原因: 而关于Apache与Tomcat整合,有3种方式可以实现: 2. http_proxy 3. ajp_proxy 下面是具体的配置过程: 1. 安装配置Apache # tar xjvf httpd-2.2.18.tar.bz2 # cd httpd-2.2.18 # make 创建apache用户 修改apache配置文件,让它以用户apache身份运行 2. 通过 Apache mod_jk 方式实现与Tomcat整合 # cp ./apache-2.0/mod_jk.so /opt/apache2/modules/ # cd /opt/apache2/conf/ # Load the jk module LoadModule jk_module modules/mod_jk.so # The location of workers.properties JkWorkersFile conf/extra/httpd-jk-workers.properties # The location of jk logs JkLogFile logs/httpd-jk.log # The location of jk mount file JkMountFile conf/extra/httpd-jk-uriworkermap.properties # The jk log level [debug/error/info] JkLogLevel info # The log format JkLogStampFormat "[%a %b %d %H:%M:%S %Y]" # JkOptions indicate to send SSL KEY SIZE, JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories # JkRequestLogFormat set the request format JkRequestLogFormat "%w %V %T" 创建 mod_jk 集群参数配置文件 # list the workers by name worker.list=DLOG4J, status #创建名为DLOG4J与status的worker # localhost server 1 # ------------------------ worker.s1.port=8009 #后端Tomcat服务器的默认AJP端口 worker.s1.host=192.168.203.133 #后端Tomcat服务器的IP worker.s1.type=ajp13 #采用AJP协议进行通信 # localhost server 2 # ------------------------ worker.s2.port=8009 #后端Tomcat服务器的默认AJP端口 worker.s2.host=192.168.203.135 #后端Tomcat服务器的IP worker.s2.type=ajp13 #采用AJP协议进行通信 #worker.s2.stopped=1 #是否暂停该服务器的开关 worker.DLOG4J.type=lb #采用负载均衡的方式 worker.retries=3 worker.DLOG4J.balanced_workers=s1, s2 #负载均衡中包括server 1与server 2两台Tomcat服务器 worker.DLOG4J.sticky_session=1 worker.status.type=status #在worker status中对集群状态进行监控 创建 mod_jk URL参数文件 /*=DLOG4J #所有的请求都交给DLOG4J处理 /jkstatus=status #在/jkstatus页面中监控集群状态 !/*.gif=DLOG4J #过滤gif !/*.jpg=DLOG4J #过滤jpg !/*.png=DLOG4J #过滤png !/*.css=DLOG4J #过滤css !/*.js=DLOG4J #过滤js !/*.htm=DLOG4J #过滤htm !/*.html=DLOG4J #过滤html 修改Apache主配置文件,使其关联mod_jk主配置文件 # The JK connector settings Include conf/extra/httpd-jk.conf 启动Apache 通过统计监听页面,可以看到后端服务器的状态,如下图所示: 通过浏览器访问http://192.168.203.134/index.jsp,可以发现,在多次刷新之后,请求会随机分配到后端的Tomcat服务器上。 2. 通过 Apache mod_proxy 方式实现与Tomcat整合 添加完成后,在httpd.conf中可以看到以下配置: # cd /opt/apache2/conf/ 创建 mod_proxy 主配置文件 ProxyPassMatch /*.gif$ ! #过滤gif ProxyPassMatch /*.jpg$ ! #过滤jpg ProxyPassMatch /*.png$ ! #过滤png ProxyPassMatch /*.css$ ! #过滤css ProxyPassMatch /*.js$ ! #过滤js ProxyPassMatch /*.htm$ ! #过滤htm ProxyPassMatch /*.html$ ! #过滤html ProxyPass /server-status ! #过滤server-stauts监控页面 ProxyPass /balancer-manager ! #过滤balancer-manager监控页面 ProxyPass / balancer://cluster/ #创建集群cluster <Proxy balancer://cluster/> BalancerMember http://192.168.203.133:8080/ #设置cluster成员 BalancerMember http://192.168.203.135:8080/ #设置cluster成员 </Proxy> <Location /server-status> #设置server-stauts监控页面 SetHandler server-status Order Deny,Allow Deny from all Allow from all </Location> <Location /balancer-manager> #设置balancer-manager监控页面 SetHandler balancer-manager Order Deny,Allow Deny from all Allow from all </Location> 修改Apache主配置文件,使其关联mod_proxy主配置文件 # The Proxy settings Include conf/extra/httpd-proxy.conf 启动Apache 通过监控页面,可以看到后端服务器的状态,如下图所示: 通过浏览器访问http://192.168.203.134/index.jsp,可以发现,在多次刷新之后,请求会随机分配到后端的Tomcat服务器上。 3. 通过 Apache mod_proxy_ajp 方式实现与Tomcat整合 ... ProxyPass / balancer://cluster/ <Proxy balancer://cluster/> BalancerMember ajp://192.168.203.133:8009/ BalancerMember ajp://192.168.203.135:8009/ </Proxy> ... 4. 其它 |
|