实现nginx和tomcat动静页面分离
实验拓扑:
公司内部需要搭建web集群,并且网站有静态页面和.jsp(java语言开发)的页面,现要求分别搭建处理这两种页面的不同服务到web群集中,并且实现调度器的备份,保证调度器的稳定;
实验环境:
服务器名称 | IP地址 | 所需软件 |
Tomcat1 | 192.168.100.154 | jdk-7u65-linux-x64.gz apache-tomcat-7.0.54.tar.gz |
Tomcat2 | 192.168.100.155 | jdk-7u65-linux-x64.gz apache-tomcat-7.0.54.tar.gz |
Nginx1 | 192.168.100.156 | nginx-1.6.0.tar.gz |
Nginx2 | 192.168.100.157 | nginx-1.6.0.tar.gz |
主调度器 | 192.168.100.158 | keepalived-1.2.13.tar.gz |
从调度器 | 192.168.100.159 | keepalived-1.2.13.tar.gz |
调度器集群IP地址(vip地址) | 192.168.100.95 |
实验思路:
在此项目中,首先一个大的框架为lvs DR模式+keealived双机热备,搭建了web集群,从而保证了调度器的稳定以及网站的负载,那么若在集群中实现动态页面的分离,则需要使用不同的服务器在集群中,使用两台nginx做web集群的前端,负责静态页面的处理和动态页面的代理,两台tomcat做web集群的后端,负责动态页面的处理。
实验重点:
1.前端lvs的节点只有两台nginx服务器,不包括tomcat服务器,nginx负责将动态页面的请求代理到两台tomcat服务器上,实现动态页面的负载;
2.在nginx服务器中,需要指定不同页面的请求到不同的服务器(静态到本身、动态到定义的负载均衡服务器列表中,也就是tomcat服务器);
实验步骤:
1.分别在后端tomcat主机:192.168.100.154-155
[root@localhost ~]# vi install_tomcat.sh
#!/bin/bash
##by linuxfan 2015.1.9
#################下载并解压#####################
wgetftp://ftp.linuxfan.cn/tools/jdk-7u65-linux-x64.gz -P /usr/src/
wgetftp://ftp.linuxfan.cn/tools/apache-tomcat-7.0.54.tar.gz -P /usr/src/
tar zxvf /usr/src/jdk-7u65-linux-x64.gz -C/usr/src/
tar zxvf/usr/src/apache-tomcat-7.0.54.tar.gz -C /usr/src/
###############config java env###########
yum -y remove java
mv /usr/src/jdk1.7.0_65/ /usr/local/java
cat <<END >>/etc/profile ##设置JAVA的环境变量
export JAVA_HOME=/usr/local/java
export PATH=\$PATH:\$JAVA_HOME/bin
END
source /etc/profile
java -version
####################config tomcat7############
mv /usr/src/apache-tomcat-7.0.54/usr/local/tomcat7
reboot
:wq
等待重启后,启动服务:
[root@localhost ~]#/usr/local/tomcat7/bin/startup.sh &&netstat -utpln |grep 8080
编辑jsp网页内容;在此两台tomcat的内容不一致,以便测试
mkdir -pv /web/webapp/
vi /usr/local/tomcat7/conf/server.xml
<Host name="localhost" appBase="webapps" ##设置虚拟主机
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/webapp"path="" reloadable="false"></Context> ##s设置web应用的文档基准目录
编写测试页面:
[root@localhost ~]# cat /web/webapp/index.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP TEST PAGE1 </title>
</head>
<body>
<% out.println("Welcome to testsite;http://www.test1.com");%>
</body>
</html>
重启tomcat
[root@localhost ~]#/usr/local/tomcat7/bin/shutdown.sh
[root@localhost ~]#/usr/local/tomcat7/bin/startup.sh
2.安装前端nginx:192.168.100.156-157
cd /etc/sysconfig/network-scripts/ ##因为调度器部署了lvs+keepalived,所以在lvs的节点中需要调整网卡
cp ifcfg-lo ifcfg-lo:0
vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.95
NETMASK=255.255.255.255
ONBOOT=yes
:wq
/etc/init.d/network restart
echo "route add -host 192.168.100.95dev lo:0" >>/etc/rc.local
route add -host 192.168.100.95 dev lo:0 ##添加vip地址本地访问路由
ip addr show dev lo
vi /etc/sysctl.conf ##调整响应参数
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
sysctl -p
yum -y install pcre-devel zlib-developenssl-devel
wgetftp.linuxfan.cn:/tools/nginx-1.6.0.tar.gz
tar zxvf nginx-1.6.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.6.0/
useradd -s /bin/false nginx
./configure --prefix=/usr/local/nginx--user=nginx --group=nginx --with-file-aio --with-http_stub_status_module \
--with-http_gzip_static_module--with-http_flv_module --with-http_ssl_module &&make &&make install
cd /usr/local/nginx/conf
vim nginx.conf ##重点,在nginx中做反向代理,指向后端的tomcat服务器
upstream tomcat_server { ##在http{}中 ##首先指定负载均衡服务器列表,后端tomcat
server192.168.100.154:8080 weight=1;
server192.168.100.155:8080 weight=1;
}
location / { ##在server{ 中 ##配置文件中默认存在,不删除
root html;
index index.html index.htm;
}
location ~\.(gif|jpg|jpeg|png|bmp|swf)$ { ##指定若是.gif等静态网页默认找本机
root html;
}
location ~\.(asp|aspx|php|jsp|do)$ { ##指定若是.asp等动态网页找上边定义的负载均衡服务器列表
proxy_pass http://tomcat_server;
}
:wq
/usr/local/nginx/sbin/nginx -c/usr/local/nginx/conf/nginx.conf 启动服务
netstat -utpln |grep 80
部署nginx静态页面,两台服务器的页面需要不一致,以便测试;
vi /usr/local/nginx/html/index.html
192.168.100.156
:wq
[root@www ~]# vi /etc/init.d/nginx ##编辑nginx的控制文件
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$NP;
if [ $? -eq 0 ]
then
echo "nginx is starting!! "
fi
;;
stop)
kill -s QUIT $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx is stopping!! "
fi
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx config file is reload! "
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@www ~]# chmod +x /etc/init.d/nginx
[root@www ~]# chkconfig --add nginx
[root@www ~]# /etc/init.d/nginx stop
nginx is stopping!!
[root@www ~]# /etc/init.d/nginx start
nginx is starting!!
[root@www ~]# /etc/init.d/nginx reload
nginx config file is reload!
3.前端lvs两台调度服务器:192.168.100.158-159
yum -y install kernel-devel openssl-devel popt-develipvsadm
lftp ftp.linuxfan.cn
cd tools/
get keepalived-1.2.13.tar.gz
bye
tar zxvf keepalived-1.2.13.tar.gz -C/usr/src/
cd /usr/src/keepalived-1.2.13/
./configure --prefix=/--with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
make &&make install
cd
[root@localhost ~]# ls /etc/keepalived/
keepalived.conf samples
chkconfig --add keepalived
chkconfig keepalived on
1)、配置主HA调度服务器:192.168.100.158
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak
vi /etc/keepalived/keepalived.conf ##编辑keepalived配置文件
global_defs {
router_id HA_TEST_R1 ##本服务器的名称
}
vrrp_instance VI_1 { ##定义VRRP热备实例
state MASTER ##MASTER表示主服务器
interface eth0 ##承载VIP地址的物理接口
virtual_router_id 1 ##虚拟路由器的ID号
priority 100 ##优先级,数值越大优先级越高
advert_int 1 ##通告间隔秒数(心跳频率)
authentication { ##认证信息
auth_type PASS ##认证类型
auth_pass 123456 ##密码字串
}
virtual_ipaddress {
192.168.100.95 ##指定漂移地址(VIP)
}
virtual_server 192.168.100.95 80 { ##指定vip地址
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.100.156 80 { ##指定web集群节点1,在此为nginx1
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.100.157 80 { ##指定web集群节点2,在此为nginx2
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
:wq
modprobe ip_vs ##启动ip_vs模块
lsmod |grep ip_vs
echo "modprobe ip_vs">>/etc/rc.local
/etc/init.d/keepalived restart
chkconfig ipvsadm off ##因为有了keepalived,所以设置ipvsadm模块开机不启动
ip addr show dev eth0
2)、配置HA从调度服务器:192.168.100.159
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak
vi /etc/keepalived/keepalived.conf
global_defs {
router_id HA_TEST_R2 ##本服务器的名称
}
vrrp_instance VI_1 { ##定义VRRP热备实例
state BACKUP ##BACKUP表示主服务器
interface eth0 ##承载VIP地址的物理接口
virtual_router_id 1 ##虚拟路由器的ID号
priority 99 ##优先级,数值越大优先级越高
advert_int 1 ##通告间隔秒数(心跳频率)
authentication { ##认证信息
auth_type PASS ##认证类型
auth_pass 123456 ##密码字串
}
virtual_ipaddress {
192.168.100.95 ##指定漂移地址(VIP)
}
virtual_server 192.168.100.95 80 {
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.100.156 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.100.157 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
:wq
modprobe ip_vs
lsmod |grep ip_vs
echo "modprobe ip_vs">>/etc/rc.local
/etc/init.d/keepalived restart
chkconfig ipvsadm off
ip addr show dev eth0
4.客户端测试:
实验拓展:
http://blog.csdn.net/alli0968/article/details/47950481 nginx+tomcat实现动静页面分离