Dynamic static separation cluster for load balancing of Nginx+Tomcat

1, Introduction to Tomcat

Originally developed by Sun's software architect James Duncan Davidson

After installing tomcat, the directories and files under the installation path are important files for using or configuring Tomcat

Tomcat important directory

bin: store the startup and shutdown Tomcat scripts

conf: store different configuration files of Tomcat

doc: store Tomcat documents

lib: store the library files required for Tomcat operation

logs: stores the log file during Tomcat execution

src: store the source code of tomcat

webapps: Tomcat's main web publishing directory

work: store the class file generated after jsp compilation

Nginx applications:

Nginx is an excellent HTTP server software

Support responses with up to 50000 concurrent connections

It has strong static resource processing ability

Stable operation

The consumption of system resources such as memory and CPU is very low

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

2, Nginx load balancing, dynamic and static separation principle

Illustration:


In the standalone mode, Tomcat runs alone and directly accepts user requests. It is not recommended.

The reverse agent, running on a single machine, provides an nginx as the reverse agent, which can provide static response from nginx and dynamic jsp

Agent to Tomcat

LNMT: Linux + Nginx + MySQL + Tomcat

LAMT: Linux + Apache(Httpd)+ MySQL + Tomcat

One Nginx is used in the front to do reverse proxy and load balancing scheduling for multiple Tomcat instances. The pure dynamic pages deployed on Tomcat are more convenient

fit

LNMT: Linux + Nginx + MySQL + Tomcat

Multilevel agent

LNNMT: Linux + Nginx + Nginx + MySQL + Tomcat

The problem with dynamic servers is that the concurrency capability is too weak, and multiple dynamic servers are often required to provide services together. How to allocate concurrent pressure requires scheduling. A certain scheduling strategy is adopted to distribute requests to different servers, which is Load Balance.

When single machine Tomcat evolves multi machine and multi-level deployment, a problem is highlighted, which is Session. The origin of this problem is that the HTTP protocol did not think of the future development at the beginning of its design.

3, Deploying the ngnix load balancer

Build the environment with three hosts:

nginx: 192.168.187.160

tomcat1:192.168.187.170

tomcat2:192.168.187.180

1. Install nginx

Turn off firewall and setenforce

	systemctl stop firewalld.service
	setenforce 0

Install dependency package

	[root@localhost opt]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make 

Upload the installation package and unzip it

	cd /opt
	[root@localhost opt]# rz -E
	rz waiting to receive.
	[root@localhost opt]# ls
	nginx-1.12.0.tar(1).gz  rh
	[root@localhost opt]# tar zxf nginx-1.12.0.tar\(1\).gz 
	[root@localhost opt]# ls
	nginx-1.12.0  nginx-1.12.0.tar(1).gz  rh

Enter the directory to compile and install

	#Configuration parameter interpretation
	./configure \
	--prefix=/usr/local/nginx \
	#Installation path
	--user=nginx \
	#Specify user name
	--group=nginx \
	#Specify user groups
	--with-http_stub_status_module
	#Enable this module to support status statistics
	--with-file-aio 
	#Enable file modification support
	--with-http_gzip_static_module 
	#Enable gzip static compression
	--with-http_flv_module
	#Enable the flv module to support flv video streaming
	--with-http_ssl_module
	#Enable ssl module and provide ssl encryption function
	--without

	#Enter the directory to compile and install
	cd /opt/nginx-1.12.0
	##compile
	./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-file-aio --with-http_gzip_static_module --with-http_flv_module

	##install
	make && make install


Create a new user for management

	[root@localhost nginx-1.12.0]# useradd -M -s /sbin/nologin nginx

Establish soft link

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

Configure system

	[root@localhost nginx-1.12.0]# vim /lib/systemd/system/nginx.service


	configuration parameter

	[Unit]
	Description=nginx
	After=network.target
	[Service]
	Type=forking
	PIDFile=/usr/local/nginx/logs/nginx.pid
	ExecStart=/usr/local/nginx/sbin/nginx
	ExecReload=/usr/bin/kill -s HUP $MAINPID
	ExecStop=/usr/bin/kill -s QUIT $MAINPID
	PrivateTmp=true
	[Install]
	WantedBy=multi-user.target

Open service and view

	##If you haven't started it before, just hit this step
	systemctl daemon-reload
	
	systemctl start nginx.service
	ss -napt | grep 80

Testing nginx on a web page

2. Install and deploy tomcat

Tags: Nginx Tomcat

Posted on Tue, 16 Nov 2021 05:10:11 -0500 by jackmn1