table of Contents
1. Introduction to Tomcat and Nginx
1.1 Tomcat important directory
bin: 存放启动Tomcat和关闭Tomcat脚本
conf:存放Tomcat不同的配置文件
doc:存放Tomcat文档
lib:存放Tomcat运行需要的库文件
logs:存放tomcat执行时的LOG 文件
src: 存在Tomcat的源代码
webapps:Tomcat的主要Web发布目录
work: 存放jsp编译后产生的class文件
1.2 Nginx
- Nginx is a very good HTTP server software,
supports up to 50,000 concurrent connections, and
has strong static resource processing capabilities,
stable operation
, and very low consumption of system resources such as memory and CPU
At present, many large websites use Nginx server as a reverse proxy and load balancer for back-end website programs to improve the load capacity of the entire site
1.3 Nginx realizes the principle of load balancing and dynamic separation
Nginx realizes load balancing through reverse proxy
- upstream service pool name{} ## Configure the backend server pool to provide response data
- proxy_pass http://服务池名## Configure the server that forwards the access request to the back-end server pool to handle the
principle of dynamic and static separation - In the server receiving the client's request, there are both static resources and dynamic resources. The static resources are served by Nginx, and the dynamic resources are forwarded by Nginx to the backend.
1.4 Nginx dynamic and static separation processing advantages
- Nginx's efficiency in processing static pages is much higher than that of Tomcat
- If Tomcat requests 1000 times, Nginx requests 6000 times
- The throughput of Tomcat is 0.6M per second, and the throughput of Nginx is 3.6M per second
- Nginx's ability to handle static resources is 6 times that of Tomcat
Two, Nginx load balancing, dynamic and static separation
- Preparation
[root@nginx ~]# systemctl stop firewalld.service
[root@nginx ~]# setenforce 0
Three CentOS 7 virtual machines, one as Nginx server, and two as Tomcat server
nginx: 192.168233.200 tomcat1
: 192.168.233.180
tomcat2: 192.168.233.50
2.1 Dynamic and static separation
2.1.1 Install and optimize Nginx service
- Prepare the Nginx installation package nginx-1.12.0.tar
[root@nginx ~]# useradd -s /sbin/nologin -M nginx
[root@nginx ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ pcre make
[root@nginx ~]# tar zxvf nginx-1.12.0.tar.gz
[root@nginx ~]# cd nginx-1.12.0/
[root@nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx nginx-1.12.0]# make && make install
[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.0]# vim /etc/init.d/nginx
#!/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.0]# chmod +x /etc/init.d/nginx
[root@nginx nginx-1.12.0]# chkconfig --add nginx
[root@nginx nginx-1.12.0]# service nginx start
[root@nginx nginx-1.12.0]# netstat -atnp | grep "nginx"
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22591/nginx: master
2.1.2 Tomcat deployment
- Prepare jdk-8u91-linux-x64.tar apache-tomcat-8.5.16.tar installation package
[root@tomcat1 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[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
[root@tomcat1 ~]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /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
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.8.0_91/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
2.1.3 The realization of dynamic and static separation
- Operation on Nginx server
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
## 在server下 location / { 上面插入下列代码
location ~.*.jsp {
proxy_pass http://192.168.233.180:8080;
proxy_set_header Host $Host;
}
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>
Restart configuration
[root@nginx ~]# 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
[root@nginx ~]# service nginx stop
[root@nginx ~]# service nginx start
- Operation on tomcat1
[root@tomcat1 ~]# mkdir /usr/local/tomcat/webapps/test
[root@tomcat1 test]# 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>
~
[root@tomcat1 ~]# shutdown.sh
[root@tomcat1 ~]# startup.sh
2.1.4 nginx processes static images, and Tomcat processes dynamic pages
- Operation on tomcat
<body>
<div>动态页面</div><br><img src="jin.jpg">
</body>
- nginx operation
location ~.*\.(gif|jpg|jpeg|png|bmg|swf|css)$ {
root html;
expires 30d;
}
Tomcat refers to the path, nginx puts pictures
[root@nginx ~]# mkdir /usr/local/nginx/html/test
[root@nginx ~]# cd /usr/local/nginx/html/test/ ## 注意 html文件名一定要是test 需要与tomcat那边的文件名对应
[root@nginx test]# ls
jin.jpg
[root@nginx ~]# service nginx restart
2.1.5 Test
Visit http:192.168.233.200/test/index.jsp
2.2 Nginx load balancing
Both tomcat have to be made
—————— Join the test page
[root@tomcat2 ~]# mkdir -pv /web/webapp1
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/webapp1"
[root@tomcat2 ~]# vim /web/webapp1/index.jsp ## 为了方便测试 两个tomcat页面内容写不一样的
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<% out.println("Welcome Tomcat2 Web");%>
</body>
</html>
[root@tomcat2 ~]# vim /usr/local/tomcat/conf/server.xml
150 <Context docBase="/web/webapp1" path="" reloadable="false">
151 </Context>
152
## docBase : web应用的文档基准目录
reloadable: 设置监视“类”是否变化
path=""“设置默认"类"
[root@tomcat2 ~]# shutdown.sh ## 修改完重启服务
[root@tomcat2 ~]# startup.sh
- Nginx modification
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip on;
upstream tomcat_server {
server 192.168.233.180:8080 weight=1;
server 192.168.233.50:8080 weight=1;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server;
}
- Restart Nginx service
[root@nginx ~]# 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
[root@nginx ~]# service nginx stop
[root@nginx ~]# service nginx start
2.2.1 Test
Access the Nginx server address 192.168.233.200