How to configure multiple tomcats with Nginx load balancing under Linux

How to configure multiple tomcats with Nginx load balancing under Linux

The methods of installing nginx and multiple tomcats under Linux are not introduced here. If you are not clear, you can refer to:

Install nginx on Linux:

https://www.jb51.net/article/159519.htm

Install multiple tomcats on Linux:

https://www.jb51.net/article/159521.htm

When our server has installed nginx and multiple tomcats, we can now try to play with nginx's load balancing.

First briefly introduce my operating environment

An Alibaba Cloud server,

Linux system, jdk1.8, nginx installed,

Four tomcats are installed and the port numbers are configured, corresponding to 8080, 8081, 8082, and 8083 respectively.


1: Enter the conf directory under the nginx directory

This is my nginx installation directory:

[root@aliServer ~]# cd /usr/local/nginx/conf

Two: Edit nginx.conf


[root@aliServer conf]# vi nginx.conf

3. Configure the server group

1: Add upstream configuration between http{} nodes. (Be careful not to write localhost, otherwise the access speed will be very slow)

upstream nginxDemo {
  server 127.0.0.1:8081; #Server address 1
  server 127.0.0.1:8082; #Server address 2
  server 127.0.0.1:8082; #Server address 3
  server 127.0.0.1:8083; #Server address 4
}

2: Change the port number 80 that nginx listens on

The default port of nginx is 80. I have not changed it yet.

server {
  listen 80; #The default is 80, which can be changed to other port numbers. Of course, occupied port numbers cannot be written.
  ......
}

3: Use proxy_pass to configure the reverse proxy address

In location\{}, the "http://" must be included, and the following address must be consistent with the name defined in the first upstream step (that is, the name nginxDemo is customized, and the two places need to be consistent)

location / {
      root html;
      index index.html index.htm;
      proxy_pass http://nginxDemo; #Configure the proxy address}

After the configuration is completed, as shown in the figure:


Four: Start nginx

My installation path of nginx is /usr/local/nginx

So my startup command is:

[root@aliServer ~]# /usr/local/nginx/sbin/nginx

Because nginx was started when it was installed before, it will report an error that the port number is occupied when it is started again


At this time, we use the command to view the occupancy of each port number

[root@aliServer ~]# netstat -ntpl 

We can see that nginx is occupied by PID 9097. Use kill -9 to kill it.

[root@aliServer ~]# kill -9 9097

Start nginx again

[root@aliServer ~]# /usr/local/nginx/sbin/nginx

There is no response, that's right, then enter your server address in the browser


This indicates that nginx has been started successfully. Now let's verify whether the configuration is correct and whether the load can be balanced. . .

Five: Verification

We all know that when nginx load balancing is used, all client requests go through nginx, so nginx can decide to whom to forward these requests. If server A has more resources (more CPU, more memory, etc.), and server B does not have as strong processing power as server A, then nginx will forward more requests to A and fewer requests to server B. In this way, load balancing is achieved, and even if one of the servers goes down, users can still access the website normally.

Before verification, some preparation is required.

1: Prepare a simple jsp, such as:


I installed 4 tomcats on one server, so I prepared 4 index.jsp files

They are

<title>Tomcat8080<title> <h1>Hellow Tomcat_8080</h1>
<title>Tomcat8081<title> <h1>Hellow Tomcat_8081</h1>
<title>Tomcat8082<title> <h1>Hellow Tomcat_8082</h1>
<title>Tomcat8083<title> <h1>Hellow Tomcat_8083</h1>

It should be noted here that the name of the jsp file must be index.jsp, because the screen of successful startup of tomcat is as shown in the figure:


Read webapps/ROOT/index.jsp in the tomcat installation directory

My address is: /usr/java/tomcat/tomcat_8080/webapps/ROOT


Overwrite each tomcat's default index.jsp file with the four index.jsp files prepared earlier.

Start each tomcat

[root@aliServer bin]# ./startup.sh

At this time, we enter xxx.xxx.xx.xx:8080 in the browser and you will find that the kitten no longer appears, but. . . . . .




All 4 tomcats have been started successfully, and nginx has also been started successfully.

At this time, enter your server IP in the browser and refresh the page continuously. You will find that the page displays 8080, 8081, 8082, and 8083. Of course, this is because nginx decides where to send the request based on which server has more sufficient resources. The address of our request in the browser remains unchanged, but we access different tomcat servers, which means that nginx is configured successfully.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to implement Nginx reverse proxy and load balancing (based on Linux)
  • Detailed explanation of nginx server installation and load balancing configuration on Linux system
  • How to build nginx load balancing under Linux
  • Detailed explanation of Linux system configuration nginx load balancing
  • Detailed explanation of the use cases of Nginx load balancing configuration on Linux.

<<:  MySQL Community Server compressed package installation and configuration method

>>:  Detailed explanation of JavaScript Promise and Async/Await

Recommend

Vue implements sending emoticons in chat box

The specific code for sending emoticons in the vu...

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

How to create an Nginx server with Docker

Operating environment: MAC Docker version: Docker...

Vue implements a simple shopping cart example

This article example shares the specific code of ...

Solve the error "Can't locate ExtUtils/MakeMaker.pm in @INC"

When installing mha4mysql, the steps are roughly:...

Vue project realizes paging effect

The paging effect is implemented in the vue proje...

JS uses canvas technology to imitate echarts bar chart

Canvas is a new tag in HTML5. You can use js to o...

vue-cli configuration uses Vuex's full process record

Table of contents Preface Installation and Usage ...

Native js to implement 2048 game

2048 mini game, for your reference, the specific ...

Detailed explanation of the data responsiveness principle of Vue

This article is mainly for those who do not under...

How to periodically clean up images that are None through Jenkins

Preface In the process of continuous code deliver...

WeChat applet implements sorting function based on date and time

I recently took over a small program project, and...