Nginx does front-end reverse load balancing, backend Httpd+tomcat

Source: Internet
Author: User

Experimental content: Using Nginx to do front-end reverse load balancing backend Httpd+tomcat


Experimental environment: Physical machine Win7, virtual machine centos7;

node1:172.18.11.111 Httpd+tomcat

node2:172.18.11.112 Httpd+tomcat

node3:172.18.11.113 Nginx Reverse Load Balancing


Description: HTTPd has two ways of communicating with Tomcat;

(1) httpd can use the HTTP module to reverse tomcat, when Tomcat uses the HTTP linker;

(2) httpd can also use the AJP module to reverse Tomcat, at which time Tomcat uses the AJP linker;

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/C1/wKiom1df9KKxa2-kAABeLevrCLc804.png "title=" 9.png " alt= "Wkiom1df9kkxa2-kaabelevrclc804.png"/>

On the NODE3:

Installing Nginx

]# yum-y Install nginx-1.8.0-1.el7.ngx.x86_64.rpm

]# vim/etc/nginx/conf.d/default.conf

In the Server configuration section, add:

Location/{

root/usr/share/nginx/html;

Index index.html index.htm;

Proxy_pass Http://websrvs;

}


]# vim/etc/nginx/nginx.conf

In the HTTP configuration section, add:

Upstream Websrvs {

Server 172.18.11.111:80 weight=1;

Server 172.18.11.112:80 weight=2;

}


On Node1 and Node2, respectively: Installing HTTPD and Tomcat

To install the Java Runtime Environment:

]# yum-y Install JAVA-1.7.0-OPENJDK java-1.7.0-openjdk-devel

]# vim/etc/profile.d/java.sh

Export JAVA_HOME=/USR


]# . /etc/profile.d/java.sh


To install Tomcat:

]# Yum install tomcat tomcat-lib Tomcat-webapps Tomcat-admin-webapps


Create an App Store directory in your web working directory;

]# cd/var/lib/tomcat/webapps/

]# mkdir TestApp

]# mkdir classes Lib Web-inf meta-inf

]# vim/var/lib/tomcat/webapps/testapp/index.jsp

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/C2/wKiom1df-g_xtldtAAAuY0ofLk4067.png "title=" 10.png "alt=" Wkiom1df-g_xtldtaaauy0oflk4067.png "/>

]# Vim/etc/tomcat/server.xml

In the Engine configuration section add:

<context path= "/test" docbase= "TestApp"/>


]# systemctl Start Tomcat.service


Copy all the created directory files to Node2, and then make the configuration file changes:

]# Scp-r/var/lib/tomcat/webapps/testapp/node2:/var/lib/tomcat/webapps/

]# Scp/etc/tomcat/server.xml node2:/etc/tomcat/


On the Node2:

]# vim/var/lib/tomcat/webapps/testapp/index.jsp

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/C2/wKiom1df-pCzqMTcAAA2Dt0cSX0340.png "title=" 11.png "alt=" Wkiom1df-pczqmtcaaa2dt0csx0340.png "/>

]# Systemctl Start Tomcat


After installing Tomcat, install httpd on Node1 and Node2 and edit the configuration file:

]# yum-y Install httpd

]# vim/etc/httpd/conf/httpd.conf

#DocumentRoot "/var/www/html"


]# vim/etc/httpd/conf.d/proxy_http_tomcat.conf

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/C2/wKiom1df_k_TDw3HAAAwe0Ppc8k484.png "title=" 12.png "alt=" Wkiom1df_k_tdw3haaawe0ppc8k484.png "/>

Because Node1 and Node2 are all httpd local anti-tomcat, all configuration files are identical;


This time using the Proxy_http_module anti-generation module to communicate with Tomcat based on the HTTP protocol:

]# httpd-m

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/82/C2/wKiom1dgBJDyEiHfAAAqpC92lOk177.png "title=" 13.png "alt=" Wkiom1dgbjdyeihfaaaqpc92lok177.png "/>

]# systemctl Start httpd


Browser test, input http://172.18.11.113/testapp/index.jsp

After multiple refreshes, the load balance is achieved and the ratio is 1:2, and the front-end nginx back-end Tomcat is implemented.


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/82/C2/wKiom1dgBSuDKcpIAAA6rbXUPak762.png "style=" float: none; "title=" 14.png "alt=" Wkiom1dgbsudkcpiaaa6rbxupak762.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/C0/wKioL1dgBj7TynFsAAA1SMVh4_Q294.png "style=" float: none; "title=" 15.png "alt=" Wkiol1dgbj7tynfsaaa1smvh4_q294.png "/>


If you want to do the session stickiness, you can add an instruction to the upstream configuration segment on Nginx:

Upstream Websrvs {

Server 172.18.11.111:80 weight=1;

Server 172.18.11.112:80 weight=2;

Ip_hash;

}

Description: Ip_hash is session-bound based on the source IP.


Browser test, input http://172.18.11.113/testapp/index.jsp

After multiple refreshes, it is bound to a source IP.


HTTPD can also use the AJP protocol to reverse Tomcat, the benefit of which is to prevent users from using the cross-httpd to access Tomcat, because if the HTTP protocol is used, it is possible for the user to directly access the back-end tomcat across the httpd.

Its configuration is also very simple, according to the above configuration slightly modified can:


Just modify the configuration files on the Node1 and Node2:

]# vim/etc/httpd/conf.d/proxy_http_tomcat.conf

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/82/C0/wKioL1dgCRyjTb_HAABR88kO03M525.png "title=" 16.png "alt=" Wkiol1dgcryjtb_haabr88ko03m525.png "/>

Everything else is the same.

To view httpd loaded modules:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/82/C2/wKiom1dgCGDwRIFBAAAqqv2jOws219.png "title=" 17.png "alt=" Wkiom1dgcgdwrifbaaaqqv2jows219.png "/>

The above process is a simple implementation of nginx as a reverse load balancing to the back-end Tomcat server configuration experiment.



Nginx does front-end reverse load balancing, backend Httpd+tomcat

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.