nginx+ Keepalived+proxy_cache for Nginx high availability and cache
Environment:
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 installs Web services in advance.
# 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:
First, install Nginx
Installation dependencies
# 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
Second, the configuration file modification
# 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 302 301 304 10m;
}
}
Startup scripts
# 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 "$" 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
Third, testing
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/01/wKiom1Vwc8jgVajvAABadu9AL98683.jpg "title=" QQ picture 20150604235027.jpg "alt=" Wkiom1vwc8jgvajvaabadu9al98683.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6D/FD/wKioL1VwdeLTReS1AABO08gusps156.jpg "title=" QQ picture 20150604235212.jpg "alt=" Wkiol1vwdeltres1aabo08gusps156.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/01/wKiom1VwdIeg42DIAAJ-S1_baG4233.jpg "title=" QQ picture 20150604235345.jpg "alt=" Wkiom1vwdieg42diaaj-s1_bag4233.jpg "/>
Four, install the configuration keepalived (here with Yum installation)
# yum Install-y keepalived
# vim/etc/keepalived/keepalived.conf
! Configuration File for Keepalived
Global_defs {
Notification_email {
# [Email protected]
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
router_id Lvs_devel1 #全局唯一, slave on need to modify
}
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 51
Priority #slave需修改
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
10.10.0.220/12 Dev eth0 label eth0:0
}
}
V. Open the service and start the boot
# service Keepalived Start
# Chkconfig keepalived on
# chkconfig Nginx on
VI. Testing
This article is from the "ngames" blog, make sure to keep this source http://ngames.blog.51cto.com/3187187/1658759
nginx+ Keepalived+proxy_cache for Nginx high availability and cache