實現nginx和tomcat動靜頁面分離php
公司內部須要搭建web集羣,而且網站有靜態頁面和.jsp(java語言開發)的頁面,現要求分別搭建處理這兩種頁面的不一樣服務到web羣集中,而且實現調度器的備份,保證調度器的穩定;html
服務器名稱前端 |
IP地址java |
所需軟件linux |
Tomcat1nginx |
192.168.100.154web |
jdk-7u65-linux-x64.gzapache apache-tomcat-7.0.54.tar.gzvim |
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實現動靜頁面分離