windows under nginx+tomcat configuration load balancing method

  • 2020-05-12 06:51:53
  • OfStack

Objective: as HttpServer, Nginx connects multiple application instances of tomcat for load balancing.

Note: this routine takes 1 machine as an example, i.e. 1 nginx and 2 Tomcat are installed on the same machine and JDK1.7 is installed.

1. Install Nginx
Install the Nginx tutorial

2. Configure two Tomcat
Two Tomcat are configured on this machine, tomcat7-8081 and tomcat7-8082 respectively.
tomcat 7-8081: http://localhost:8081, this is 8081 port
tomcat7-8082 visit address: http://localhost:8082, browse display content: this is 8082 port

D:\div\ tomcat7-8081 \webapps\ROOT\ index.jsp


<!DOCTYPE html>

<html lang="en">
  <head>this is 8081 port</head>
</html>

D:\div\ tomcat7-8082 \webapps\ROOT\ index.jsp


<!DOCTYPE html>

<html lang="en">
  <head>this is 8082 port</head>
</html>

Configure multiple Tomcat tutorials on the same server

If you want to configure multiple Tomcat servers on one server, the main problem is to avoid port conflicts on the Tomcat servers. Just modify the startup port and connection port in CATALINA_HOME\conf\ server.xml!


 For your reference, let's write down the configuration details below :(in this case, configuration 3 a Tomcat As an example) 
1.  download apache-tomcat-7.0.63 , download the file for apache-tomcat-7.0.63.zip.
2.  Unzip the zip to D:/div/ Directory. 
3.  Modify the unzip folder name to: tomcat7-8080
4.  in D:/div/ Create two copies of this folder under the directory, renamed as: tomcat7-8081 , tomcat7-8082
5.  Add environment variables: right-click my computer -> Select properties -> Select advanced -> Select environment variables: add system variables: 
CATALINA_HOME_8080 , its value is: D:\div\tomcat7-8080 ; 
CATALINA_HOME_8081 , its value is: D:\div\tomcat7-8081 ; 
CATALINA_HOME_8082 , its value is: D:\div\tomcat7-8082 ; 

6.  Modify the startup and shutdown ports: 
 Enter the D:\div\tomcat7-8081\conf\ Directory, open server.xml File, modify the following two places: 
 ( 1 ) <Server port="8006" shutdown="SHUTDOWN">
 Modify the port= " 8006 ", the original default is: 8005 Make it close the port and another 1 None of the closed ports conflict. 
 ( 2 ) <Connector port="8081" maxHttpHeaderSize="8192"
        maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
        enableLookups="false" redirectPort="8443" acceptCount="100"
        connectionTimeout="20000" disableUploadTimeout="true" />
 Modify the port= " 8081 ", the original default is ". 8080 ", making it connected to the port and another 1 There is no conflict. 
 ( 3 ) <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
 Modify the port= " 8010 ", the original default is: 8009 . AJP 1.3 Connector Where it's defined. 

7.  Modify the startup.bat and catalina.bat File contents: 
(1)  Open the D:\div\tomcat7-8081\bin\startup.bat File them all CATALINA_HOME Replace with CATALINA_HOME_8081 . 
(2)  Open the D:\div\tomcat7-8081\bin\catalina.bat File them all CATALINA_HOME Replace with CATALINA_HOME_8081 . 

tomcat7-8082 Configuration method and configuration tomcat7-8081 steps 1 Kind of. 

8 . Start the Tomcat , enter separately from the command line 3 A different Tomcat Install under directory, execute startup.bat , respectively start 3 a Tomcat . Then type in the browser: 
http://localhost:8080
http://localhost:8081 
http://localhost:8082 

9.  So far, we've been here 1 Configured on a server 3 a Tomcat . 

Thus, we successfully set up one nginx service and successfully configured two application instances of tomcat.

3. Nginx+Tomcat load balancing configuration
All you need to do is change the configuration of Nginx to forward it via tomcat.
a, nginx.conf configuration files


worker_processes 1;

events {
  worker_connections 1024;
}

http {
  include    mime.types;
  default_type application/octet-stream;
  sendfile    on;
  keepalive_timeout 10;
  include extra/upstream01.conf;
}

b, extra/ upstream01.conf files, load balancing configuration information


upstream mysite {
  server localhost:8081 weight=5;
  server localhost:8082 weight=5;
}
 
server {
  listen 80;
  server_name localhost;
 
  location / {
  proxy_pass http://mysite;
  }
}

When a request arrives at localhost, the request is dispatched to the list of servers set up by the corresponding upstream. Each request is dispatched to a random server.

Then run start nginx once, and when you keep refreshing http://localhost, the browser will switch back and forth between "this is is 8081 port" and "this is 8082 port".

This indicates that the load balancing configuration was successful!!


Related articles: