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

Experimental Environment and configuration process of Nginx+Tomcat load balancing Cluster

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The following brings you the experimental environment and configuration process of Nginx+Tomcat Load Balancer cluster. I hope it can bring some help to you in practical application. Load Balancer involves many things, but there are not many theories. There are many books on the Internet. Today, we will use the accumulated experience in the industry to make an answer.

####Experimental environment centos 6.5

####Required installation packages

jdk-7u65-linux-x64.gz

apache-tomcat-7.0.54.tar.gz

nginx-1.6.0.tar.gz

Two tomcat Cloud Virtual Machine and one Nginx Server

tomcat1 ip 192.168.1.100: 8080

Tomcat2 IP 192.168.1.101: 8080

Nginx ip 192.168.1.102

Note: firewall, selinux and gcc environment must be turned off during the test

==========================

(1) Install JDK and configure JAVA environment

[root@localhost ~]# tar zxvf jdk-7u65-linux-x64.gz

[root@localhost ~]# mv jdk1.7.0_65/ /usr/local/java

[root@localhost ~]# vim /etc/profile.d/java.sh #Write a script under this path that reads as follows

export JAVA_HOME=/usr/local/java

export PATH=$PATH:$JAVA_HOME/bin

[root@localhost ~]# source /etc/profile.d/java.sh

(2) Installation Configuration Tomcat

[root@localhost ~]# tar xf apache-tomcat-7.0.54.tar.gz

[root@localhost ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat7

[root@localhost ~]# /usr/local/tomcat7/bin/startup.sh #Start tomcat as follows

Using CATALINA_BASE: /usr/local/tomcat7

Using CATALINA_HOME: /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME: /usr/local/java

Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

Tomcat started.

You have new mail in /var/spool/mail/root

(3) Open the browser to access the test, http://192.168.1.100: 8080, and the following interface will appear.

(4) Creating Java Web Site

[root@localhost ~]# mkdir -pv /web/webapp1 #Create a directory of web pages

[root@localhost ~]#vim/usr/local/tomcat7/conf/server.xml #Modify the configuration file, specify the path, add the content of the ××× zone

It reads as follows:

[root@localhost ~]#vim/web/webapp1/index.jsp #Add Test Page

It reads as follows:

[root@localhost java]# cat /web/webapp1/index.jsp

JSP test1 page

(5) Successful verification test

Open the browser, enter http://192.168.1.100:8080, and the following will appear

=============================================================

Tomcat2 configuration method is basically the same as tomcat1, including

(1) Turn off iptables firewall

(2) Install JDK, configure JAVA environment, version consistent with Tomcat1 server

(3) Installation configuration Tomcat, version consistent with Tomcat1 server

(4) Create/web/webapp1 directory, modify Tomcat configuration file server.xml, change website file directory to/web/webapp1/path

(5) Create Index.jsp under/web/webapp1/path, change the contents of index.jsp to distinguish the test page as follows

[root@localhost ~]# cat /web/webapp1/index.jsp

JSP test2 page

=================

[root@localhost table]# yum -y install prce-devel zlib-devel openssl-devel

[root@localhost desktop]# groupadd www

[root@localhost table]# useradd -g www -s /bin/false

root@localhost desktop]# mv nginx-1.6.0.tar.gz ~

[root@localhost Desktop]# cd ~

[root@localhost ~]# tar zxvf nginx-1.6.0.tar.gz

[root@localhost ~]# cd nginx-1.6.0

[root@localhost nginx-1.6.0]# ./ configure --prefix=/usr/local/nginx --user=www --group=www --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module

[root@localhost nginx-1.6.0]# make && make install

Edit the configuration file after installation (the front is the number of rows in the configuration file, the red part is what is added)

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf

32 #gzip on;

33 upstream tomcat_server {

34 server 192.168.1.100:8080 weight=1;

35 server 192.168.1.101:8080 weight=2;

36

37 }

43

44 #access_log logs/host.access.log main;

45

46 location / {

47 root html;

48 index index.html index.htm;

49 proxy_pass http://tomcat_server;

50 }

51

52 #error_page 404 /404.html;

In order to start conveniently write an Nginx script and put it in the environment variable, the script content is as follows

[root@localhost init.d]# vim /etc/init.d/nginx

#!/ bin/bash#chkconfig: - 99 20#description: Nginx Server Control ScriptPROG="/usr/local/nginx/sbin/nginx"PIDF="/usr/local/nginx/logs/nginx.pid"case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 (start|stop|restart|reload)" exit 1esacexit 0

[root@localhost init.d]# echo "PATH=$PATH:/etc/init.d/" >> /etc/profile

[root@localhost init.d]# . /etc/profile

[root@localhost init.d]# nginx restart #Restart Nginx

Test the correct configuration file

[root@localhost init.d]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Restart nginx

[root@localhost init.d]# nginx restart

[root@localhost init.d]# netstat -anpt | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 40041/nginx

Testing Load Balancer Effects

Open your browser and type 192.168.1.102

Refreshing the browser test repeatedly found that the weight is the same, the page will repeatedly switch back and forth between the two pages.

Note: If the virtual machine is transferred to the same wmnat, and the configured ip address is permanent, try not to configure temporary error-prone

After reading the above experimental environment and configuration process of Nginx+Tomcat Load Balancer cluster, if you still need to know anything, you can find what you are interested in the industry information or find our professional technical engineers to answer. Technical engineers have more than ten years of experience in the industry.

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