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

How to build a load balancing server based on Centos7+Nginx+Tomcat8

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to build a load balancing server based on Centos7+Nginx+Tomcat8". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

After manually installing nginx, everything is normal, and there is no problem with the configuration of virtual hosts and load balancers. Enabled smoothly, the specific configuration file is as follows:

Add: include vhost/*.conf; to the nginx.conf file of conf to introduce all the .conf files in the vhost directory for easy configuration and, of course, one by one if necessary.

Since the load service is the api service, the name of the conf file here is api.xxx.com.conf. The configuration for opening the file is as follows:

Ip polling is not used here, but usstream is used. what are the differences between these ways? you can introduce them by Baidu or google. What needs to be noted is proxy_pass. The name configured here is upstream, and others are not allowed. If the ip address is configured directly, load balancing will not be implemented.

The greater the number of weights represented by weight, the higher the weight, and the greater the chance of allocation. Backup refers to the hot backup server, that is, when there are problems with the first two loads, the hot backup server will automatically undertake these visits.

At this point, the configuration of the nginx load balancer configuration is complete, but because after the load is involved, if it is an ordinary URL, the loss of session will be reproduced when visiting, so at this time, it is necessary to deal with the problem of session loss. To deal with this problem, there are several solutions, one is to use the cookies proxy, the other is to realize the sharing of session, and the sharing of session is used here. Relevant configuration needs to be made in tomcat. To achieve this requirement, the shared server of session configured here serves redis memory, mainly to ensure efficient data reading and storage. When using redis for session sharing, you must add the following names:

There is no problem with supporting tomcat8 in these packages, and no related tests have been carried out on the specific support of other versions.

Then modify the configuration file in tomcat:

The address of 10.0.0.x seen here is all completed by the internal network, the external network is not connected, and only the server responsible for the load is directly connected to the external network.

At this time, you can successfully start the tomcat server and the nginx server, but before that, you must start the nginx server first, otherwise tomcat may fail to start. The nginx service can start smoothly

The three configurations are as follows:

Referenced content added in nginx.conf

Include vhost/*.conf

Load balancer configuration:

Upstream api.xxx.com {server 10.0.0.12 weight=5; server 8080 weight=5; server 10.0.0.15 server 8080 weight=5 backup;} server {listen 80; server_name api.xxx.com; location / {proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $remote_addr; proxy_pass http://api.xxx.com;}}

Tomcat configuration

This is the end of the content of "how to build a load balancing server based on Centos7+Nginx+Tomcat8". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report