varnish 缓存服务器安装与配置
1, Varnish采用了Squid具有优势,它避免了
Varnish已经有一个月,没有发生过故障,而Squid服务器就倒过几次。Varnish管理端口,可以使用正则表达式快速、批量地清除部分缓存,这一点是<span style="font-family: "Tahoma','sans-serif'; FONT-SIZE: 9pt" lang=EN-US>Squid不能具备的。
说明:
web server 前端:192.168.10.127
Varnish 缓存服务:192.168.10.41
web server 后端:192.168.10.30
web server 后端:192.168.10.43
-----------------------------------------------------
一、编译安装varnish
tar zxvf varnish-2.1.3.tar.gz -C /usr/src/
cd /usr/src/varnish-2.1.3/
./configure --prefix=/usr/local/varnish
make && make install
cd /usr/local/varnish/etc/varnish/
cp default.vcl mos.conf
二、配置varnish缓存服务
#vim mos.conf
------------------------------------------------------------------------------------------------------------
# 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 default {
.host = "192.168.10.30";
.port = "80";
}
backend default {
.host = "192.168.10.43";
.port = "80";
}
# Below is a commented-out copy of the default VCL logic.If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
sub vcl_recv {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For ", " 192.168.10.30;
} else {
set req.http.X-Forwarded-For = 192.168.10.43;
}
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);
}
return (lookup);
}
#
sub vcl_pipe {
## Note that only the first request to the backend will have
## X-Forwarded-For set.If you use X-Forwarded-For and want to
## have it set for all requests, make sure to have:
## set req.http.connection = "close";
## here.It is not set by default as it might break some broken web
## applications, like IIS with NTLM authentication.
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);
}
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 ~ "\.(txt|js|css|shtml|html|htm)$") {
set beresp.ttl = 3600s;
}
else {
set beresp.ttl = 10d;
}
return (deliver);
}
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";
}
}
以上是vcl的配置。
:wq!
mkdir -p /data/varnish/{cache,logs}
chmod +w /data/varnish/{cache,logs}
groupadd www
useradd www -g www -s /sbin/nologin
chown -R www:www /data/varnish/{cache,logs}
/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/mos.conf -a 192.168.10.41:80 -s file,/data/varnish/cache/varnish_cache.data,256MB -w 256,2560,10 -t 3600 -T 192.168.10.41:3000
转载于:https://blog.51cto.com/haowen/1150038