第二部分 apache 配置(ver 2.2.3)
在Apache 2.2版本之前,一般有mod_jk2和mod_jk两个模块可供选择,mod_jk2模块是比较早的一种连接器,在动、静页面过滤上可以使用正则表达式,因此使用配置灵活。但是mod_jk2模块现在已经没有开发人员支持了,版本更新也就此停止。继承jk2模块的是mod_jk模块,mod_jk模块支持Apache 1.x和2.X系列版本,现在一般都使用mod_jk做Apache和Tomcat的连接器。
在Apache 2.2版本以后,又出现了两种连接器可供选择,就是http-proxy和proxy-ajp模块。Apache的proxy(代理)模块可以实现双向代理,功能非常强大,从连接器的实现原理看,用http-proxy模块实现也是很自然的事情,只需打开Tomcat的http功能,然后用Apache的proxy代理功能将动态请求交给Tomcat处理,而静态数据交给Apache自身就可以了。proxy-ajp模块是专门为Tomcat整合所开发的,通过ajp协议专门代理对Tomcat的请求。根据官方的测试,proxy-ajp的执行效率要比http-proxy高,因此在Apache 2.2以后的版本,用proxy-ajp模块作为Apache和Tomcat的连接器是个不错的选择。
A: Apache 采用ajp方式连接tomcat
1. apache 采用系统默认安装和编译安装都可以,apache 2.2后版本默认安装都支持mod_proxy mod_proxy_ajp mod_proxy_http mod_proxy_balancer mod_proxy_connect等模块,只要确认httpd.conf 配置中未注释即可(默认安装后全部模块支持都已打开,如下:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_connect_module modules/mod_proxy_connect.
2.在/etc/http/conf.d 下新增虚拟主机文件proxy_ajp.conf(有些版本系统自带,内容如下)
<VirtualHost *:80>
ServerAdmin admin@coob.cn
DocumentRoot /home/gov/www/fg_gov/root
DirectoryIndex index.shtml
ServerName node2.12398.gov.cn
ErrorLog logs/gov.error_log
CustomLog logs/dummy-host.example.com-access_log common
#################目录权限配置###############
<Directory "/home/gov/www/fg_gov/root">
Options Indexes FollowSymLinks Includes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
#################代理配置,静态对象由apache处理###############
ProxyRequests Off
ProxyPass /images !
ProxyPass /pic !
ProxyPass /js !
ProxyPass /css !
ProxyPass /html !
ProxyPass /index.shtml !
#################集群配置###############
ProxyPass / balancer://fg_gov/ stickysession=JSESSIONID|jsessionid nofailover=Off
ProxyPassReverse / balancer://fg_gov/
#ProxyPass / balancer://fg_gov/ stickysession=jsessionid nofailover=On
<Proxy balancer://fg_gov>
BalancerMember ajp://127.0.0.1:5109 loadfactor=1 route=s1
BalancerMember ajp://127.0.0.1:6109 loadfactor=1 route=s2
</Proxy>
</VirtualHost>
说明:stickysession=jsessionid nofailover=On(点开每个页面都轮循)
stickysession=JSESSIONID|jsessionid nofailover=Off(关闭浏览器后才轮循)
B: 采用mod_jk方式连接tomcat
1. 在/etc/httpd/conf.d/目录下新增mod_jk.conf workers.properties文件,
[root@node conf]# cat mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile "conf.d/workers.properties"
JkLogLevel info
JKLogFile "logs/mod_jk.log"
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkRequestLogFormat "%w %V %T"
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin admin@coob.cn
DocumentRoot /home/gov/www/fg_gov/root
Alias /login "/home/gov/www/bg_gov/root"
Alias /admin "/home/gov/www/admin_gov/root"
DirectoryIndex index.shtml index.jsp
ServerName node2.12398.gov.cn
ErrorLog logs/gov.error_log
CustomLog logs/dummy-host.example.com-access_log common
#################目录权限配置###############
<Directory "/home/gov/www/fg_gov/root">
Options FollowSymLinks Includes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkMount /jkstatus status
JkMount /* loadbalancer
JkUnmount /*.jpg loadbalancer
JkUnmount /*.css loadbalancer
JkUnMount /*.gif loadbalancer
JkUnMount /*.png loadbalancer
JkUnmount /*.js loadbalancer
JkUnmount /*.html loadbalancer
JkUnmount /*.htm loadbalancer
JkUnmount /*.shtml loadbalancer
JkUnmount /*.swf loadbalancer
JkUnmount /*.flv loadbalancer
JkUnmount /*.xml loadbalancer
JkUnmount /*.ico loadbalancer
</VirtualHost>
说明:JkMount和JkUnmount的写法
JkMount把匹配的转发到指定服务器.
JkUnMount把匹配的不转发到指定服务器.
JkUnMount选项的级别高于JkMount.
单独有JkMount规则有效,但单独有JkUnMount无效,JkUnMount与JkMount要成对出现.
[root@node conf.d]# cat workers.properties
########worker list#################
worker.list=loadbalancer,status
############server名为s1############
worker.s1.port=5109
worker.s1.host=localhost
worker.s1.type=ajp13
worker.s1.lbfactor=50
worker.s1.socket_timeout=300
worker.s1.cache_timeout=750
worker.s1.socket_keepalive=1
worker.s1.redirect=s2
#worker.s1.fail_on_status=-500,-503,404
############server名为s2#############
worker.s2.port=6109
worker.s2.host=localhost
worker.s2.type=ajp13
worker.s2.lbfactor=50
worker.s2.socket_timeout=300
worker.s2.cache_timeout=750
worker.s2.socket_keepalive=1
worker.s2.redirect=s1
#worker.s1.fail_on_status=500,503,-404
############oadbalancer负载均衡########
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=s1,s2
worker.loadbalancer.sticky_session=1
#worker.loadbalancer.sticky_session_force=1
worker.status.type=status
说明:
如果要想改变apache 和tomcat处理的静态元素,不重启apache就生效的话,可以在mod_jk.conf中使用JkWorkFile “conf.d/uriworkermap.properties”参数,这时需在/conf.d下新增uriworkermap.properties文件,就可以把上述文件中的JkMount 和JkUnmout 单独写入这个文件中,写法要用apache 官方文档中的另外一种写法(这是可取消上文中的JkMount JkUnMout部分,文件内容如下
[root@node httpd]# cat /etc/httpd/conf.d/uriworkermap.properties
/*=loadbalancer
ps=/
/jkstatus=status
!/*.gif=loadbalancer
!/*.jpg=loadbalancer
!/*.png=loadbalancer
!/*.css=loadbalancer
!/*.js=loadbalancer
!/*.htm=loadbalancer
!/*.html=loadbalancer
!/*.shtml=loadbalancer
!/*.swf=loadbalancer
!/*.flv=loadbalancer
!/*.xml=loadbalancer
!/*.ico=loadbalancer
Tomcat集群+Apache的实现(3)
内容版权声明:除非注明,否则皆为本站原创文章。