昨晚对公司官网缓存程序做了变更,由nginx更换为varnish。Varnish是一款高性能的开源HTTP加速器,相比较squid和nginx,具有更大的优势,目前已广泛用于各网站缓存系统。
部署服务器:223.5.X.X、223.5.X.Y
安装:
#yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
接下来将你下载的软件包解压到一个目录下
#tar zxvf varnish-2.1.3.tar.gz然后进入解压后的目录中
#cd varnish-2.1.3
在配置前可以检查一下软件的依赖关系是否满足,避免不必要的麻烦
#sh autogen.sh
#sh configure
#make
#make install
安装过程中出现.so不存在问题解决:
如果发现了libXXX.so.1.2.3的确存在,就把libXXX.so.1.2.3所在的目录加入到 /etc/ld.so.conf 中,或者在 /etc/ld.so.conf.d/ 下新建一文件,如 XXX.conf ,其内容是libXXX.so.1.2.3所在的目录。
如果发现libXXX.so.1.2.3不存在,你可能没安装包含库的程序。一般google一下“XXX linux”就能找到相应的软件。
ldconfig
主程序路径:/usr/local/etc/varnish/bobo365.vcl
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend web01_bobo365 {
.host = "192.168.1.232";
.port = "80";
.probe = {
.url = "/";
.interval = 5s;
.timeout = 1 s;
.window = 5;
.threshold = 3;
}
}
backend web02_bobo365 {
.host = "192.168.1.236";
.port = "80";
.probe = {
.url = "/";
.interval = 5s;
.timeout = 1 s;
.window = 5;
.threshold = 3;
}
}
backend img01_bobo365 {
.host = "192.168.1.235";
.port = "80";
# .probe = {
# .url = "/";
# .interval = 5s;
# .timeout = 1 s;
# .window = 5;
# .threshold = 3;
# }
}
#backend img02_bobo365 {
# .host = "192.168.1.250";
# .port = "80";
# .probe = {
# .url = "/";
# .interval = 5s;
# .timeout = 1 s;
# .window = 5;
# .threshold = 3;
# }
# }
director lb_web random {
.retries = 6;
{
.backend = web01_bobo365;
.weight = 2;
}
{
.backend = web02_bobo365;
.weight = 2;
}
}
director lb_img random {
.retries = 6;
{
.backend = img01_bobo365;
.weight = 2;
}
# {
# .backend = img02_bobo365;
# .weight = 2;
# }
}
acl local {
"localhost";
"127.0.0.1";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?bobo365.com$") {
set req.backend = lb_web;
}
elsif (req.http.host ~ "^(img.)?bobo365.com$") {
set req.backend = lb_img;
}
else {
error 404 "Unknown HostName!";
}
if (req.request == "PURGE") {
if (!client.ip ~ local) {
error 405 "Not Allowed.";
return (lookup);
}
}
if (req.request == "GET" && req.url ~ ".(jpg|png|gif|swf|jpeg|ico)$") {
unset req.http.cookie;
}
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;
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
return (pass);
}
if (req.request == "GET" && req.url ~ ".(php)($|?)") {
return (pass);
}
return (lookup);
}
sub vcl_pipe {
return (pipe);
}
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);
}
sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
return (deliver);
}
sub vcl_miss {
return (fetch);
}
sub vcl_fetch {
if (!beresp.cacheable) {
return (pass);
}
if (beresp.http.Set-Cookie) {
return (pass);
}
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 ~ ".(gif|png|ico|jpg|jpeg|bmp|tif|swf
)$" ) {
set beresp.ttl = 5d;
}
else if (req.request == "GET" && req.url ~ ".(txt|shtml|html|htm)$" ) {
set beresp.ttl = 1d;
}
else if (req.request == "GET" && req.url ~ ".(css|j|js)$" ) {
set beresp.ttl = 1d;
}
else {
set beresp.ttl = 30d;
}
return (deliver);
}
sub vcl_deliver {
set resp.http.x-hits = obj.hits ;
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT cqtel-web01_bobo365";
} else {
set resp.http.X-Cache = "MISS cqtel-web02_bobo365";
}
}
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x
html1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>"} obj.status " " obj.response {"</title>
</head>
<body>
<h1>Error "} obj.status " " obj.response {"</h1>
<p>"} obj.response {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} req.xid {"</p>
<hr>
<address>
<a href="http://www.bobo365.com/">http://www.bobo365.com</a>
# <a href="http://bobo365.com/">default02 cache server</a>
</address>
</body>
</html>
"};
return (deliver);
}
日志存放路径:
/opt/varnish/logs/varnish.log
日志切割脚本路径:
/opt/varnish/cutvlog.sh
#!/bin/sh
vlog= /opt/varnish/logs/varnish.log
logs_path=/var/log/varnish-log
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mkdir -p /var/log/varnish-log
mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv /opt/varnish/logs/varnish.log ${logs_path}/$(date -d "yesterday" +"%Y")/$(dat
e -d "yesterday" +"%m")/varnish-${date}.log
varnishncsa -w /opt/varnish/logs/varnish.log &
启动日志记录命令:
varnishncsa -w /opt/varnish/logs/varnish.log &
程序关闭:pkill varnish
程序启动:varnishd -f /usr/local/etc/varnish/bobo365.vcl -s file,/data/varnish/vcache/varnish_cache.data,1G -T 127.0.0.1:2000 -a 0.0.0.0:800
缓存文件路径:/data/varnish/vcache/varnish_cache.data
计划任务:0 0 * * * /opt/varnish/cutvlog.sh
查看缓存命中情况:
varnishstat
varnishhist
varnishlog
haproxy监控页面:
http://www.bobo365.com/haproxy-stats
缓存清除:
varnishadm -T 127.0.0.1:2000 purge "req.http.host ~ www.bobo365.com$&& req.url~ /static/image/tt.jpg"
说明:
127.0.0.1:2000 为被清除缓存服务器地址及端口(与程序启动命令有关)
www.bobo365.com 为被清除的域名
/static/image/tt.jpg 为被清除的url地址列表
清除所有缓存
# varnishadm -T 127.0.0.1:2000 purge.rul *$
清除image目录下所有缓存
varnishadm -T 127.0.0.1:2000 purge.url /image/
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。