linux

tomcat反向代理-mod_jk、mod_proxy

发布时间:9年前热度: 4802 ℃评论数:

配置apache通过mod_proxy模块与Tomcat连接: 
                    注:apache2.2之前版本(mod_jk:ajp) apache2.4之后版本(mod_proxy:ajp、http、https) 
1、检测当前的apache是否支持mod_proxy、mod_proxy_http、mod_proxy_ajp和proxy_balancer_module模块: 
# httpd -D DUMP_MODULES 
…………………… 
proxy_module (shared) 
proxy_balancer_module (shared) 
proxy_ftp_module (shared) 
proxy_http_module (shared) 
proxy_connect_module (shared) 
…………………… 

2、在httpd.conf的全局配置段或虚拟主机中添加如下内容: 
1) apache2.4 
ProxyVia On #用于控制在http首部是否使用Via,主要用于在多级代理中的控制代理请求流向。 

ProxyRequests Off #正向代理 
ProxyPreserveHost On #代理请求报文中的host,发送给后端服务,需要支持虚拟主机时开启。 
<Proxy *> 
Require all granted   
</Proxy> 
ProxyPass  /  ajp://192.168.10.10:8009/ #反向代理 
ProxyPassReverse  /  ajp://192.168.10.10:8009/ #反向代理时依然作原有映射 
<Location / > 
 Require all granted 
</Location> 
2) apache2.2 
ProxyVia On 
ProxyRequests Off 
ProxyPreserveHost On 
<Proxy *> 
 Order deny,allow 
 Allow from all 
</Proxy> 
ProxyPass  / ajp://192.168.10.10:8009/ 
ProxyPassReverse  /  ajp://192.168.10.10:8009/ 
<Location / > 
 Order allow,deny 
 Allow from all 
</Location> 

3)LNMP: nginx+tomcat(http,https) 
http { 
upstream tomcat{ 
  server 192.168.10.9:8080; 
  server 192.168.10.10:8080; 
    } 
location ~* \.(jsp|do)$ { proxy_pass http://tomcat; 
   }         
   } 
配置apache和tomcat一起工作: 

1、编译安装apache: 
# ./configure --prefix=/usr/local/apache --enable-proxy --enable-proxy-http --enable-proxy-ajp
2、配置apache加载相应的模块: 
LoadModule proxy_module modules/mod_proxy.so 
LoadModule proxy_http_module modules/mod_proxy_http.so 
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so 

3、配置apache代理tomcat: 

第一种方法,基于proxy-http: 
ProxyPass /   http://192.168.10.10:8080/ 
ProxyPassReverse /  http://192.168.10.10:8080/ 
ProxyVia On 

第二种方法,基于proxy-ajp: 
ProxyPass /  ajp://192.168.10.10:8009/ 
ProxyPassReverse /  ajp://192.168.10.10:8009/ 
ProxyVia On 

4、确保tomcat配置有如上使用的连接器; 
tar xf httpd-2.4.2 
cd httpd-2.4.2 
./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util  --enable-proxy --enable-proxy-http --enable-proxy-ajp 
make && make install 

tar xf tomcat-connectors-1.2.41-src.tar.gz 
cd tomcat-connectors-1.2.41-src/native/ 
./configure --with-apxs=/usr/local/apache/bin/apxs  #模块钩子路径 
make && make install 
apache要使用mod_jk连接器,需要在启动时加载此连接器模块。为了方便管理与mod_jk模块相关配置,这里使用一个专门的配置文件/etc/httpd/conf.d/httpd-jk.conf来保存相关指令及其设置。其内容如下: 
1)mod_jk单机 
# vim /etc/httpd/conf.d/httpd-jk.conf 
LoadModule jk_module modules/mod_jk.so 
JkWorkersFile /etc/httpd/conf.d/workers.properties #指定线程,默认不存在。 
JkLogFile logs/mod_jk.log 
JkLogLevel debug 
JkMount /* TomcatA #所有代理都转发给TomcatA 
JkMount /status/ stat1 
# vim /etc/httpd/conf.d/workers.properties 
worker.list=TomcatA,statA 
worker.TomcatA.type=ajp13 
worker.TomcatA.port=8009 
worker.TomcatA.host=192.168.10.10 
worker.TomcatA.lbfactor=1 
worker.statA.type = status 
2) mod_jk实现负载均衡
 
# vim /etc/httpd/conf.d/httpd-jk.conf 
LoadModule jk_module modules/mod_jk.so 
JkWorkersFile /etc/httpd/conf.d/workers.properties 
JkLogFile logs/mod_jk.log 
JkLogLevel debug 
JkMount /* lbcluster1 
JkMount /jkstatus/ statA 
# vim /etc/httpd/conf.d/workers.properties 
worker.list=lbcluster1,stat1 
worker.TomcatA.type=ajp13 
worker.TomcatA.port=8009 
worker.TomcatA.host=192.168.10.9 
worker.TomcatA.lbfactor=1
 
worker.TomcatB.type=ajp13 
worker.TomcatB.port=8009 
worker.TomcatB.host=192.168.10.10 
worker.TomcatB.lbfactor=1
 
worker.lbcluster1.type=lb 
worker.lbcluster1.sticky_session=1 
worker.lbcluster1.balance_workers=TomcatA,TomcatB 
worker.statA.type = status
 
[ root@TomcatA $ ] vim /usr/local/tomcat/conf/vim server.xml 
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA"> 
[ root@TomcatB $ ] vim 
/usr/local/tomcat/conf/vim server.xml               
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB"> 
3)基于mod_proxy实现负载均衡 
在httpd.conf的全局配置中配置如下内容: 
ProxyVia On 
ProxyRequests Off 
ProxyPreserveHost On 
<proxy balancer://lbcluster1> 
BalancerMember ajp://192.168.10.9:8009 loadfactor=1 route=TomcatA 
BalancerMember ajp://192.168.10.10:8009 loadfactor=1 route=TomcatB 
ProxySet lbmethod=bytraffic #负载均衡方法 bytraffic:流量计数 byrequests:轮询 bybusyness 当前负载 
</proxy> 
<Location /lbmanager> 
SetHandler balancer-manager #状态页面 
</Location> 
ProxyPass /lbmanager ! #不向后代理 
ProxyPass  /  balancer://lbcluster1/ stickysession=JSESSIONID 
ProxyPassReverse  /  balancer://lbcluster1/ 
<Location / > 
 Require all granted 
</Location> 
[ root@TomcatA $ ] vim /usr/local/tomcat/conf/vim server.xml <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA"> 
[ root@TomcatB $ ] vim 
/usr/local/tomcat/conf/vim server.xml               
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB"> 
基于连接器提供Tomcat性能的方法: 
1) 设置tcpNoDelay属性值为“true”; 
2) 通过maxKeepAliveRequest属性调整允许keep-alive功能的请求的最大数目,值为1时表示禁用; 
3) 调整socketBuffer属性的值以改变套接字缓冲的大小; 
4) 将enableLookups设置为false以禁用DNS反解; 
5) Tomcat是一个多线程的Servlet容器,使用线程池能对服务器性能带去很大影响;这主要通过maxThreads、maxSpareThreads和minSpareThreads来定义; 
6) 通过JAVA_OPTS,如-Xms和-Xmx设定JVM相关的参数以定义其使用内存的能力;

tomcat,mod_jk,mod_proxy

手机扫码访问