Complete steps for using Nginx+Tomcat for load balancing under Windows

Complete steps for using Nginx+Tomcat for load balancing under Windows

Preface

Today, Prince will talk to you about the complete steps of how to use Nginx+Tomcat for load balancing under Windows. You can try to practice it yourself.

In addition, I would like to point out that this article is a purely practical article and does not involve too much interpretation of the principles. I may open a separate column later to discuss the principles of Nginx in depth.

Okay, let’s get started.

Download Nginx

First, we go to the official website to download a stable version of Nginx for Windows. The official website address is: http://nginx.org/en/download.html

After downloading, unzip it to the desired location. Friends, please pay attention, do not unzip it to a folder with Chinese characters, as this will cause it to fail to start.

The startup on Windows is very simple. We just need to enter the folder and double-click nginx.exe to start it directly.

The default port of nginx is port 80. Since port 80 on Prince's computer is occupied by ISS, we change the port to port 81.

It is also easy to modify the port. Just go to the conf folder, open the nginx.conf file, and modify the following location:

After the modification, we can start the Nginx service by double-clicking it. You will see a command window flashing by. Don’t worry about it, because it has actually started successfully. We enter http://localhost:81/ in the browser and we can see the Nginx startup page.

In this way, our Nginx environment is deployed.

Configuration file introduction

Now that we have an Nginx environment, our next goal is to reverse proxy the user's request to Tomcat through Nginx, so we first start a Tomcat server with the default configuration.

Then we open the nginx.conf file, and Prince will briefly introduce the meaning of some of the configurations in it.

    listen 81;
    server_name localhost;

Listen: We have just changed it. It represents the listening port of Nginx. There is nothing to say about it.

server_name: indicates where the request will be forwarded after being monitored. By default, it will be forwarded directly to the local computer.

    location / {
      root html;
      index index.html index.htm;
    }

location: indicates the matching path. In this case, / indicates that all requests are matched here.

root: The root is configured inside, which means that when the path of this request is matched, the corresponding file will be found in the html folder.

index: When no homepage is specified, the specified file will be selected by default. There can be multiple indexes, and they are loaded in order. If the first one does not exist, the second one is found, and so on.

In addition to these configurations, we add another configuration

proxy_pass, it represents the proxy path, which is equivalent to forwarding, unlike the root mentioned before, which must specify a folder.

Now let's modify the configuration file as follows:

location / { 
  proxy_pass http://localhost:8080;
}

Then we let Nginx reload the configuration file. Go back to the Nginx root directory and execute the nginx -s reload command.

Then we reopen the Nginx page, friends, did you find that it has opened the Tomcat page?

Implementing load balancing configuration

We have just implemented the reverse proxy of the request, forwarding it from Nginx to Tomcat. So how to configure a Tomcat load balancing cluster? It is actually very easy.

The configuration is as follows:

upstream localtomcat { 
  server localhost:8080; 
} 
 
server{ 
    location / { 
      proxy_pass http://localtomcat; 
    } 
    #......Others omitted}

Friends, this is the key point, you must pay attention here. The name after upstream must not have an underscore. Nginx does not recognize underscores and will cause forwarding abnormalities.

So how do you add a new tomcat to achieve load balancing?

We modify the port, open a new tomcat server, port 8081, and then add the following configuration:

upstream localtomcat { 
  server localhost:8080; 
  server localhost:8081; 
}

Reload the Nginx configuration file and you will find that load balancing has been implemented and requests will now be forwarded to the two tomcats.

And we can set weight=number to specify the weight of each tomcat. The larger the number, the greater the chance of the request being accepted.

The configuration is as follows:

upstream localtomcat { 
  server localhost:8080 weight=1; 
  server localhost:8081 weight=5; 
}

Summarize

Well, here we have built a load balancing cluster of Nginx+Tomcat under Windows.

That’s all for Prince’s sharing today. Welcome friends to continue to pay attention to follow-up articles.

The above is the detailed content of the complete steps of using Nginx+Tomcat for load balancing under Windows. For more information about Nginx+Tomcat for load balancing, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Tomcat Nginx Redis session sharing process diagram
  • Nginx/Httpd reverse proxy tomcat configuration tutorial
  • Nginx/Httpd load balancing tomcat configuration tutorial
  • Build Tomcat9 cluster through Nginx and realize session sharing
  • How to use Docker to build a tomcat cluster using nginx (with pictures and text)
  • Detailed tutorial on how to monitor Nginx/Tomcat/MySQL using Zabbix
  • Implementation of deploying vue project to nginx/tomcat server
  • Nginx+Tomcat high performance load balancing cluster construction tutorial
  • Analysis of the process of implementing Nginx+Tomcat cluster under Windwos

<<:  Mysql aggregate function nested use operation

>>:  Example of implementing login effect with vue ElementUI's from form

Recommend

A complete list of commonly used HTML tags and their characteristics

First of all, you need to know some characteristi...

CSS achieves colorful and smart shadow effects

background Ever wondered how to create a shadow e...

MySQL full backup and quick recovery methods

A simple MySQL full backup script that backs up t...

How to count the number of specific characters in a file in Linux

Counting the number of a string in a file is actu...

Detailed explanation of the new array methods in JavaScript es6

Table of contents 1. forEach() 2. arr.filter() 3....

How to operate json fields in MySQL

MySQL 5.7.8 introduced the json field. This type ...

MySQL query sorting and paging related

Overview It is usually not what we want to presen...

Summary of Docker configuration container location and tips

Tips for using Docker 1. Clean up all stopped doc...

Docker implements re-tagging and deleting the image of the original tag

The docker image id is unique and can physically ...

CSS animation property usage and example code (transition/transform/animation)

During development, a good user interface will al...

Script to quickly list all host names (computer names) in the LAN under Linux

Recently, I have a need to list all host names in...