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

Explain in detail the construction of load balancing server based on Centos7+Nginx+Tomcat8

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

Share

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

Due to the demand of work, in use, we need to build load balancing, study the scheme of Apache+Tomat load balancing, and by retrieving relevant articles, we find that the efficiency of Apache load balancing is far lower than that of Nginx, so we decided to use Nginx to build services. Initially, Nginx was installed in YUM, but for some reason, Nginx services can be started normally and static pages can be accessed normally, but virtual hosts and load balancers cannot be built normally. Without delving into the cause of the quotation, I decided to uninstall it and install it manually.

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

The discussion on how to install Tomcat,Nginx,Redis is outside the scope of this discussion.

If you need it, please check

Detailed explanation of CentOS7.0 installation and configuration of Tomcat-7

Introduction of two methods of installing nginx in centos7

Explain the installation and deployment of Redis on Centos7 in detail

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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