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:
|
<<: Mysql aggregate function nested use operation
>>: Example of implementing login effect with vue ElementUI's from form
Table of contents 1. Directive custom directive 2...
Table of contents Scenario Code Implementation Su...
Table of contents Special characters in URLs URL ...
1. Download the software 1. Go to the MySQL offic...
I recently wrote a script for uploading multiple ...
When I was looking at some CSS3 animation source ...
1. Scenario display The tomcat log occasionally r...
Preface Sometimes file copies amount to a huge wa...
Table of contents 1 Introduction to the new opera...
NAT In this way, the virtual machine's networ...
There are three ways to create an image: creating...
First create a specific project directory for you...
Preface Speaking of text search tools, everyone m...
Table of contents 1. Requirements description 2. ...
Preface In MySQL, multi-table join query is a ver...