{"id":7247,"date":"2018-03-08T00:32:40","date_gmt":"2018-03-07T16:32:40","guid":{"rendered":"http:\/\/rmohan.com\/?p=7247"},"modified":"2018-03-08T00:32:40","modified_gmt":"2018-03-07T16:32:40","slug":"tomcat-load-balancing-using-nginx-reverse-proxies","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7247","title":{"rendered":"Tomcat load balancing using Nginx reverse proxies."},"content":{"rendered":"<p><strong>This essay focuses on Tomcat clusters and Tomcat load balancing using Nginx reverse proxies.<\/strong><\/p>\n<p>First, we need to literacy some knowledge points (to literacy, embarrassment):<br \/>\nCluster (Cluster)<br \/>\nSimply put, N servers form a loosely coupled multiprocessor system (external is a server), internal communication through the network. Let N servers cooperate with each other and jointly carry the request pressure of a website. In the words of the last author is &#8220;the same business, deployed on multiple servers,&#8221; This is the cluster. The more important task in the cluster is task scheduling.<br \/>\nLoad Balance<br \/>\nsimply means that the request is distributed to each server in the cluster according to a certain load policy, and the entire server group processes the website request so as to jointly complete the task.<br \/>\n2, the installation environment is as follows:<br \/>\nTencent cloud host, the installation is CentOS 7.3 64bits<br \/>\nNginx 1.7.4<br \/>\nJDK8 and Tomcat8<\/p>\n<p>Configure Nginx web reverse proxy to implement two Tomcat load balancing:<\/p>\n<p>&#8211;Install and configure Tomcat tar -zxvf apache-tomcat-8.5.28.tar.gz<br \/>\ncp -rf apache-tomcat-8.5.28 \/usr\/local\/<br \/>\ntomcat1 mv apache-tomcat-8.5.28 \/usr\/local\/tomcat2<\/p>\n<p>&#8211; Modify the tomcat1 port number<br \/>\n$ cd \/usr\/local\/tomcat1\/conf\/<br \/>\n$ cp server.xml server.xml.bak<br \/>\n$ cp web.xml web.xml.bak<br \/>\n$ vi server.xml<\/p>\n<p># # Modify the Tomcat2 port number<br \/>\n$ cd \/ usr \/ local \/ tomcat \/ conf \/<br \/>\n$ cp server.xml server.xml.bak<br \/>\n$ cp web.xml web.xml.bak<br \/>\n$ vi server.xml<\/p>\n<p>&#8211; Add Tomcat1 to boot automatically<br \/>\n&#8211; Copy the \/usr\/local\/tomcat1\/bin\/catalina.sh file to the \/etc\/init.d directory and rename it to tomcat1<br \/>\n# cp \/usr\/local\/tomcat1\/bin\/catalina. Sh \/etc\/init.d\/tomcat1 &#8211;Modify the<br \/>\n\/etc\/init.d\/tomcat1 file and add it in the file:<br \/>\n# vi \/etc\/init.d\/tomcat1 &#8211;Enter the<br \/>\nfollowing in the first line (otherwise Error: The tomcat service does not support chkconfig):<\/p>\n<p>#!\/bin\/sh<br \/>\n# chkconfig: 2345 10 90<br \/>\n# description:Tomcat1 service<\/p>\n<p># Licensed to the Apache Software Foundation (ASF) under one or more<br \/>\n# contributor license agreements. See the NOTICE file distributed with<br \/>\n&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;. &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<br \/>\n# $ Id: catalina. Sh 1498485 2013-07-01 14:37:43Z markt $<br \/>\n# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>CATALINA_HOME=\/usr\/local\/tomcat1<br \/>\nJAVA_HOME=\/usr\/local\/jdk8<\/p>\n<p># OS specific support. $var _must_ be set to either true or false.<\/p>\n<p>&#8211; Add tomcat service:<br \/>\n# chkconfig &#8211;add tomcat1<br \/>\n&#8211; Set tomcat to boot from:<br \/>\n# chkconfig tomcat1 on &#8211; Tomcat2 will be set to boot from<br \/>\n&#8211; Copy \/usr\/local\/tomcat2\/bin\/catalina.sh file To \/etc\/init.d directory and renamed tomcat2<br \/>\n# cp \/usr\/local\/tomcat2\/bin\/catalina.sh \/etc\/init.d\/tomcat2<br \/>\n&#8211; Modify \/ etc \/ init.d \/ tomcat2 file in the file :<br \/>\n# Vi \/etc\/init.d\/tomcat2<br \/>\n&#8211; Enter the following in the first line (otherwise error: tomcat service does not support chkconfig):<br \/>\n#! \/ Bin \/ sh<br \/>\n# chkconfig: 2345 10 90<br \/>\n# description: Tomcat2 Service<\/p>\n<p># Licensed to the Apache Software Foundation (ASF) under one or more<br \/>\n# contributor license agreements. See the NOTICE file distributed with<br \/>\n&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;. &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<br \/>\n# &#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>CATALINA_HOME=\/usr\/local\/tomcat2<br \/>\nJAVA_HOME=\/usr\/local\/jdk8<\/p>\n<p># OS specific support. $var must be set to either true or false.<\/p>\n<p>&#8211; Add tomcat service:<br \/>\n# chkconfig &#8211;add tomcat2<br \/>\n&#8212; Set tomcat to start from boot:<br \/>\n# chkconfig tomcat2 on<\/p>\n<p>Here, tomcat has been installed, respectively, enable them, print out the referenced environment corresponds to two tomcat is right:<\/p>\n<p>[root @ VM_177_101_centos src] # service tomcat1 start<br \/>\nUsing CATALINA_BASE: \/ usr \/ local \/ tomcat1<br \/>\nUsing CATALINA_HOME: \/ usr \/ local \/ tomcat1<br \/>\nUsing CATALINA_TMPDIR: \/ usr \/ local \/ tomcat1 \/ temp<br \/>\nUsing JRE_HOME: \/ usr \/ local \/ jdk8<br \/>\nUsing CLASSPATH : \/usr\/local\/tomcat1\/bin\/bootstrap.jar:\/usr\/local\/tomcat1\/bin\/tomcat-juli.jar<br \/>\nTomcat started.<\/p>\n<p>[root @ VM_177_101_centos src] # service tomcat2 start<br \/>\nUsing CATALINA_BASE: \/ usr \/ local \/ tomcat2<br \/>\nUsing CATALINA_HOME: \/ usr \/ local \/ tomcat2<br \/>\nUsing CATALINA_TMPDIR: \/ usr \/ local \/ tomcat2 \/ temp<br \/>\nUsing JRE_HOME: \/ usr \/ local \/ jdk8<br \/>\nUsing CLASSPATH: \/usr\/local\/tomcat2\/bin\/bootstrap.jar:\/usr\/local\/tomcat2\/bin\/tomcat-juli.jar<br \/>\nTomcat started.<\/p>\n<p>Last configuration configuration Nginx:<\/p>\n<p>&#8211; Switch to the directory<br \/>\ncd \/usr\/local\/nginx\/conf<br \/>\n&#8211; Modify the configuration file<br \/>\nvi nginx.conf<\/p>\n<p>&#8211; some common configuration &#8211;<br \/>\nworker_processes: the number of work processes can be configured more than one<br \/>\n&#8211;worker_connections: the maximum number of connections<br \/>\nper process &#8211;server: each server is equivalent to a proxy server<br \/>\n&#8211;lister: monitor port, the default 80<br \/>\n&#8211; server_name: The current service domain name, there can be more than one, separated by a space (we are local and therefore localhost) &#8211;<br \/>\nLocation: said path matching, then configured \/ that all requests are matched here<br \/>\n&#8211;index: when If you do not specify a home page, the specified file will be selected by default, and can be separated by more than one space.<br \/>\n&#8211;proxy_pass: request to a custom server list<br \/>\n&#8211;upstream name {}: server cluster name<\/p>\n<p>&#8211; Now want to access tomcat through nginx, you need to modify the server part of the configuration<\/p>\n<p>server<br \/>\n{<br \/>\nlisten 80 default;<br \/>\ncharset utf-8;<br \/>\nserver_name localhost;<br \/>\naccess_log logs \/ host.access.log;<\/p>\n<p>Location \/ {<br \/>\nproxy_pass http:\/\/localhost:8080;<br \/>\nproxy_redirect default;<br \/>\n}<br \/>\n}<br \/>\n&#8211; The proxy has been completed here, so that all requests need to go through the proxy server to access the official server.<\/p>\n<p>Next, load balancing is implemented. During the installation process, the port configured by tomcat1 is 8080, and the port configured by tomcat2 is 8081. Then we need to define the upstream server in the configuration file (upstream server)<\/p>\n<p># The cluster<br \/>\nupstream testcomcat {<br \/>\n#weight is the greater the weight, the greater the probability of allocation<br \/>\nserver 127.0.0.1:8080 weight=1;<br \/>\nserver 127.0.0.1:8081 weight=2;<br \/>\n}<\/p>\n<p>Server<br \/>\n{<br \/>\nlisten 80 default;<br \/>\ncharset utf-8;<br \/>\naccess_log logs\/host.access.log;<\/p>\n<p>location \/ {<br \/>\nproxy_pass http: \/\/ testcomcat;<br \/>\nproxy_redirect default;<br \/>\n}<br \/>\n}<\/p>\n<p>&#8211; In order to see is not the same, I will tomcat root the following index.jsp page slightly changed, respectively, joined TEST1, TEST2, to facilitate the distinction, restart nginx, the browser address bar enter the IP, visits, refresh several times Page, you will find that the switch between the two servers, as shown below:<br \/>\nservice nginx reload<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This essay focuses on Tomcat clusters and Tomcat load balancing using Nginx reverse proxies.<\/p>\n<p>First, we need to literacy some knowledge points (to literacy, embarrassment): Cluster (Cluster) Simply put, N servers form a loosely coupled multiprocessor system (external is a server), internal communication through the network. Let N servers cooperate with each other and jointly [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7247"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7247"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7247\/revisions"}],"predecessor-version":[{"id":7248,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7247\/revisions\/7248"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}