首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Varnish缓存的配备优化

2012-12-15 
Varnish缓存的配置优化? ? ? ? ? ? {? ? ? ? ? ? ? unset req.http.cookie? ? ? ? ? ? ?} ??? ? ? ? #判

Varnish缓存的配置优化
? ? ? ? ? ? {? ? ? ? ? ? ? unset req.http.cookie;? ? ? ? ? ? ?} ??? ? ? ? #判断req.http.X-Forwarded-For 如果前端有多重反向代理,这样可以获取客户端IP地址。? ? ? ? if (req.http.x-forwarded-for)? ? ? ? ? ?{? ? ? ? ? ? ? set req.http.X-Forwarded-For = req.http.X-Forwarded-For ", " client.ip;? ? ? ? ? ?}? ? ? ? else { set req.http.X-Forwarded-For = client.ip; }##varnish实现图片的防盗链# ? ? ? ?if (req.http.referer ~ "http://.*)?# ? ? ? ? ?{# ? ? ? ? ? ? if ( !(req.http.referer ~ "http://.*vicp\.net" ||# ? ? ? ? ? ? ? ? ? req.http.referer ~ "http://.*kangta15474\.net" ) )# ? ? ? ? ? ? ? ? {# ? ? ? ? ? ? ? ? ? set req.http.host = "kangta15474.vicp.net";# ? ? ? ? ? ? ? ? ? set req.url = "/referer.jpg";?# ? ? ? ? ? ? ? ? }# ? ? ? ? ? ? ?return(lookup);# ? ? ? ? ?}# ? ? ? ? else {return(pass);}? ? ? ?if (req.request != "GET" &&?? ? ? ? ? ?req.request != "HEAD" &&?? ? ? ? ? ?req.request != "PUT" &&?? ? ? ? ? ?req.request != "POST" &&?? ? ? ? ? ?req.request != "TRACE" &&?? ? ? ? ? ?req.request != "OPTIONS" &&?? ? ? ? ? ?req.request != "DELETE")?? ? ? ? { return (pipe); }? ? ? ? #对非GET|HEAD请求的直接转发给后端服务器? ? ? ? if (req.request != "GET" && req.request != "HEAD")? ? ? ? ? ? { return (pass); }? ? ? ? ##对GET请求,且url里以.php和.php?结尾的,直接转发给后端服务器? ? ? ? if (req.request == "GET" && req.url ~ "\.(php)($|\?)")? ? ? ? ? ? { return (pass); }? ? ? ? ##对请求中有验证及cookie,直接转发给后端服务器? ? ? ? if (req.http.Authorization || req.http.Cookie)? ? ? ? ? ? { return (pass);}? ? ? ? ?{? ? ? ? ? ?##除以上的访问请求,从缓存中查找? ? ? ? ? ?return (lookup);? ? ? ? ?}? ? ? ?##指定的font目录不进行缓存? ? ? ?if (req.url ~ "^/fonts/")? ? ? ? ? ?{ return (pass); }}sub vcl_pipe?? ? ? ? ? ? { return (pipe); }##进入pass模式,请求被送往后端,后端返回数据给客户端,但不进入缓存处理?sub vcl_pass?? ? ? ? ? ? { return (pass); }sub vcl_hash? ? ? {? ? ? ? ? set req.hash += req.url;?? ? ? ? if (req.http.host)?? ? ? ? ? ?{ set req.hash += req.http.host; }?? ? ? ? else { set req.hash += server.ip; }?? ? ? return (hash);?? ? ? }##在lookup后如果在cache中找到请求的缓存,一般以下面几个关键词结束sub vcl_hit?? ? ? ? ? {?? ? ? ? ? ? ? if (!obj.cacheable)?? ? ? ? ? ? ? ? { return (pass); }?? ? ? ? ? ? ? ?return (deliver);?? ? ? ? ? }?##lookup后没有找到缓存时调用,以下面几个关键词结束,及调用fetch参数重新测试是否加入缓存sub vcl_miss?? ? ?{ return (fetch); }#让varnish服务器缓存的类型,从后端取得数据后调用sub vcl_fetch?? { ? ?if (!beresp.cacheable)?? ? ? ? ? ? { return (pass); }?? ? ? ? if (beresp.http.Set-Cookie)?? ? ? ? ? ?{ return (pass); }?? ? ? ?##WEB服务器指明不缓存的内容,varnish服务器不缓存? ? ? ?if (beresp.http.Pragma ~ "no-cache" || beresp.http.Cache-Control ~ "no-cache" || beresp.http.Cache-Control ~ "private")?? ? ? ? ? { return (pass); }? ? ? ?##对访问中get有包含jpg,png等格式的文件进行缓存,缓存时间为7天,s为秒? ? ? if (req.request == "GET" && req.url ~ "\.(js|css|mp3|jpg|png|gif|swf|jpeg|ico)$")?? ? ? ? ?{ set beresp.ttl = 7d; }? ? ? ##对访问get中包含htm等静态页面,缓存300秒?? ? ? if (req.request == "GET" && req.url ~ "\/[0-9]\.htm$")?? ? ? ? ?{ set beresp.ttl = 300s; }? ? ? ? ? ?return (deliver);?? ?}####添加在页面head头信息中查看缓存命中情况########sub vcl_deliver??{? ? ? ?set resp.http.x-hits = obj.hits ;?? ? ? ?if (obj.hits > 0)?? ? ? ? ? ? ? { set resp.http.X-Cache = "HIT cqtel-bbs"; }?? ? ? ?else { set resp.http.X-Cache = "MISS cqtel-bbs"; }?? }#########################以上为 varnish的配置文件##########################?创建用户:groupadd wwwuseradd www -g www创建 varnish_cache的缓存位置mkdir /data/varnish_cache启动varnishulimit -SHn 8192 ? ####设置文件描述符,因为我的机子性能并不好,可以按照自己的配置去设置/usr/local/varnish-2.1.3/sbin/varnishd -u www -g www -f /usr/local/varnish-2.1.3/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/data/varnish_cache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500####-u 以什么用运行 -g 以什么组运行 -f varnish配置文件 -a 绑定IP和端口 -s varnish缓存文件位置与大小 -w 最小,最大线程和超时时间 -T varnish管理端口,主要用来清除缓存#结束varnishd进程pkill varnishd启动varnishncsa用来将Varnish访问日志写入日志文件:/usr/local/varnish-2.1.3/bin/varnishncsa -w /data/logs/varnish.log &每天0点运行,按天切割Varnish日志,生成一个压缩文件,同时删除上个月旧日志的脚本(/var/logs/cutlog.sh):vim /usr/local/varnish-2.1.3/etc/varnish/cut_varnish_log.sh写入以下脚本:#!/bin/sh# This file run at 00:00date=$(date -d "yesterday" +"%Y-%m-%d")pkill -9 varnishncsamv /data/logs/varnish.log /data/logs/${date}.log/usr/local/varnish-2.1.3/bin/varnishncsa ?-w /data/logs/varnish.log &mkdir -p /data/logs/varnish/gzip -c /data/logs/${date}.log > /data/logs/varnish/${date}.log.gzrm -f /data/logs/${date}.logrm -f /data/logs/varnish/$(date -d "-1 month" +"%Y-%m*").log.gz定时任务:crontab -e00 00 * * * /usr/local/varnish-2.1.3/etc/varnish/cut_varnish_log.sh?优化Linux内核参数vi /etc/sysctl.confnet.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_keepalive_time = 300net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.ip_local_port_range = 5000 ? ?65000使配置生效/sbin/sysctl -p?通过Varnish管理端口,使用正则表达式批量清除缓存清除所有缓存/usr/local/varnish-2.1.3/bin/varnishadm -T 127.0.0.1:3500 url.purge *$清除image目录下所有缓存/usr/local/varnish-2.1.3/bin/varnishadm -T 127.0.0.1:3500 url.purge /image/127.0.0.1:3500 为被清除缓存服务器地址 www.kangta.com 为被清除的域名 /static/image/tt.jsp 为被清除的url地址列表/usr/local/varnish-2.1.3/bin/varnishadm -T 127.0.0.1:3500 purge "req.http.host ~ www.kangta.com$ && req.url ~ /static/image/tt.jsp"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++一个清除Squid缓存的PHP函数<?php ??function purge($ip, $url) ??{ ??? ? $errstr = ''; ??? ? $errno = ''; ??? ? $fp = fsockopen ($ip, 80, $errno, $errstr, 2); ??? ? if (!$fp) ??? ? { ??? ? ? ? ?return false; ??? ? } ??? ? else ?? ? { ??? ? ? ? $out = "PURGE $url HTTP/1.1\r\n"; ??? ? ? ? $out .= "Host:blog.s135.com\r\n"; ??? ? ? ? $out .= "Connection: close\r\n\r\n"; ??? ? ? ? fputs ($fp, $out); ??? ? ? ? $out = fgets($fp , 4096); ??? ? ? ? fclose ($fp); ??? ? ? ? return true; ??? ? } ??} ????purge("192.168.0.4", "/index.php"); ???>?++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++?配置开机自动启动Varnishvim /etc/rc.d/rc.local在末行写入以下内容:ulimit -SHn 8192/usr/local/varnish-2.1.3/sbin/varnishd -u www -g www -f /usr/local/varnish-2.1.3/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/data/varnish_cache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500/usr/local/varnish-2.1.3/bin/varnishncsa -w /data/logs/varnish.log &查看Varnish服务器连接数与命中率:/usr/local/varnish-2.1.3/bin/varnishstat
以上为varnish的状态,1675 ? ? ? ? 0.00 ? ? ? ? 0.06 Client requests received ? 为服务端接收的客户端请求次数179 ? ? ? ? 0.00 ? ? ? ? 0.01 Cache hits ? ?为命中缓存,从缓存中取得数据返回给客户端的次数,即命中率11 ? ? ? ? 0.00 ? ? ? ? 0.00 Cache misses ?为跳过pass缓存,从后端服务应用中取得数据返回给用户的次数用help看看可以使用哪些Varnish命令:/usr/local/varnish-2.1.3/bin/varnishadm -T 127.0.0.1:3500 help

热点排行