说明:最近几天时间学习了一下varnish高性能高HTTP加速器的搭建及简单配置,经过初步测试已达到预期效果,这里我就不阐述varnish的概念和好处了,因本人水平有限,所有研究的还不是很深刻,请大家理解!
实验初步目的:一台服务器既跑varnish,又跑系统自带的apache,当然端口肯定不一样,要测试确定varnish缓存了apache?
环境:RedHat4.8(64位)
IP地址:192.168.5.55
varnish版本:2.1.2
1、下载varnish并安装,我这里就不安装pcre库了,因为我系统安装时所有的包都安装完毕。另外可以到
http://varnish-cache.org下载不同的版本,我个人还是建议下载和我一样的版本,因为1版本和2版本之间的配置文件格式不太一样,不然N多人又犯头疼了!
tar -zxvf varnish-2.1.2.tar.gz
cd varnish-2.1.2
./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings
如果提示找不到no package 'libpcre' found,那么你需要指定pcre库的路径,其实很简单,你指定你的pcre库的正确路径就OK了,我这默认是在/usr/lib目录下,你的如果是之后源码安装的,那么在你安装目录下的lib目录下,比如你安装在/usr/local/pcre,那么库文件就是/usr/local/pcre/lib/pkgconfig。执行如下命令 :
export PKG_CONFIG_PATH=你的pcre库的路径
make
make install
2、基本配置varnish,我先不管里面的一些函数使用说明介绍了,如果要看的话请看权威指南,默认的varnish的配置文件全是注释在/usr/local/varnish/etc/varnish/目录下,这里我备份一下配置文件,然后新建一个default.vcl文件,内容如下:
##通过backend定义一个名称为webserver的后端主机,".host"指定后端主机的IP地址或者域名,".port"指定后端主机的端口,这里也可以定义多台后端服务器,起到辅助分担和健康检测机制。
backend webserver {
.host = "192.168.5.55";
.port = "9999";
}
.host = "192.168.5.55";
.port = "9999";
}
##开始调用vcl_recv
sub vcl_recv {
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.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;
}
##如果请求的类型不是GET、HEAD、PUT、POST、TRACE、OPTIONS、DELETE,则进入
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" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
##如果请求的类型不是GET或HEAD,则进入pass模式
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
##对zzz.com域名进行缓存加速,后端的服务器apache就是对应的域名
www.zzz.com。这个是泛域名的概念
if (req.http.host ~ "^(.*).zzz.com") {
set req.backend = webserver;
}
}
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 (req.http.host ~ "^(.*).zzz.com") {
set req.backend = webserver;
}
}
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);
}
}
下面添加一个Header标识,以判断缓存是否命中
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from www.zzz.com";
} else {
set resp.http.X-Cache = "MISS from www.zzz.com";
}
return (deliver);
}
这就是一个简单的实例,如果有别的需求请参考别的文档!
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from www.zzz.com";
} else {
set resp.http.X-Cache = "MISS from www.zzz.com";
}
return (deliver);
}
这就是一个简单的实例,如果有别的需求请参考别的文档!
3、启动varnish
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,128m -T 127.0.0.1:200 -a 0.0.0.0:9000
-f是指定配置文件
-s是一种存储类型和存储容量,使用的是malloc类型(malloc是一个c函数,用于分配内容空间),128定义多少内存被malloced。
-T是varnish基于文本的管理接口
-a是指定varnish监听所有IP发给9000端口的http请求。
查看varnish进程
root 13635 1 0 16:33 ? 00:00:00 /usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,128m -T 127.0.0.1:200 -a 0.0.0.0:9000
nobody 13636 13635 0 16:33 ? 00:00:00 /usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,128m -T 127.0.0.1:200 -a 0.0.0.0:9000
查看200端口和9000端口,200是管理端口,9000是代理端口。
nobody 13636 13635 0 16:33 ? 00:00:00 /usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,128m -T 127.0.0.1:200 -a 0.0.0.0:9000
查看200端口和9000端口,200是管理端口,9000是代理端口。
netstat -an | grep 9000
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN
netstat -an | grep 200
tcp 0 0 127.0.0.1:200 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN
netstat -an | grep 200
tcp 0 0 127.0.0.1:200 0.0.0.0:* LISTEN
4、启动系统自带的apache,配置文件基本没变动,端口改为9999而已。另外在/var/www/html目录下新建一个index.html测试页,随便写点什么内容!最后启动apache服务
/etc/init.d/httpd start
在本机hosts文件加上映射关系
192.168.5.55
www.zzz.com
5、现在应该看到varnish和apache的url是一个网页,但是不知道是代理还是缓存?
curl
http://www.zzz.com:9000 抓取varnish的页面
hello!!!
curl http://www.zzz.com:9999 抓取apache的页面
hello!!!
hello!!!
curl http://www.zzz.com:9999 抓取apache的页面
hello!!!
6、varnish配置文件在最后一行加入了Header信息,所有直接抓取报头就知道缓存是否成功?
curl -I
http://www.zzz.com:9000
HTTP/1.1 200 OK
Server: Apache/2.0.52 (Red Hat)
Last-Modified: Mon, 15 Oct 2012 07:07:27 GMT
ETag: "1bc893-9-4cc13b0a6a9c0"
Content-Type: text/html; charset=UTF-8
Content-Length: 9
Date: Tue, 13 Nov 2012 08:45:45 GMT
X-Varnish: 337763601
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from www.zzz.com 这里的MISS表示此次访问没有从缓存读取
HTTP/1.1 200 OK
Server: Apache/2.0.52 (Red Hat)
Last-Modified: Mon, 15 Oct 2012 07:07:27 GMT
ETag: "1bc893-9-4cc13b0a6a9c0"
Content-Type: text/html; charset=UTF-8
Content-Length: 9
Date: Tue, 13 Nov 2012 08:45:45 GMT
X-Varnish: 337763601
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from www.zzz.com 这里的MISS表示此次访问没有从缓存读取
再次打开这个页面,查看网页的头信息。
curl -I
http://www.zzz.com:9000
HTTP/1.1 200 OK
Server: Apache/2.0.52 (Red Hat)
Last-Modified: Mon, 15 Oct 2012 07:07:27 GMT
ETag: "1bc893-9-4cc13b0a6a9c0"
Content-Type: text/html; charset=UTF-8
Content-Length: 9
Date: Tue, 13 Nov 2012 08:45:47 GMT
X-Varnish: 337763602 337763601
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from www.zzz.com 这里的HIT表示此次访问从缓存读取,也就是缓存命中
7、配置一下缓存时间吧!我在varnish的配置文件里面配置没有生效,不知道为什么?QQ一哥们说一般都是在后端服务器配置缓存时间,所以这个问题暂不纠结了,另外再说一下varnish如果不配置缓存时间,默认是两分钟,也就是120秒。不信可以拿一个脚本测试一下!打开apache的配置文件httpd.conf,在最后面加上如下内容:
HTTP/1.1 200 OK
Server: Apache/2.0.52 (Red Hat)
Last-Modified: Mon, 15 Oct 2012 07:07:27 GMT
ETag: "1bc893-9-4cc13b0a6a9c0"
Content-Type: text/html; charset=UTF-8
Content-Length: 9
Date: Tue, 13 Nov 2012 08:45:47 GMT
X-Varnish: 337763602 337763601
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from www.zzz.com 这里的HIT表示此次访问从缓存读取,也就是缓存命中
7、配置一下缓存时间吧!我在varnish的配置文件里面配置没有生效,不知道为什么?QQ一哥们说一般都是在后端服务器配置缓存时间,所以这个问题暂不纠结了,另外再说一下varnish如果不配置缓存时间,默认是两分钟,也就是120秒。不信可以拿一个脚本测试一下!打开apache的配置文件httpd.conf,在最后面加上如下内容:
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A604800
ExpiresByType text/css A604800
ExpiresByType text/html A125 其实我测试暂时只有这一行起到作用,别的可加可不加,时间是125秒
8、重启apache服务
/etc/init.d/httpd restart
9、测试看看缓存有没有生效?
curl -I
http://www.zzz.com:9000
HTTP/1.1 200 OK
Server: Apache/2.0.52 (Red Hat)
Last-Modified: Mon, 15 Oct 2012 07:07:27 GMT
ETag: "1bc893-9-4cc13b0a6a9c0"
Cache-Control: max-age=125 这里表示缓存时间
Expires: Tue, 13 Nov 2012 09:14:28 GMT 这里表示缓存过期时间,需要加8小时算
Content-Type: text/html; charset=UTF-8
Content-Length: 9
Date: Tue, 13 Nov 2012 09:12:23 GMT
X-Varnish: 337763610
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from www.zzz.com
说明生效了!
HTTP/1.1 200 OK
Server: Apache/2.0.52 (Red Hat)
Last-Modified: Mon, 15 Oct 2012 07:07:27 GMT
ETag: "1bc893-9-4cc13b0a6a9c0"
Cache-Control: max-age=125 这里表示缓存时间
Expires: Tue, 13 Nov 2012 09:14:28 GMT 这里表示缓存过期时间,需要加8小时算
Content-Type: text/html; charset=UTF-8
Content-Length: 9
Date: Tue, 13 Nov 2012 09:12:23 GMT
X-Varnish: 337763610
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from www.zzz.com
说明生效了!
10、查看varnish系统缓存的状态
/usr/local/varnish/bin/varnishstat
0+00:44:05 nagios-server
Hitrate ratio: 1 1 1
Hitrate avg: 0.4545 0.4545 0.4545
Hitrate ratio: 1 1 1
Hitrate avg: 0.4545 0.4545 0.4545
11 0.00 0.00 Client connections accepted
11 0.00 0.00 Client requests received
5 0.00 0.00 Cache hits
6 0.00 0.00 Cache misses
6 0.00 0.00 Backend conn. success
6 0.00 0.00 Fetch with Length
10 . . N struct sess_mem
1 . . N struct objectcore
1 . . N struct objecthead
10 . . N worker threads
10 0.00 0.00 N worker threads created
1 . . N backends
6 . . N expired objects
1 . . N LRU moved objects
1 0.00 0.00 Objects sent with write
11 0.00 0.00 Total Sessions
11 0.00 0.00 Total Requests
6 0.00 0.00 Total fetch
4223 0.00 1.60 Total header bytes
9 0.00 0.00 Total body bytes
11 0.00 0.00 Session Closed
11 0.00 0.00 Session Linger
2427 2.00 0.92 SHM records
1850 2.00 0.70 SHM writes
12 0.00 0.00 SMA allocator requests
5126 . . SMA bytes allocated
5126 . . SMA bytes free
6 0.00 0.00 Backend requests made
1 0.00 0.00 N vcl total
1 0.00 0.00 N vcl available
1 . . N total active purges
2 0.00 0.00 N new purges added
1 0.00 0.00 N old purges deleted
11 0.00 0.00 HCB Lookups without lock
6 0.00 0.00 HCB Lookups with lock
6 0.00 0.00 HCB Inserts
2645 1.00 1.00 Client uptime
以上各字段不解释了,参考权威指南吧!
11 0.00 0.00 Client requests received
5 0.00 0.00 Cache hits
6 0.00 0.00 Cache misses
6 0.00 0.00 Backend conn. success
6 0.00 0.00 Fetch with Length
10 . . N struct sess_mem
1 . . N struct objectcore
1 . . N struct objecthead
10 . . N worker threads
10 0.00 0.00 N worker threads created
1 . . N backends
6 . . N expired objects
1 . . N LRU moved objects
1 0.00 0.00 Objects sent with write
11 0.00 0.00 Total Sessions
11 0.00 0.00 Total Requests
6 0.00 0.00 Total fetch
4223 0.00 1.60 Total header bytes
9 0.00 0.00 Total body bytes
11 0.00 0.00 Session Closed
11 0.00 0.00 Session Linger
2427 2.00 0.92 SHM records
1850 2.00 0.70 SHM writes
12 0.00 0.00 SMA allocator requests
5126 . . SMA bytes allocated
5126 . . SMA bytes free
6 0.00 0.00 Backend requests made
1 0.00 0.00 N vcl total
1 0.00 0.00 N vcl available
1 . . N total active purges
2 0.00 0.00 N new purges added
1 0.00 0.00 N old purges deleted
11 0.00 0.00 HCB Lookups without lock
6 0.00 0.00 HCB Lookups with lock
6 0.00 0.00 HCB Inserts
2645 1.00 1.00 Client uptime
以上各字段不解释了,参考权威指南吧!
11、管理缓存内容,清除缓存内容的命令格式
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:200 puger.url
列出最近清除的url列表的命令如下:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:200 purge.list
清除所有的缓存
清除所有的缓存
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:200 purge.url ^.*$
另外还可以通过管理端口来清除缓存页面!
这里暂不介绍了!