首页 >
高性能 >
缓存技术
varnish,memcache,rsync+inotify配置
时间:2013-09-12 17:10:44 点击: 来源: 作者:
一,同步
1,Rsync篇:
yum install rsync
tar zxvf rsync-3.0.7.tar.gz
cd rsync-3.0.7
./configure --prefix=/usr/local/rsync
&make &&make install
rsync server 192.168.1.103
uid = no ...
一,同步
1,Rsync篇:
yum install rsync
tar zxvf rsync-3.0.7.tar.gz
cd rsync-3.0.7
./configure --prefix=/usr/local/rsync
&make &&make install
rsync server 192.168.1.103
uid = nobody
gid = nobody
use chroot= no
max connections = 10
strict mode = yes
pid file=/var/run/rsyncd.pid
lock file =/var/run/rsync.lock
log file=/var/log/rsyncd.log
[data]
path =/data/
comment= data file
ignore errors
read only =no
write only =no
hosts allow = *
hosts deny =192.168.12.131
list =false
uid =root
gid =root
auth users = backup
secrets file= /etc/server.pass
server.pass
chmod 600 service.pass
backup:123456
rsync click 192.168.1.102
touch /etc/rsyncd.conf
rsync --daemon
service iptables stop
rsync -vzrtopg --delete /data/ backup@192.168.1.103::backup --password-file=/etc/service.pass // 192.168.1.102 --> 192.168.1.103
2,允许防火墙某个端口:
#/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT // 允许防火墙80端口通过
3,inotify:
#!bin/sh
#Edit date by 2013-09-07 Deng
PATH=/usr/local/bin:/usr/local/sin:/usr/sbin:/bin:/sbin
export PATH
source /etc/init.d/functions
#user custom variable
src=/etc/www/mysql
dsthost=192.168.1.103
user=rsync_body //目标服务器用户名
dstmodule=mysql
passwdfile=/etc/rsync.secrets
inotify_home=/usr/local/inotify
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:$M' --format '%T %w%f%e' -e modify,delete,create,attrib $src /
| while read files
do
cd $src && rsync -zvrz -R --delete ./ --timeout=100 $user@$dsthost::${dstmodule} --password-file=${passwdfile} > /dev/null 2>&1
done
exit 0
启动:
sh -x /etc/inotify.sh
sh inotify.sh
4.inotify v2
#!bin/bash
host=192.168.1.102
src=/web/wwwroot
dst1=web1
user=backup
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,create,attrib $src /
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.
pass $src $user@$host::$dst
echo "${file} was rsynced" >>/tmp/rsync.log 2>&1
done
二,memcache篇
memcache
首先安装libevent
# wget http://www.monkey.org/~provos/libevent-1.4.13-stables.tar.gz
# tar zxvf libevent-1.4.13-stable
# cd libevent-1.4.13-stable
# ./configure
# make && make install
memcache
# wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
# tar zxvf memcached-1.4.5.tar.gz
# ./configure
# make && make insatll
启动
# /usr/local/bin/memcached -m 32m -p 11211 -d -u root -p /var/run/memcached.pid -c 256 -vv
# echo "/usr/local/bin" >> /etc/ld.so.conf
# ldconfig
三,varnish篇:
1.建立varnish用户,组和缓存目录
# useradd s /sbin/nologin varnish
# mkdir /data/varnish/cache
# mkdir /data/varnish/log
# chown -R varnish:varnish /data/varnish/cache
# chown -R varnish:varnish /data/varnish/log
2.安装pcre
首先下载
# tar zxvf pcre-7.9.tar.gz
# cd pcre-7.9
# ./configure --prefix=/usr/local/pcre
# make && make install
3.安装varnish
首先下载
# tar -zxvf varnish-2.1.2.tar.gz
# cd varnish-2.1.2
# export PKG_CONFIG_PATH=/usr/local/pcre/lib/pkgconfig //varnish查找pcre库的路径
# ./configure --prefix=/usr/local/varnish /
# make && make install
# cp redhat/varnish.initrc /etc/init.d/varnish //复制varnish守护进程的初始化脚本文件,用于varnish的启动。关闭;
# cp redhat/varnish.sysconfig /etc/sysconfig/varnish
4.vcl
默认配置文件是 /usr/local/varnish/etc/varnish/default.vcl
# touch vcl.conf //在 /usr/local/varnish/etc 中创建
# vi vcl.conf
*/以下定义4台服务器
# backend webserver1 {
.host="192.168.12.12";
.port="80";
}
backend webserver2 {
.host="192.168.12.13";
.port="80";
}
backend webserver3 {
.host="192.168.12.14";
.port="80";
}
backend webserver4 {
.host="192.168.12.15";
.port="80";
}
*/定义一个名为webserver的director ,由webserver1和webserver2两台后端服务器随机分担,weight为权值,权值高的被请求机率高
director webserver random{
{.backend=webserver1; .weight= 5;}
{.backend=webserver2; .weight= 8;}
}
*/设定purge方式清除缓存,允许“localhost,127.0.0.1,192.168.12.0的来源通过”
acl purge {
"localhost";
"127.0.0.1";
"192.168.12.0"/26;
}
*/ 发送purge请求,当url不是php,cgi时返回405错误“not allowed”,是则交给后面的服务器处理
sub vcl_recv {
if (req.request =="PURGE"){
if (!client =="purge"){
error 405 "Not allowed.";
}
elseif(req.url ~ "/.(php|cgi) ($|/?)"){
return(pass);
}
else{
return(lookup);
}
}
*/访问策略配置
if((req.httpd.host ~"^(www.|bbs.?lanjuwang.com)"&&(req.restarts ==0))
{set req.backend = webserver;
}elseif(req.restarts ==1){
set req.backend = webserver3;
if(req.http.host ~"^(img.|images.)?lanjuwang.com")
{ set req.backend = webserver4;
}
*/缓存策略配置
if(req.request !="GET" && req.request !="HEAD")
{
return (pipe);
}
else (req.url ~"/.(cgi|php)($|/?)")
{return(pass);
}
elseif (req.httpd.Authenticate || req.httpd.Authorization)
{return (pass);
}
return(lookup);
}
*/如果请求的类型是purge方法,varnishd会将缓存周期设为0,即缓存失效,从而达到刷新目的。
sub val_hit
{
if (req.request == "PURGE"){
set obj.ttl =0s;
error 200 "Purged.";
}
if(!obj.cacheable)
{return(pass);
}
if (obj.http.Vary)
{ unset obj.http.Vary;
}
}
sub vcl_miss
{
if (req.request == "PURGE"){
ERROR 404 "Not in cache.";
}
}
*/定义 hash 的值,并且处理压缩内容
sub vcl_hash {
set req.hash += req.url;
if (req.http.host){
set req.hash += req.http.host;
}
else{
set req.hash += server.ip;
}
if (req.http.Accept-Encoding){
if (req.url ~"/(jpg|jpeg|png|gif|rar|zip|gz|tgz|bz2|tbz|mp3|ogg|swf|exe|flv|avi|rmvb|rm|mpg|mpeg|pdf)$"){}
else {set req.hash += req.httpd.Accept-Encoding;
}
}
return (hash);
}
sub vcl_fetch
{
if (!beresp.cacheable){
return (pass);
}
if (beresp.http.Set-cookie){
return (pass);
}
if (beresp.status == 500 || beresp.status == 501 || beresp.status == 502 ||
beresp.status == 503 || beresp.status == 504 || beresp.status == 404)
{
return (restart);
}
*/定义不缓存的http请求
if (beresp.http.Pragma ~ "no-cache" || beresp.http.Cache-Control ~ "no-Cache" || beresp.http.Cache-Control ~ "private")
{return (pass);
}
if (req.request == "GET" && req.url ~"./(css|js|html|htm)$")
{
set beresp.ttl =300s;
}
if (req.request == "GET" && req.url ~"./(gif|jpeg|jpg|png|ico|img|wmf|bmp|tif|tiff)$")
{
set beresp.ttl =3600s;
}
if (req.request == "GET" && req.url ~"./(svg|swf|ico|mp3)$")
{
set beresp.ttl =10d;
}
return (deliver);
}
sub vcl_deliver {
set resp.http.X-Cache ="HIT from www.lanjuwang.com";
} else {
set resp.http.X-Cache ="MISS from www.lanjuwang.com";
}
return (deliver);
}
5.启动
# usr/local/varnish/sbin/varnishd
6.修改Varnish运行脚本
1.>
# vi /etc/sysconfig/varnish
# NFILES=131072
MEMLOCK=82000
DAEMON_OPTS="-A 192.168.12.246:80 /
-T 127.0.0.1:3500 /
-f /usr/local/varnish/etc/vcl.conf /
-u varnish -g varnish /
-w 2,51200,10 /
-n /data/varnish/cache /
-s file, /data/varnish/cache/varnish_cache.data,4G"
# vi /etc/init.d/varnish
exec="/usr/local/varnish/sbin/varnishd"
prog="varnishd"
config="/etc/sysconfig/varnishd" //指定varnishd守护进程路径
lockfile="/var/lock/subsys/varnish"
7.授权,运行脚本
# chmod 755 /etc/init.d/varnish
# /etc/init.d/varnish
Usage:/etc/init.d/varnish
{start|stop|status|restart|condrestart|try-restart|reload|force-reload}
# /etc/init.d/varnish start
四.
”您可通过以下微信二维码,赞赏作者“