一、varnish介绍:
[引用]Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang (http://www.vg.no) 使用3台Varnish代替了原来的12台squid,性能居然比以前更好。
Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算 机系统的内存除了主存外,还包括了cpu内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此squid cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。
官网:https://www.varnish-cache.org/
二、安装配置:
wget http://repo.varnish-cache.org/source/varnish-3.0.4.tar.gz
tar zxvf varnish-3.0.4.tar.gz
groupadd varnish
useradd -u 2001 -s /sbin/nologin -g varnish varnish
chwon -R varnish.varnish /u1/vcache
chown -R varnish.varnish /u1/vcache
chown -R varnish.varnish /u1/vcache/logs
yum -y install pcre pcre-devel
cd varnish-3.0.4
./configure --prefix=/usr/local/varnish
make && make install
cp redhat/varnish.initrc /etc/init.d/varnish
chmod 755 /etc/init.d/varnish
cp redhat/varnish.sysconfig /etc/sysconfig/varnish
chown varnish:varnish /etc/sysconfig/varnish
cp redhat/varnish_reload_vcl /usr/local/varnish/bin/varnish_reload_vcl
cd /usr/local/
chown -R varnish.varnish /usr/local/varnish
cd varnish/
cp etc/varnish/default.vcl etc/vcl.conf
chown varnish.varnish vcl.conf
cat etc/vcl.conf
backend bbsServer { #配置后端服务器的地址和端口,负载均衡可配置多个实现。
.host = "192.168.1.108";
.port = "80";
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
}
acl purge{
"localhost";
"127.0.0.1";
"192.168.1.0"/24;
}
sub vcl_recv {
if (req.request == "PURGE") {
if(!client.ip ~ purge){
error 405 "Not allowed.";
}
return(lookup);
}
if (req.http.host ~ "^(www.bbs.linuxblind.com)") {
set req.backend = bbsServer;
if(req.request!="GET" && req.request != "HEAD"){
return(pipe);
}
else{
return(lookup);
}
}
else{
error 404 "bbs Cache server";
return(lookup);
}
}
sub vcl_pipe {
return (pipe);
}
sub vcl_pass {
return (pass);
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache";
}
}
sub vcl_fetch { #缓存控制
if (req.request == "GET" && req.url ~ "\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg|css|js|html|htm)$") {
set beresp.ttl = 3600s; #设置缓存时间
}
else{
set beresp.ttl = 60s;
}
return (deliver);
}
cat /etc/init.d/varnish
#! /bin/sh
#
# varnish Control the Varnish Cache
#
# chkconfig: - 90 10
# description: Varnish is a high-perfomance HTTP accelerator
# processname: varnishd
# config: /etc/sysconfig/varnish
# pidfile: /var/run/varnishd.pid
### BEGIN INIT INFO
# Provides: varnish
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start:
# Default-Stop:
# Should-Start: $syslog
# Short-Description: start and stop varnishd
# Description: Varnish is a high-perfomance HTTP accelerator
### END INIT INFO
# Source function library.
. /etc/init.d/functions
retval=0
pidfile=/var/run/varnish.pid
exec="/usr/local/varnish/sbin/varnishd"
reload_exec="/usr/local/varnish/bin/varnish_reload_vcl"
prog="varnishd"
config="/etc/sysconfig/varnish"
lockfile="/var/lock/subsys/varnish"
# Include varnish defaults
[ -e /etc/sysconfig/varnish ] && . /etc/sysconfig/varnish
start() {
if [ ! -x $exec ]
then
echo $exec not found
exit 5
fi
if [ ! -f $config ]
then
echo $config not found
exit 6
fi
echo -n "Starting Varnish Cache: "
# Open files (usually 1024, which is way too small for varnish)
ulimit -n ${NFILES:-131072}
# Varnish wants to lock shared memory log in memory.
ulimit -l ${MEMLOCK:-82000}
# Maximum number of threads (default in CentOS is 1024, which
# is often too small for varnish)
ulimit -u ${NPROCS:-unlimited}
# $DAEMON_OPTS is set in /etc/sysconfig/varnish. At least, one
# has to set up a backend, or /tmp will be used, which is a bad idea.
if [ "$DAEMON_OPTS" = "" ]; then
echo "\$DAEMON_OPTS empty."
echo -n "Please put configuration options in $config"
return 6
else
# Varnish always gives output on STDOUT
daemon --pidfile $pidfile $exec -P $pidfile "$DAEMON_OPTS" > /dev/null 2>&1
retval=$?
if [ $retval -eq 0 ]
then
touch $lockfile
echo_success
echo
else
echo_failure
echo
fi
return $retval
fi
}
stop() {
echo -n "Stopping Varnish Cache: "
killproc -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
if [ "$RELOAD_VCL" = "1" ]
then
$reload_exec
else
force_reload
fi
}
force_reload() {
restart
}
rh_status() {
status -p $pidfile $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
configtest() {
if [ -f "$VARNISH_VCL_CONF" ]; then
$exec -f "$VARNISH_VCL_CONF" -C -n /tmp > /dev/null && echo "Syntax ok"
else
echo "VARNISH_VCL_CONF is unset or does not point to a file"
fi
}
# See how we were called.
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
configtest)
configtest
;;
*)
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
cat /etc/sysconfig/varnish
# Configuration file for varnish
# /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
NFILES=131072
MEMLOCK=82000
NPROCS="unlimited"
RELOAD_VCL=1
VARNISH_VCL_CONF=/usr/local/varnish/etc/vcl.conf
VARNISH_LISTEN_PORT=80
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=3500
#VARNISH_SECRET_FILE=/etc/varnish/secret
VARNISH_MIN_THREADS=50
VARNISH_MAX_THREADS=1000
VARNISH_THREAD_TIMEOUT=120
VARNISH_STORAGE_FILE=/var/vcache/varnish_cache.data
VARNISH_STORAGE_SIZE=4G
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
VARNISH_TTL=120
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
-f ${VARNISH_VCL_CONF} \
-T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
-t ${VARNISH_TTL} \
-w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
-u varnish -g varnish \
-n /var/vcache \
-s ${VARNISH_STORAGE}"
三、启动关闭
[root@www varnish-3.0.4]# /etc/init.d/varnish --help
Usage: /etc/init.d/varnish {start|stop|status|restart|condrestart|try-restart|reload|force-reload}
启动命令详情:/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/etc/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g varnish -u varnish -w 30000,51200,10 -T 192.168.1.108:3500
参数说明:
-f 参数是制定配置文件的路径
-n 指向cache目录
-s malloc是指后端缓存类型为内存,单位可以是M也可以是G,file是文件类型
-T 192.168.1.108:3500 是监听的管理地址和端口,可以访问http://192.168.1.108:3500查看缓存情况
-a 0.0.0.0:80 是varnish对外服务的地址端口,这里监听本机所有地址的80端口,80端口就是我们访问缓存对外的服务端口
-g 组
-u 用户
-w 最小线程数,最大线程数,timeout时长
记录varnish日志: /usr/local/varnish3.0/bin/varnishncsa -w /u1/vcache/logs/varnish.log &
四、配置文件参数讲解[引用]
内置公用变量:VCL内置的公用变量可以用在不同的VCL函数中,根据这些公用变量使用的不同阶段,下面依次介绍。当请求到达后,可以使用的公用变量如表1所示:
表1
公用变量名称 含义
req.backend 指定对应的后端主机
server.ip 表示服务器端IP
client.ip 表示客户端IP
req.request 指定请求的类型,例如GET、HEAD、POST等
req.url 指定请求的地址
req.proto 表示客户端发起请求的HTTP协议版本
req.http.header 表示对应请求中的http头部信息
req. restarts 表示请求重启的次数,默认最大值为4
Varnish 在向后端主机请求时,可以使用的公用变量如表3所示:
表2
公用变量名称 含义
beresp.request 指定请求的类型,例如GET、HEAD等
beresp.url 指定请求的地址
beresp .proto 表示客户端发起请求的HTTP协议版本
beresp .http.header 表示对应请求中的http头部信息
beresp .ttl 表示缓存的生存周期,也就是cache保留多长时间,单位是秒
从cache或者后端主机获取内容后,可以使用的公用变量如表4所示:
表3
公用变量名称 含义
obj.status 表示返回内容的请求状态代码,例如200、302、504等
obj.cacheable 表示返回的内容是否可以缓存,也就是说,如果HTTP返回是200、203、300、301、302、404、410等,并且有非0的生存期,则可以缓存
obj.valid 表示是否是有效的HTTP应答
obj.response 表示返回内容的请求状态信息
obj.proto 表示返回内容的HTTP协议版本
obj.ttl 表示返回内容的生存周期,也就是缓存时间,单位是秒
obj.lastuse 表示返回上一次请求到现在的间隔时间,单位是秒
对客户端应答时,可以使用的公用变量如表5所示:
表4
公用变量名称 含义
resp.status 表示返回给客户端的HTTP状态代码
resp.proto 表示返回给客户端的HTTP协议版本
resp.http.header 表示返回给客户端的HTTP头部信息
resp.response 表示返回给客户端的HTTP状态信息
在上面的讲述中,我们只是介绍了常用的VCL内置公用变量,如果需要了解和使用更多的公用变量信息,请登录varnish官方网站查阅。
五、测试结果展示:
或者使用如下命令查看:
[root@www varnish-3.0.4]# curl -I http://www.bbs.linuxblind.com/index.html
HTTP/1.1 200 OK
Server: Tengine/1.2.2
Content-Type: text/html
Last-Modified: Wed, 21 Aug 2013 03:28:52 GMT
Cache-Control: public, max-age=200000
Content-Length: 13
Accept-Ranges: bytes
Date: Fri, 23 Aug 2013 02:52:47 GMT
X-Varnish: 248140804 248140801
Age: 1183
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from www.linuxblind.com
[root@www varnish-3.0.4]# wget -SO /dev/null http://www.bbs.linuxblind.com -e http_proxy=192.168.1.108
--2013-08-23 10:54:34-- http://www.bbs.linuxblind.com/
正在连接 192.168.1.108:80... 已连接。
已发出 Proxy 请求,正在等待回应...
HTTP/1.1 200 OK
Server: Tengine/1.2.2
Content-Type: text/html
Last-Modified: Wed, 21 Aug 2013 03:28:52 GMT
Cache-Control: public, max-age=200000
Content-Length: 13
Accept-Ranges: bytes
Date: Fri, 23 Aug 2013 02:54:34 GMT
X-Varnish: 248140805
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS from www.linuxblind.com
长度:13 [text/html]
正在保存至: “/dev/null”
100%[======================================================================>] 13 --.-K/s in 0s
2013-08-23 10:54:34 (2.14 MB/s) - 已保存 “/dev/null” [13/13])
遇到的一个问题:
[root@www varnish-3.0.4]# /usr/local/varnish/bin/varnishstat -n /var/vcache
Cannot read /var/vcache/_.vsm: Success
[root@www vcache]# ps aux | grep varnish
root 8839 0.0 0.0 119544 1260 ? Ss 11:00 0:00 /usr/local/varnish/sbin/varnishd -P /var/run/varnish.pid -a :80 -f //usr/local/varnish/etc/vcl.conf -T 127.0.0.1:3500 -t 120 -w 50,1000,120 -u varnish -g varnish -s file,/var/vcache/varnish_cache.data,4G
varnish 8840 0.0 0.1 5426376 9776 ? Sl 11:00 0:00 /usr/local/varnish/sbin/varnishd -P /var/run/varnish.pid -a :80 -f //usr/local/varnish/etc/vcl.conf -T 127.0.0.1:3500 -t 120 -w 50,1000,120 -u varnish -g varnish -s file,/var/vcache/varnish_cache.data,4G
root 9002 0.0 0.0 103420 880 pts/2 S+ 11:04 0:00 grep varnish
[root@www vcache]# lsof -p 8840 | grep vsm
varnishd 8840 varnish mem REG 8,2 84934656 15869035 /usr/local/varnish/var/varnish/www.bbs.linuxblind.com/_.vsm
解决办法是指定"_.vsm"所在的位置,使用-n参数指定,如下:
vim /etc/sysconfig/varnish
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
-f ${VARNISH_VCL_CONF} \
-T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
-t ${VARNISH_TTL} \
-w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
-u varnish -g varnish \
-n /var/vcache \
-s ${VARNISH_STORAGE}"
*********************然后再分享一个监控脚本**********************
[root@www www]# cat monitor.php
<?php
$outfile=shell_exec("/usr/local/varnish/bin/varnishstat -x -n /var/vcache");
$xml=simplexml_load_string($outfile);
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
//$tmpName="";
foreach($child->children() as $subChild)
{
if ($subChild->getName() =="name" )
{
$tmpName=$subChild;
}
else if ($subChild->getName() =="value" )
{
if ($tmpName!="")
{
$arys["$tmpName"]=$subChild;
$tmpName="";
}
}
else
{
continue;
}
}
}
function byteReduce($bytes)
{
if ($bytes>1099511627776)
{
return round($bytes/1099511627776)."TB";
}
else if ($bytes > 1073741824)
{
return round($bytes/1073741824)."GB";
}
else if ($bytes>1048576)
{
return round($bytes/1048576)."MB";
}
else if ($bytes>1024)
{
return round($bytes/1024)."KB";
}
else
{
return $bytes."B";
}
}
echo "client_conn: ".$arys["client_conn"] . "<br />";
echo "client_req: ".$arys["client_req"] . "<br />";
echo "cache_hit: ".$arys["cache_hit"] . "<br />";
echo "cache_miss: ".$arys["cache_miss"] . "<br />";
echo "Cache hit rate: ".round(($arys["cache_hit"]/$arys["client_req"])*100)." % <br/>";
echo "LRU nuked objects: ".$arys[n_lru_nuked]."<br/>";
echo " ".byteReduce($arys["s_bodybytes"]+$arys["s_hdrbytes"])." Acc Content (".byteReduce($arys["s_hdrbytes"])." header ".byteReduce($arys["s_bodybytes"])." Body)";
?>
结果如下:
varnishstat
client_conn: 1499806
client_req: 1500031
cache_hit: 1500030
cache_miss: 1
Cache hit rate: 100 %
LRU nuked objects: 0
515MB Acc Content (496MB header 19MB Body)
推荐两篇博客:
转载于:https://blog.51cto.com/linuxblind/1274855