Use Apache and Nginx proxies for Tomcat load balancing and cluster configuration

Source: Internet
Author: User

Lab Environment:

1, nginx agent function

Nginx Proxy:
eth0:192.168.8.48
Vmnet2 eth1:192.168.10.10


Tomcat Server1:
Vmnet2 eth0:192.168.10.20


Tomcat Server2:
Vmnet2 eth0:192.168.10.30




# yum Install-y nginx-1.8.1-1.el6.ngx.x86_64.rpm
# vim/etc/nginx/conf.d/default.conf


Location/{
Root/web/htdocs;
Index index.jsp index.html index.htm;
}


Location ~* \. (jsp|do|action) $ {
Proxy_pass http://192.168.10.20;
}


Location ~* \. (jpg|jpeg|gif|png|pdf|doc|rar|exe|zip|) $ {
Proxy_pass http://192.168.10.30;
}


2, Apache agent function ①Proxy for HTTP mode# cd/etc/httpd/conf.d/
# Vim Mod_proxy.conf
Add the following content:


Proxyrequests off
Proxypreservehost on


proxypass/http://192.168.10.20/
proxypassreverse/http://192.168.l0.20/


<location/>
Order Allow,deny
Allow from all
</Location>


Agent Proxyvia on ②AJP mode
Proxyrequests off
Proxypreservehost on


proxypass/ajp://192.168.10.20/
proxypassreverse/ajp://192.168.l0.20/


<location/>
Order Allow,deny
Allow from all
</Location>


②, configuring Apache MOD_JK-based load balancing

A. Installing the APXS Plugin
# yum Install-y httpd-devel
# Tar XF tomcat-connectors-1.2.37-src.tar.gz
# CD Tomcat-connectors-1.2.37-src/native
#./configure--with-apxs=/usr/sbin/apxs
# Make && make install


Configure related files
# CD/ETC/HTTPD/CONF.D
# Vim Mod_jk.conf


