准备:
下载源码包
cd /usr/src
wget https://repo.varnish-cache.org/source/varnish-4.0.1.tar.gz
yum -y install python-docutils
yum -y install ncurses*
yum -y install libedit*
安装:
root@d1:/opt/1/varnish-4.0.0-tp1# ./autogen.sh+ libtoolize --copy --forcelibtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `build-aux'.libtoolize: copying file `build-aux/ltmain.sh'libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.libtoolize: copying file `m4/libtool.m4'libtoolize: copying file `m4/ltoptions.m4'libtoolize: copying file `m4/ltsugar.m4'libtoolize: copying file `m4/ltversion.m4'libtoolize: copying file `m4/lt~obsolete.m4'+ aclocal -I m4+ autoheader+ automake --add-missing --copy --foreign+ autoconf安装、编译:export PKG_CONFIG_PATH=/usr/lib/pkgconfig:$PKG_CONFIG_PATH;./autogen.sh && ./configure --prefix=/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking --with-jemalloc报错:
varnish安装时出现:No package 'libpcre' found
系统:centos 5.7 64位varnish:2.1.3
PS:但是在安装varnish-3.0版本时没有出现这样的错误!!
安装编译的时候出现一下错误:
checking for library containing pthread_create... -lpthread
checking for socket in -lsocket... nochecking for getaddrinfo in -lnsl... yeschecking for cos in -lm... yeschecking for pkg-config... /usr/bin/pkg-configchecking pkg-config is at least version 0.9.0... yeschecking for PCRE... noconfigure: error: Package requirements (libpcre) were not met:No package 'libpcre' foundConsider adjusting the PKG_CONFIG_PATH environment variable if youinstalled software in a non-standard prefix.Alternatively, you may set the environment variables PCRE_CFLAGSand PCRE_LIBS to avoid the need to call pkg-config.See the pkg-config man page for more details.提示是缺少 pcre,我已安装好pcre-8.13。还是提示错误,经过baidu一查,后终于找到解决办法:yum -y install libpcre3-dev
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
这样就能configure成功了!!!
make
/usr/local/src/varnish-4.0.1/bin/varnishd/.libs/lt-varnishd: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法: ln -s /usr/local/lib/libpcre.so.1 /lib
make install
#############################################varnish-3.0.4-4.0.0 编译都出错误。varnishadm.c:48:33: error: editline/readline.h:No such file or directory解决:apt-get install -y ncurses*apt-get install -y libedit*Centos6.2 x86_64环境yum -y install ncurses-develrpm -ivh ftp://rpmfind.net/linux/centos/6.4/os/x86_64/Packages/libedit-devel-2.11-4.20080712cvs.1.el6.x86_64.rpm再重新configure 解决3.04/3.05问题#############################################root@d1:/usr/local/varnish/sbin# ./varnishd -Vvarnishd (varnish-4.0.0-tp1 revision b9226c1)Copyright (c) 2006 Verdens Gang ASCopyright (c) 2006-2011 Varnish Software AS 建立缓存目录useradd -s /sbin/nologin varnishmkdir -p /usr/local/varnish/cachemkdir -p /usr/local/varnish/logchown -R varnish:varnish /usr/local/varnish/logchown -R varnish:varnish /usr/local/varnish/cache启动主服务/usr/local/varnish/sbin/varnishd -P /usr/local/varnish/run/varnish.pid -a 10.10.18.244:80 -T 127.0.0.1:3500 -f /usr/local/varnish/etc/vcl.conf -u varnish -g varnish -n /usr/local/varnish/cache/ -s malloc,1G -P client_http11=onVarnish添加到服务自启动
cp /usr/src/varnish-4.0.1/redhat varnish.initrc /etc/init.d/varnish
chmod +x /etc/init.d/varnish
chkconfig varnish on
service varnish restart
启动日志管理
/usr/local/varnish/bin/varnishncsa -n /usr/local/varnish/cache/ -w /usr/local/varnish/logs/varnish.log &测试(此图片已经在mongodb中):[root@vm-250 ~]# curl -I http://10.10.18.244/pics/1.jpgHTTP/1.1 200 OKServer: nginx/1.5.8Content-Type: jpgExpires: Thu, 31 Dec 2037 23:55:55 GMTCache-Control: max-age=315360000, publicDate: Thu, 16 Jan 2014 02:53:00 GMTX-Varnish: 32806 22Age: 349Via: 1.1 varnishContent-Length: 2807Connection: keep-alive[root@vm-250 ~]# curl -I http://10.10.18.244/pics/1.jpgHTTP/1.1 200 OKServer: nginx/1.5.8Content-Type: jpgExpires: Thu, 31 Dec 2037 23:55:55 GMTCache-Control: max-age=315360000, publicDate: Thu, 16 Jan 2014 06:28:11 GMTX-Varnish: 32798 3Age: 15Via: 1.1 varnishX-Cache: MISS from www.xikang.comContent-Length: 2807Connection: keep-alive如何判断是否是经过缓存的呢??首先可以看Age字段,如果是大于0的话,说明是从缓存命中的。其次查看X-Varnish字段,如果是两个数字例如X-Varnish:32798 3代表的是Varnish两次请求id(req.xid):一个为发起请求的id,另外一个是获取缓存的id,而缓存未命中或者pass到后端的情况都是一个数值(当然你可以在deliver过程中修改这些头信息或者直接隐藏掉)?注意!? 如果后端程序设置了类似这样的头信息Cache-Control:?max-age=300?会重置默认的缓存时间TTL,当然你可以在配置文件中重置这个时间#####################################################去掉 varnish 中的一些头信息(如果你不想保留的话,建议保留 Age,方便查看)remove resp.http.X-Varnish;remove resp.http.Via;return (deliver);[root@vm-250 ~]# curl -I http://10.10.18.244/pics/1.jpgHTTP/1.1 200 OKServer: nginx/1.5.8Content-Type: jpgExpires: Thu, 31 Dec 2037 23:55:55 GMTCache-Control: max-age=315360000, publicDate: Thu, 16 Jan 2014 06:39:33 GMT-------------------------X-Varnish: 32774 3Age: 6-------------------------X-Cache: MISS from www.xikang.comContent-Length: 2807Connection: keep-alive检测性能:root@d1:/usr/local/varnish/etc# /usr/local/varnish/bin/varnishstat -n /usr/local/varnish/cache/Uptime mgt: 0+00:11:00Uptime child: 0+00:11:00 NAME CURRENT CHANGE AVERAGE AVG_10 AVG_100 AVG_1000MAIN.uptime 660 1.00 1.00 1.00 1.00 1.00MAIN.sess_conn 10 0.00 . 0.00 0.00 0.00MAIN.client_req 97 0.00 . 0.00 0.00 0.00MAIN.cache_hit 93 0.00 . 0.00 0.00 0.00MAIN.cache_miss 4 0.00 . 0.00 0.00 0.00MAIN.backend_conn 2 0.00 . 0.00 0.00 0.00MAIN.backend_reuse 2 0.00 . 0.00 0.00 0.00MAIN.backend_toolate 1 0.00 . 0.00 0.00 0.00MAIN.backend_recycle 4 0.00 . 0.00 0.00 0.00MAIN.fetch_length 4 0.00 . 0.00 0.00 0.00MAIN.pools 2 0.00 . 2.00 2.00 2.00MAIN.threads 200 0.00 . 200.00 200.00 200.00MAIN.threads_created 200 0.00 . 0.00 0.00 0.00MAIN.n_object 4 0.00 . 4.00 4.00 4.00MAIN.n_objectcore 6 0.00 . 6.00 6.00 6.00MAIN.n_objecthead 6 0.00 . 6.00 6.00 6.00MAIN.n_backend 1 0.00 . 1.00 1.00 1.00MAIN.s_sess 10 0.00 . 0.00 0.00 0.00MAIN.s_req 97 0.00 . 0.00 0.00 0.00MAIN.s_fetch 4 0.00 . 0.00 0.00 0.00MAIN.s_hdrbytes 30909 0.00 46.00 0.00 0.00 0.00MAIN.s_bodybytes 2142154 0.00 3245.00 0.00 0.00 0.00MAIN.backend_req 4 0.00 . 0.00 0.00 0.00MAIN.n_vcl 1 0.00 . 0.00 0.00 0.00MAIN.bans 1 0.00 . 1.00 1.00 1.00MGT.uptime 660 1.00 1.00 1.00 1.00 1.00SMA.s0.c_req 7 0.00 . 0.00 0.00 0.00SMA.s0.c_bytes 1993075 0.00 3019.00 0.00 0.00 0.00SMA.s0.g_alloc 7 0.00 . 7.00 7.00 7.00SMA.s0.g_bytes 1993075 0.00 . 1993075.00 1993075.00 1993075.00SMA.s0.g_space 1071748749 0.00 . 1071748749.0 1071748749.0 1071748749.004.0版本的配置文件语法与以往的语法有一定的区别,需要注意一下。官方地址:https://www.varnish-cache.org/docs/trunk/index.html