Four ways to configure Tomcat reverse proxy + load Balancer

Source: Internet
Author: User
Tags nginx server

Environment topology

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/B3/wKiom1WC-dzR6QWUAAH2zGcZxyY458.jpg "/>

Environment Description:


IP Address
Function description
Nginx or httpd
172.16.4.100
Proxy server, which distributes user requests to the back-end Tomcat server for reverse proxy and load balancing
Tomcat-01
172.16.4.101
The server on which the backend actually provides services
Tomcat-02
172.16.4.102
The server on which the backend actually provides services

The system environment is as follows:

[Email protected] ~]# Cat/etc/redhat-releasecentos release 6.6 (Final) [[email protected] ~]# uname-rm2.6.32-504.el6.x8 6_64 x86_64


This article has a total of four experiments:

1, using nginx Server to implement reverse proxy Tomcat and load balancing;

2. use httpd 's mod_proxy_http module to achieve Tomcat load Balancing;

3. use httpd 's mod_proxy_ajp module to achieve Tomcat load Balancing;

4, The use of httpd mod_jk module, to achieve the service balance Tomcat.


back-end Tomcat configuration

four types of anti-generational + load Balancing are used with this Tomcat deployment.

Tomcat-01Deployment

Installing the JDK

[Email protected] ~]# RPM-IVH jdk-7u67-linux-x64.rpm

Set environment variables for Java

[Email protected] ~]# vim/etc/profile.d/java.shexport java_home=/usr/java/latest #设置家目录export path= $JAVA _home/bin:$ PATH #设置命令路径 [[email protected] ~]#. /etc/profile.d/java.sh

use the-version output Java version number if it can output that means the Java Environment deployment OK

[[email protected] ~]# Java-versionjava version "1.7.0_67" Java (TM) SE Runtime Environment (BUILD1.7.0_67-B01) Java HOTSPO T (TM) 64-bit Server VM (build 24.65-b04,mixed mode)

install Tomcat and add an instance of the application

[[email protected] ~] #tar  xf apache-tomcat-8.0.23.tar.gz -c /usr/local/[[email  protected] ~] #cd  /usr/local/[[email protected] local]# ln -s  Apache-tomcat-8.0.23/tomcat[[email protected] local]# cd tomcat/[[email protected]  tomcat]# mkdir -p/usr/local/tomcat/webapps/testapp/{WEB-INF,classes,lib,META-INF}[[email  Protected] tomcat]# vim/usr/local/tomcat/webapps/testapp/index.jsp<%@ page language= " Java " %>

setting the Tomcat environment variable

[Email protected] tomcat]# Vim/etc/profile.d/tomcat.shexport Catalina_home=/usr/local/tomcatexport PATH= $CATALINA _ Home/bin: $PATH [[email protected] tomcat]#. /etc/profile.d/tomcat.sh

Setup Complete Start Service

[[email protected] tomcat]# catalina.sh start

access to the TestApp application is normal

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/B3/wKiom1WC-dzTJ1AvAAC2EP4IqPY563.jpg "/>

Tomcat-02Deployment

In addition to the test pages provided, the other setup procedures are the same

[[Email protected] tomcat]# vim/usr/local/tomcat/webapps/testapp/index.jsp<%@ page  language= "Java"  %>

Set complete access results as follows

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/B0/wKioL1WC-46xpyRNAADKotPdXz0025.jpg "/>

Nginx Reverse proxy + load balancer Tomcat

Installing Nginx

[[email protected] ~]# yum-y install Nginx

Start the service

[[Email protected] ~]# service Nginx start

access to Nginx can be accessed normally

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/B0/wKioL1WC-5CgtiyEAAJh8Fsoze4138.jpg "/>

in the http field definition upstream, set the back-end Tomcat host and Port, and set the health state check

[Email protected] ~]# vim/etc/nginx/nginx.conf upstreamupservers {server172.16.4.101:8080 max_fails=2 fail_time      Out=1; server172.16.4.102:8080 max_fails=2 fail_timeout=1;}

forwarding Settings forward requests to Nginx to upstream

[Email protected] ~]# vim/etc/nginx/conf.d/default.conf location/{#root/usr/share/nginx/html;       Proxy_pass http://upservers/;    Index index.jsp index.htmlindex.htm; }


once set, the address of the Nginx server is accessed and the request is forwarded to the back-end Tomcat server for load Balancing

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/B3/wKiom1WC-d6BZEy-AADFpq0d9zc858.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/B3/wKiom1WC-d-zRyPuAAC8J7RPmJ0999.jpg "/>

Static and dynamic separation Setup method: Simply forward the JSP and do end files to the back-end Tomcat server

Location ~* \. (Jsp|do) $ {proxy_passhttp://tomcat_host:port;}

Set Nginx After the setup is complete, remember to close nginx Services, so as not to affect the following httpd Test

based onhttpdImplementing a reverse proxy+Load Balancingbased onMod_proxyImplementing a reverse proxy+Load Balancing

Modify httpd Master Profile Logoff Center host

[Email protected] ~]# vim/etc/httpd/conf/httpd.conf#documentroot "/var/www/html"

Set up load balancing related information

