一、关于动静分离和负载均衡的介绍
Nginx+Tomcat的动静分离:
所谓的动静分离就是通过nginx(或apache等)来处理用户端请求的图片、html等静态的文件,tomcat(或weblogic)处理jsp、do等动态文件,从而达到动静页面访问时通过不同的容器来处理。nginx处理静态页面效率远高于tomcat,而tomcat擅长动态页面处理,这样一来就能更好的提高并发,处理性能。
Nginx+Tomcat的负载均衡:
在服务器集群中,Nginx起到一个代理服务器的角色(即反向代理),为了避免单独一个服务器压力过大,将来自用户的请求转发给不同的服务器,tomcat负责处理nginx转发的用户请求
二、具体步骤
1、环境介绍
nginx 192.168.28.134
tomcat1 192.168.28.131
tomcat2 192.168.28.132
client win7系统
目的:当访问服务器时,静态页面由nginx服务器处理,动态页面由tomcat处理
软件下载:
nginx官网下载:http://nginx.org/en/download.html
jdk官网下载地址(jdk提供tomcat运行环境):
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
tomcat配置:
安装tomcat(两台都需要配置)
#rpm安装jdk
rpm -ivh jdk-8u201-linux-x64.rpm
#环境
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
tar zxvf apache-tomcat-9.0.16.tar.gz -C /usr/local/
cd /usr/local
mv apache-tomcat-9.0.16/ tomcat
cd /usr/local/tomcat/bin
ln -s /usr/local/tomcat/bin/startup.sh /usr/bin/
#创建tomcat启动脚本的软链接到系统目录中,方便系统识别
ln -s /usr/local/tomcat/bin/shutdown.sh /usr/bin/
#创建tomcat关闭脚本的软链接到系统目录中,方便系统识别
startup.sh
systemctl stop firewalld.service
setenforce 0
netstat -ntap | grep 8080
安装完成jdk之后,加入测试页面(tomcat1配置)
mkdir -pv /web/webapp1
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 to test site,http://www.test1.com");%>
</body>
</html>
vim /usr/local/tomcat/conf/server.xml
<Host name="licalhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
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>this is jsp</title>
</head>
<body>
<div>this is jsp</div><br><img src="game.jpg">
</body>
</html>
shutdown.sh
startup.sh
通过客户机访问:http://192.168.28.131:8080
加入测试页面(tomcat2配置)
mkdir -pv /web/webapp1
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 to test site,http://www.test1.com");%>
</body>
</html>
vim /usr/local/tomcat/conf/server.xml
<Host name="licalhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
vim /usr/local/tomcat/wedapps/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>this is page</title>
</head>
<body>
<div>this is page</div><br><img src="aaa.jpg">
</body>
</html>
shutdown.sh
startup.sh
通过客户机访问:http://192.168.28.132:8080
(Tomcat指路径,nginx放图片)
注意:目录名称需要和java项目名称相同
mkdir /usr/local/nginx/html/test #创建文件夹
Nginx安装及配置:
yum install pcre-devel zlib-devel openssl-devel gcc gcc-c++ -y
tar zxvf nginx-1.12.0.tar.gz
useradd -s /bin/false www
cd nginx-1.12.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
make && make install
vim /usr/local/nginx/html/index.html
#静态页面的配置
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>this is static page</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>this is static page</h1>
<p><em>this is static page</em></p>
</body>
</html>
vim /usr/local/nginx/conf/nginx.conf
http {
......
#负载均衡
upstream tomcat {
server 192.168.28.131:8080 weight=1;
server 192.168.28.132:8080 weight=1;
}
server {
......
#静态文件交给nginx处理
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root html;
expires 30d;
}
#jsp页面交由Tomcat进行处理
location ~ .*.jsp$ {
proxy_set_header Host $host;
proxy_pass http://tomcat;
}
......
}
}
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx
netstat -ntap | grep nginx
systemctl stop firewalld.service
setenforce 0
客户机访问(静态)http://192.168.28.134
客户机访问(动态)http://192.168.28.134/test/index.jsp
tomcat1的动态页面
tomcat2的动态页面(点击刷新会两种之间切换)