Xiaobai also understands the load balancing of Nginx+Tomcat, the practical operation of separation of dynamics and statics
One: achieve dynamic and static separation
1.1: Practical environment
- VMware software
- A centos7 serves as the nginx proxy server. IP address: 20.0.0.51
- A centos7 serves as the Tomcat1 node server. IP address: 20.0.0.52
- A centos7 serves as the Tomcat2 node server. IP address: 20.0.0.48
1.2: Experimental purpose
- By accessing the nginx proxy server, statically handle it by itself, and dynamically hand it over to Tomcat for processing
1.3: Experimental steps
1.3.1: Build nginx service
- Turn off the firewall
[root@nginx ~]# systemctl stop firewalld.service
[root@nginx ~]# setenforce 0
- Installation Environment
[root@nginx ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make '安装环境'
[root@nginx ~]# useradd -M -s /sbin/nologin nginx '创建没有家目录,不允许登陆的nginx用户'
- Unzip the nginx source package and compile and install
[root@nginx ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@nginx ~]# cd nginx-1.12.0/
[root@nginx nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@nginx nginx-1.12.2]# make && make install
- Create a soft link and check if the syntax is correct
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@nginx nginx-1.12.2]# nginx -t
- Make nginx startup script
[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx '添加使用systemctl工具进行管理'
#!/bin/bash
# chkconfig:- 99 20
# description:Nginx Service Control Script
PROG="/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 1
esac
exit 0
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@nginx nginx-1.12.2]# chkconfig --add nginx
- Start nginx service
[root@nginx nginx-1.12.2]# netstat -ntap |grep 80
[root@nginx nginx-1.12.2]# service nginx start
[root@nginx nginx-1.12.2]# netstat -ntap |grep 80
tcp6 0 0 :::8009 :::* LISTEN 14111/java
tcp6 0 0 :::8080 :::* LISTEN 14111/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 14111/java
1.3.2: Build Tomcat service
- Turn off firewall and core protection
[root@tomcat1 ~]# systemctl stop firewalld.service
[root@tomcat1 ~]# setenforce 0
- Unzip the prepared jdk
[root@tomcat1 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
- Configure jdk environment
[root@tomcat1 ~]# vim /etc/profile '编辑环境'
'在最后加入以下内容'
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${
JAVA_HOME}/jre
export CLASSPATH=.:${
JAVA_HOME}/lib:${
JRE_HOME}/lib
export PATH=${
JAVA_HOME}/bin:$PATH
[root@tomcat1 ~]# source /etc/profile '立即生效,引用环境'
- Install Tomcat
[root@tomcat1 ~]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/ '将安装包移动到/usr/local下'
[root@tomcat1 ~]# cd /usr/local/
[root@tomcat1 local]# mv apache-tomcat-8.5.16/ tomcat '重命名'
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin
[root@tomcat1 local]# startup.sh '启动Tomcat'
1.3.3: Dynamic and static separation configuration
- Operation on nginx
- Operate and edit the configuration file nginx.conf in the nginx server
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
'在server中添加'
server {
..省略
'添加以下内容'
location ~.*.jsp$ {
'做跳转'
proxy_pass http://20.0.0.52:8080;
proxy_set_header Host $host;
}
[root@nginx ~]# nginx -t
- Create a static page
[root@nginx ~]# vim /usr/local/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>静态页面</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>静态页面</h1>
<p>这是一个静态页面</p>
</body>
</html>
[root@nginx ~]# service nginx stop
[root@nginx ~]# service nginx start
- Operation on tomcat
- Create dynamic pages
[root@tomcat1 ~]# mkdir /usr/local/tomcat/webapps/test
[root@tomcat1 ~]# vim /usr/local/tomcat/webapps/test/index.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>动态页面</title>
</head>
<body>
<div>动态页面</div>
</body>
</html>
- Access test
1.3.4: nginx handles static images, Tomcat handles dynamic pages
- tomcat service operation
[root@tomcat1 local]# vim /usr/local/tomcat/webapps/test/index.jsp
'在<div>动态页面</div>下面添加'
<img src="chiji.jpg">
- Nginx service operation
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
'在server中添加'
server {
..省略
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root html;
expires 30d;
}
(Tomcat指路径,nginx放图片)
注意:目录名称需要和Java项目名称相同
[root@nginx ~]# cd /usr/local/nginx/html/test
[root@nginx test]# rz -E
rz waiting to receive.
[root@nginx test]# ls
chiji.jpg
[root@nginx test]# service nginx restart
- test
Two: load balancing
2.1: Experimental environment
Based on the previous experimental environment, there is also a centos7: 20.0.0.48 is useless
First install tomcat according to the same steps as tomcat1
2.2: Experimental purpose
- Realize load balancing by accessing nginx proxy server
2.3: Experimental steps
- Perform page test
[root@tomcat2 local]# cd tomcat/
[root@tomcat2 tomcat]# mkdir -pv /web/webapp1 创建web目录
[root@tomcat2 tomcat]# vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("Welcome KEVIN Web");%>
</body>
</html>
'在Tomcat1中将KEVIN换成BENET,做一个区分'
- Modify the configuration file of tomcat
vim /usr/local/tomcat/conf/server.xml
'148行后添加以下内容'
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
'注解:docBASE:web应用的文档基准目录'
'reloadable设置简史“类”是否变化'
'path=""设置默认“类”'
- Start tomcat service
[root@tomcat2 tomcat]# shutdown.sh
[root@tomcat2 tomcat]#startup.sh
- test
- Configure dynamic and static separation on nginx, modify the main configuration file
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
...省略内容
#gzip on;
'添加'
upstream tomcat_server {
'添加节点服务器地址'
server 20.0.0.52:8080 weight=1;
server 20.0.0.48:8080 weight=1;
}
...省略内容
'location 里添加'
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server; ’添加,设置转发到节点服务器‘
}
- Check the syntax and start
[root@nginx html]# /usr/local/nginx/sbin/nginx -t
[root@nginx html]# service nginx stop
[root@nginx html]# service nginx start
- Cluster test