[[email protected] ~]# vim /etc/httpd/conf.d/mod_proxy.conf<proxy balancer:// tcsrvs>                    balancermember http://172.16.4.101:8080 loadfactor=1 balancermember http ://172.16.4.102:8080 loadfactor=1</proxy> namevirtualhost *:80 <virtualhost  *:80>  servernamewww.a.com  proxyvia on proxyrequests off  proxypreservehost on  <proxy*>    orderdeny,allow     allowfrom all </proxy>  proxypass/status !  proxypass / balancer://tcsrvs/ proxypassreverse / balancer://tcsrvs/ <location />      Orderdeny,allow     Allowfrom all </Location> </VirtualHost>

The service is started after the setting is complete, and the load balancing effect occurs when the httpd address is accessed

based onAjpprotocol for load Balancing

as long as you modify the protocol for accessing the backend server to AJP , the port is 8009

 [[email protected]~]# vim /etc/httpd/conf.d/mod_ proxy.conf<proxy balancer://tcsrvs> balancermember ajp://172.16.4.101:8009  loadfactor=1 balancermember ajp://172.16.4.102:8009 loadfactor=1</proxy>  namevirtualhost *:80 <virtualhost *:80>  servernamewww.a.com   proxyvia on proxyrequests off proxypreservehost on  <proxy*>     Orderdeny,allow    Allowfrom all </Proxy>   proxypass/status !  proxypass /balancer://tcsrvs/ proxypassreverse /  balancer://tcsrvs/ <location />     orderdeny,allow      allowfrom all </location></virtualhost> 

set to complete restart httpdto achieve load balancing effect

Set Status page

 [[email protected]~]# vim /etc/httpd/conf.d/mod_proxy.conf<proxy balancer:// tcsrvs> balancermember ajp://172.16.4.101:8009 loadfactor=1 balancermember ajp:// 172.16.4.102:8009 loadfactor=1</proxy> namevirtualhost *:80 <virtualhost * :80>  servernamewww.a.com  proxyvia on  proxyrequests off  proxypreservehost on  <proxy*>    orderdeny,allow     allowfrom all </proxy>  proxypass/status !  proxypass/ balancer-manager !  proxypass /balancer://tcsrvs/ proxypassreverse /  balancer://tcsrvs/ <location />     orderdeny,allow      Allowfrom all </Location> <Location /balancer-manager>    sethandler balancer-manager&Nbsp;   proxypass!    orderdeny,allow    allowfrom  all </Location></VirtualHost>

Access Status page can be accessed normally

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/B0/wKioL1WC-5KhCcnNAANJg2d6Hr8331.jpg "/>

based onMod_jkthe Load Balancer

compiling mod_jk- dependent httpd Development Packages

[Email protected] ~]# yum-y install Httpd-devel

compiling and installing MOD_JK

Tar XF tomcat-connectors-1.2.40-src.tar.gzcd tomcat-connectors-1.2.40-src/native/whereis apxs./configure-- With-apxs=/usr/sbin/apxsmake && make Install

Description: If you do not know the path to the Apxs file, you can use Whereis to find

[Email protected] native]# Whereis APXSAPXS:/usr/sbin/apxs/usr/share/man/man8/apxs.8.gz

The following tips are available when compiling

chmod 755/usr/lib64/httpd/modules/mod_jk.so
Configure load Balancing

set up tomcat routing information

[Email protected] ~]# vim/usr/local/tomcat/conf/server.xml <engine name= "Catalina" defaulthost= "localhost" Jvmroute= "Tomcata" >[[email protected] ~]# vim/usr/local/tomcat/conf/server.xml <engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCATB" >

Modify the front-end httpd configuration file, set mod_jk related configuration

[email protected] conf.d]# cat Mod_jk.confloadmodule jk_modulemodules/mod_jk.so jkworkersfile/etc/httpd/conf.d/ Workers.propertiesjklogfilelogs/mod_jk.logjkloglevelinfojkmount/status! Jkmount/*tcsrvs #表示将请求转发到tcsrvs列表JkMount/status StatA

set up workers.propertiesto define load balancing related information

[email protected] conf.d]# cat Workers.propertiesworker.list=tcsrvs,stata #定义tcsrvs列表worker. Tomcatb.host=172.16.4.101worker. Tomcatb.port=8009worker. Tomcatb.type=ajp13worker. Tomcatb.lbfactor=1 #设置权重worker. Tomcata.host=172.16.4.102worker. Tomcata.port=8009worker. Tomcata.type=ajp13worker. Tomcata.lbfactor=1worker. STATA.TYPE=STATUSWORKER.TCSRVS.TYPE=LB #代理方式为lb, i.e. load Balancer WORKER.TCSRVS.BALANCE_WORKERS=TOMCATA,TOMCATB # Description The Tcsrvs list contains, Tomcata, and tomcatbworker.tcsrvs.sticky_session=0 #关闭基于session的绑定, otherwise it will remain anchored to a server if the value is set to 1, or if it is not set to open the bind

Load balancing capability with access testing

Access Status page is also normal


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/B3/wKiom1WC-eHSs7BLAATEVQhhibc354.jpg "/>

This article from "Plum blossom fragrance from bitter cold" blog, please be sure to keep this source http://ximenfeibing.blog.51cto.com/8809812/1663390

Four ways to configure Tomcat reverse proxy + load Balancer

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.