varnish部署(三)

时间 : 14-08-10 栏目 : linux运维 作者 : 老薛 评论 : 0 点击 : 1,043 次

#############################
backend www {
.host = "www.bdkyr.com";
.port = "80";
}

acl purge {
"localhost";
"127.0.0.1";
"172.16.2.0"/24;
}

sub vcl_recv {
if (req.restarts == 0) {
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") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}

else {
lookup;
}
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);
}
return (deliver);
}

sub vcl_deliver {
return (deliver);
}

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/xhtml1/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>
<p>Varnish cache server</p>
</body>
</html>
"};
return (deliver);
}
###################################
配置文件解释:
(1)、Varnish通过反向代理请求后端IP为172.16.2.223,端口为80的web服务器;
(2)、Varnish允许localhost、127.0.0.1、172.16.2.233 三个来源IP通过PURGE方法清除缓存;
(3)、Varnish对域名为www.elain.org的请求进行处理,非www.elain.org域名的请求则返回“elain Cache Server”;
(4)、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;
(5)、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。

启动Varnish
ulimit -SHn 65535
/elain/apps/varnish/sbin/varnishd -f /elain/apps/varnish/etc/varnish/elain_vcl.conf -a 172.16.2.223:80 -s file,/elain/data/vcache,1G -w 1024,51200,10 -t 3600 -T 172.16.2.223:3500

参数:
-u 以什么用运行
-g 以什么组运行
-f varnish 配置文件
-a 绑定 IP 和端口
-s varnish 缓存文件位置与大小
-w 最小,最大线程和超时时间
-T varnish 管理端口,主要用来清除缓存

启动varnishncsa用来将Varnish访问日志写入日志文件:
/application/varnish/bin/varnishncsa  -w /data/varnish/log &

停止Varnish
pkill varnish

配置开机自动启动Varnish
vi /etc/rc.local
在末尾增加以下内容:

ulimit -SHn 65535
/application/varnish/sbin/varnishd -f /application/varnish/etc/varnish/varnish_vcl.conf -a 172.16.2.223:80 -s file,/data/varnish/cache,1G -w 1024,51200,10 -t 3600 -T 172.16.2.223:3500
/application/varnish/bin/varnishncsa -n /data/varnish/cache -w /data/varnish/log/varnish.log &

优化Linux内核参数
vi /etc/sysctl.conf
在末尾增加以下内容:

net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000

sysctl -p

本文标签

除非注明,文章均为( 老薛 )原创,转载请保留链接: http://www.bdkyr.com/xtyw002/211.html

varnish部署(三):等您坐沙发呢!

发表评论

6 + 9 = ?


博主微信号,很高兴为您提供帮助

随便看看

0