nginx+ keepalived+proxy_cache实现nginx高可用及高速缓存
nginx+ keepalived+proxy_cache实现nginx高可用及高速缓存
环境:
master: 10.10.0.224
slave: 10.10.0.225
real server 1: 10.10.0.221
real server 2: 10.10.0.226
vip: 10.10.0.220
real server 事先安装好web服务
# yum install httpd -y
# cd /var/www/html
# touch test.html
# echo "10.10.0.221" > test.html # real server1
# echo "10.10.0.226" > test.html # real server2
Master/slave:
一、安装nginx
安装依赖
# yum install -y pcre pcre-devel gd gd-devel
# tar zcvf nginx-1.9.1.tar.gz
# cd nginx-1.9.1
# ./configure --prefix=/etc/nginx --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre
# make
# make install
二、配置文件修改
# cd /etc/nginx/conf
# vim nginx
#负载均衡
upstream webs {
server 10.10.0.221 weight=2;
server 10.10.0.226 weight=1 max_fails=2 fail_timeout=2;
}
#设置缓存
proxy_cache_path /data/cache levels=1:2 keys_zone=one:20m max_size=1g;
server {
listen 80;
server_name www.a.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://webs;
add_header X-Via-IP $server_addr;
add_header X-Cache-Status $upstream_cache_status;
proxy_cache one;
proxy_cache_valid 200 302 301 304 10m;
}
}
启动脚本
# vim /etc/init.d/nginx
#!/bin/bash
# it is nginx
# chkconfig: - 85 15
# discription: nginx is a high-performance web and proxy server
# date: 2015-6-2
nginxd=/etc/nginx/sbin/nginx
nginx_config=/etc/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ ${NETWORKING} = no ] && exit 0
[ -x $ngind ] || exit 0
start() {
if [ -e $nginx_pid ]
then
echo "nginx already running..."
exit 1
fi
echo -n $"staring $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
stop() {
echo -n $"stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
reload() {
echo -n $"reloading $prog: "
killproc $nginxd -HUP
RETVAL=$?
echo
}
case "$1" in
start)
start;;
stop)
stop;;
reload)
reload;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?;;
*)
echo $"Usage:$prog{start|stop|status|reload|restart|help}"
exit 1;;
esac
exit $RETVAL
# chmod +x /etc/init.d/nginx
# service nginx restart
三、测试
四、安装配置keepalived(这里采用yum安装)
# yum install -y keepalived
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL1 #全局唯一,slave上需修改
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100 #slave需修改
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.0.220/12 dev eth0 label eth0:0
}
}
五、开启服务,并开机启动
# service keepalived start
# chkconfig keepalived on
# chkconfig nginx on
六、测试
本文出自 “ngames” 博客,请务必保留此出处http://ngames.blog.51cto.com/3187187/1658759
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。