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 realize Nginx+Tomcat load balancing Cluster

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

Share

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

This article introduces the knowledge of "how to achieve Nginx+Tomcat load balancing cluster". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!

Introduction

In general, a Tomcat site may have a single point of failure and can not cope with the complex and diverse requests of too many customers, so it can not be used in the production environment alone, so a more reliable solution is needed to improve the Web site architecture.

I. Overview of the case

Nginx is a very excellent http server software, which can support up to 50000 concurrent connections, has a strong static resource processing capacity, runs very stably, and the consumption of memory, CPU and other system resources is very low.

At present, many large websites use Nginx server as the reverse proxy and load balancer of back-end website programs to improve the load concurrency ability of the whole site.

In the case, Nginx is used as the load balancer and Tomcat as the load cluster setting method of the application server. The architecture diagram is as follows.

II. Environmental deployment

The case environment is as follows

Host operating system IP address main software Nginx server CentOS 7.4 x86_64192.168.8.140nginx-1.12.2.tar.gzTomcat server 1CentOS 7.4 x86 "64192.168.133 ① apache-tomcat-9.0.16.tar.gz / ② jdk-8u201-linux-x64.rpmTomcat server 2CentOS 7.4 x86" 64192.168.134 ① apache-tomcat-9.0.16.tar.gz / ② jdk-8u201-linux-x64.rpm

Turn off the firewall, turn off the boot

[root@ng133 ~] # systemctl stop firewalld.service # turn off the firewall and boot function [root@ng133 ~] # systemctl status firewalld.service [root@ng133 ~] # setenforce 0 # shut down the security enhancement system [root@ng133 ~] # setenforce: SELinux is disabled III, Nginx host installation

Install the Nginx service, the one-click deployment script used here

#! / bin/bashiptables-Fyum-y install epel-release & & yum clean all & & yum makecacheyum-y install pcre-devel zlib-devel gcc gcc-c++ make wgetuseradd-M-s / sbin/nologin nginxwget http://nginx.org/download/nginx-1.12.2.tar.gz-P / opttar zxvf / opt/nginx-1.12.2.tar.gz-C / optcd / opt/nginx-1.12.2./configure\-prefix=/usr/local/nginx\-- user=nginx\- -group=nginx\-- with-http_stub_status_modulecd / opt/nginx-1.12.2make-j 4 & & make installln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/cat > / usr/lib/systemd/system/nginx.service / usr/local/nginx/html/index.html [root@ng140 ~] # cat / usr/local/nginx/html/index.html this is static [root@ng140 / usr/local/nginx/html] # mkdir / usr/local/ Nginx/html/picture # upload image [root@ng140 / usr/local/nginx/html] # cd picture/ [root@ng140 / usr/local/nginx/html/picture] # rz-Erz waiting to receive. [root@ng140 / usr/local/nginx/html/picture] # lsha.jpg# main configuration file add the following configuration parameters under line 57 Enable the page to load images [root@ng140 / usr/local/nginx/html/picture] # vim / usr/local/nginx/conf/nginx.conflocation ~. *\. (gif | jpg | jpeg | png | bmp | swf | css) ${root / usr/local/nginx/html/picture Expires 10d;} [root@ng140 / usr/local/nginx/html/picture] # nginx-t [root@ng140 / usr/local/nginx/html/picture] # systemctl restart nginx.service

Configure the nginx master profile

[root@ng140 ~] # vim / usr/local/nginx/conf/nginx.conf.# configure the server list of load balancer. The weight parameter indicates the weight. The higher the weight, the greater the probability of being assigned # gzip on. Add the following below line # 33: upstream tomcat_server {server 192.168.133 weight=1;server 8080 weight=1;server 192.168.8.134 8080 weight=1 Add the following configuration parameter 45 # access_log logs/host.access.log main; 46 location ~. *\ .jsp$ {47 proxy_pass http://tomcat_server; under line 45 48 proxy_set_header HOST $host; 49 proxy_set_header X-Real-IP $remote_addr; 50 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 51}

Add parameter resolution

Location ~. *\ .jsp$ {# assigns the ip address of the client received by nginx to the source ip that jumps to the tomcat request, identifies the customer's real ip, and assigns a value to the jump proxy_pass http://tomcat_server; proxy_set_header HOST $host # set the hostname (domain name or ip, port) of the request received by the backend web server. The default value of host is the hostname proxy_set_header X-Real-IP $remote_addr; set by proxy _ pass direct connection # copy $remote_addr to X-Real-IP (custom), and IPproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for back and forth # when nginx is the proxy server, the IP list will record the passing and its ip, the agent and its ip

Check the master profile syntax and restart the service

[root@ng140 ~] # nginx-tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful [root@ng140 ~] # systemctl restart nginx.service6. Verification result

Test the effect of static pages with Firefox browser

Test whether the load balancing is effective. You can see the switch between dynamic pages 1 and 2 by refreshing the web page.

Http://192.168.8.140/index.jsp

Summary

You can put two or more Tomcat server into the upstream of Nginx to form a load balancer cluster, and then set the cluster site in the location through the way of proxy_pass as a Web proxy, and then set the weight of the Tomcat server by the weight value.

In a production environment, the hardware configuration of Tomcat server may be different, and the access requests of servers with higher configuration or lower configuration can be assigned and controlled by modifying the weight value of the corresponding server.

This is the end of the content of "how to implement Nginx+Tomcat load balancing Cluster". 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