LoadModule Jk_module modules/mod_jk.so
Jkworkersfile/etc/httpd/conf.d/workers.properties
Jklogfile Logs/mod_jk.log
Jkloglevel Notice
Jkmount/* Tomcata
Jkmount/status/stata

# Vim Workers.properties


Worker.list=tomcata,stata
Worker. Tomcata.type=ajp13
Worker. toccata.port=8009
Worker. tomcata.host=192.168.10.20
Worker. Tomcata.lbfactor=1
Worker.stata.type=status


Access the following address to see the status of Tomcat Server1
http://192.168.8.48/status/


③ Configuring the Load Balancer server with MOD_JK

Configuring a proxy Server
# CD/ETC/HTTPD/CONF.D
# Cat Mod_jk.conf
LoadModule Jk_module modules/mod_jk.so
Jkworkersfile/etc/httpd/conf.d/workers.properties
Jklogfile Logs/mod_jk.log
Jkloglevel Notice
Jkmount/* LBCA
Jkmount/status/stata


# Cat Workers.properties
Worker.list=lbca,stata
Worker. Tomcata.type=ajp13
Worker. tomcata.port=8009
Worker. tomcata.host=192.168.10.20
Worker. Tomcata.lbfactor=1
Worker. Tomcatb.type=ajp13
Worker. tomcatb.port=8009
Worker. tomcatb.host=192.168.10.30
Worker. Tomcatb.lbfactor=1
Worker.lbca.type=lb
Worker.lbca.sticky_session=1
Worker.lbca.balance_workers=tomcata,tomcatb
Worker.stata.type=status


Configure back-end Tomcat
# cd/usr/local/tomcat/webapps/
# mkdir TestApp
# CD testapp/
# MKDIR-PV Web-inf/{classes,lib}
mkdir:created directory ' Web-inf '
mkdir:created directory ' web-inf/classes '
mkdir:created directory ' Web-inf/lib '
# Vim Index.jsp


Tomcata Server
INDEX.JSP:
<%@ page language= "java"%>
<body>
<table align= "centre" border= "1" >
<tr>
<td>session id</td>
<% Session.setattribute ("chinasoft.com.com", "chinasoft.com"); %>
<td><%= Session.getid ()%></td>
</tr>
<tr>
<td>created on</td>
<td><%= session.getcreationtime ()%></td>
</tr>
</table>
</body>


TOMCATB Server
INDEX.JSP:
<%@ page language= "java"%>
<body>
<table align= "centre" border= "1" >
<tr>
<td>session id</td>
<% Session.setattribute ("chinasoft.com", "chinasoft.com"); %>
<td><%= Session.getid ()%></td>
</tr>
<tr>
<td>created on</td>
<td><%= session.getcreationtime ()%></td>
</tr>
</table>
</body>


Test is OK
# Curl Http://192.168.10.20:8080/testapp/index.jsp
# Curl Http://192.168.10.30:8080/testapp/index.jsp



Configure session Sessions binding (which can break the load balancing effect):
Load balancer, and implement session binding be aware of a Jvmroute property for each Tomcat instance's Egine container! This name should be consistent with the name of the front-end dispatch module!
In addition, the mod_proxy implements load-balanced session binding, and also uses Sticksession=jsessionid (characters to capitalize)!
Worker.properties:
Worker. Tomcatb.lbfactor=1
Define Jvmroute in the back-end Tomcat server Server.xml file:
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "Tomcata" >


④.apache Mod_proxy implements HTTP-based load Balancing configuration:

# MV Mod_jk.conf Mod_jk.conf.bak
# MV Mod_proxy.conf.bak mod_proxy.conf
# Vim Mod_proxy.conf


Proxyvia on
Proxyrequests off
Proxypreservehost on


<proxy balancer://lb>
Balancermember http://192.168.10.20:8080 loadfactor=1 Route=tomcata
Balancermember http://192.168.10.30:8080 loadfactor=1 ROUTE=TOMCATB
</Proxy>


proxypass/balancer://lb/
proxypassreverse/balancer://lb/


<location/>
Order Allow,deny
Allow from all
</Location>


⑤. Configuring the persistence of Session sessions

Proxyvia on
Proxyrequests off
Proxypreservehost on


<proxy balancer://lb>
Balancermember http://192.168.10.20:8080 loadfactor=1 Route=tomcata
Balancermember http://192.168.10.30:8080 loadfactor=1 ROUTE=TOMCATB
</Proxy>


Proxypass/balancer://lb/stickysession=jsessionid
proxypassreverse/balancer://lb/


<location/>
Order Allow,deny
Allow from all
</Location>



⑥. Configuration Management Interface Proxyvia on
Proxyrequests off
Proxypreservehost on


<proxy balancer://lb>
Balancermember http://192.168.10.20:8080 loadfactor=1 Route=tomcata
Balancermember http://192.168.10.30:8080 loadfactor=1 ROUTE=TOMCATB
</Proxy>


<Location/lbmanager>
SetHandler Balancer-manager
</Location>


Proxypass/lbmanager!
proxypass/balancer://lb/
proxypassreverse/balancer://lb/


<location/>
Order Allow,deny
Allow from all

</Location>


Access: Http://192.168.8.48/lbmanager, you can see the load-balanced state interface



Cluster configuration:

Load balancer: 192.168.10.10:
# Vim Mod_proxy.conf


Proxyvia onproxyrequests offproxypreservehost on<proxy balancer://lb>  balancermember/http 192.168.10.20:8080 loadfactor=1 route=tomcata  balancermember http://192.168.10.30:8080 loadfactor=1 route= tomcatb</proxy><location/lbmanager>  SetHandler Balancer-manager</location>proxypass/ Lbmanager! Proxypass/balancer://lb/proxypassreverse/balancer://lb/<location/>  Order allow,deny allow from all  </Location>


192.168.10.20:


<engine name= "Catalina" defaulthost= "localhost" jvmroute= "tomcata" ><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.1.0.4 "port=" 45564 "frequency=" "Droptime="      "/> <receiver classname=" org.apache.catalina.tribes.transport.nio.NioReceiver "address=" 192.168.10.20 " port= "4000" autobind= "selectortimeout=" maxthreads= "6"/> <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 "/> </Channel> <valve Classname= "Org.apache.catalina.ha.tcp.ReplicationValve" filter= ""/> <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="/t mp/war-listen/"watchenabled=" false "/> <clusterlistener classname=" Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener "/> <clusterlistener classname=" Org.apache.catalina.ha.session.ClusterSessionListener "/></cluster>



192.168.10.30:


<engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCATB" ><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.1.0.4 "port=" 45564 "frequency=" "Droptime="      "/> <receiver classname=" org.apache.catalina.tribes.transport.nio.NioReceiver "address=" 192.168.10.30 " port= "4000" autobind= "selectortimeout=" maxthreads= "6"/><sender classname= "org.apache.c Atalina.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 "/></channel><valve Classname= "Org.apache.catalina.ha.tcp.ReplicationValve" filter= "/><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="/t mp/war-listen/"watchenabled=" false "/><clusterlistener classname=" Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener "/><clusterlistener classname=" Org.apache.catalina.ha.session.ClusterSessionListener "/></cluster>


and add the routing information on each cluster node:
Route add-net 228.1.0.4 netmask 255.255.255.255 Dev eth0


And in:

Add the Web. XML node:

<distributable/>


You can see that the session ID has not changed


Use Apache and Nginx proxies for Tomcat load balancing and cluster configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.