Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Methods and steps of multiple tomcat configuration for Nginx load balancer under Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Linux installation nginx and installation of multiple tomcat methods here is not too much introduction, not clear can refer to:

Linux installation nginx:

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

Linux installs multiple tomcat:

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

Now that we have nginx installed on our server and have multiple tomcat installed, we can try to play with nginx's Load Balancer.

Let's start with a brief introduction to my operating environment

An Ali Cloud Virtual Machine,

Linux system, jdk1.8, nginx installed,

4 tomcat installed and configured port numbers 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

Edit nginx.conf

[root@aliServer conf]# vi nginx.conf

III: Configure Server Groups

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

upstream nginxDemo { server 127.0.0.1: 8081; #serveraddress1 server 127.0.0.1: 8082; #serveraddress2 server 127.0.0.1: 8082; #serveraddress3 server 127.0.0.1: 8083; #serveraddress4}

2: Modify the port number 80 on which nginx listens

The default port of nginx is 80. I haven't changed it yet. It remains unchanged.

server { listen 80; #Default is 80, can also be changed to other, of course, has been occupied port number can not be written. ......}

3: Configure reverse proxy address with proxy_pass

In location\{}, there must be no less than "http://," and the following address must be consistent with the name defined in the upstream in the first 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 directional proxy address }

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

4: Start nginx

My installation path is/usr/local/nginx

So my activation order is:

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

Because nginx has been started before installation, and now it will report an error port number is occupied.

At this time we use the command to check the port number occupation

[root@aliServer ~]# netstat -ntpl

We see that nginx is occupied by the PID 9097 and killed with kill -9.

[root@aliServer ~]# kill -9 9097

Start nginx again.

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

if there is no response, that's right. type your serv address into your browser

Description nginx started successfully, as for whether the configuration is correct, whether the load balance, now start verifying.

V: Verification

We all know that when nginx Load Balancer, all requests from clients go through nginx, so nginx can decide who to forward these requests to. If server A has more resources (more CPU, more memory, etc.), server B is not as powerful as server A, then nginx will forward more requests to A and fewer requests to server B. This will achieve Load Balancer, and even if one of the servers is down, users can still access the website normally.

Before you validate, you need to prepare.

1: Prepare a simple point jsp, such as:

I have 4 tomcat installed on a server, so I have 4 index.jsp files

are

Tomcat8080 Hellow Tomcat_8080Tomcat8081 Hellow Tomcat_8081Tomcat8082 Hellow Tomcat_8082Tomcat8083 Hellow Tomcat_8083

What needs to be noted here is that the name of the:jsp file must be index.jsp, because tomcat starts successfully, as shown in the figure:

The requested URL/webapps/ROOT/index.jsp was not found on this server.

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

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

Open each tomcat

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

At this time, we will enter xxx.xxx.xx.xx:8080 in the browser and you will find that the kitten does not appear, but...

4 tomcat have been successfully launched, nginx has also been successfully launched.

At this time, enter your server IP in the browser, keep refreshing the page, you will find that the page will display 8080, will display 8081, will display 8082, will display 8083, of course, this is nginx according to which server resources are more adequate and decide where to request, we in the browser request address unchanged, but access is a different tomcat server, explain, nginx configuration success.

The above is all the content of this article, I hope to help everyone's study, but also hope that everyone a lot of support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report