准备:

下载源码包

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 --force
libtoolize: 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... no
checking for getaddrinfo in -lnsl... yes
checking for cos in -lm... yes
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for PCRE... no
configure: error: Package requirements (libpcre) were not met:

No package 'libpcre' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables PCRE_CFLAGS
and 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-devel
rpm -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 -V
varnishd (varnish-4.0.0-tp1 revision b9226c1)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2011 Varnish Software AS
 
建立缓存目录
useradd -s /sbin/nologin varnish
mkdir -p /usr/local/varnish/cache
mkdir -p /usr/local/varnish/log
chown -R varnish:varnish /usr/local/varnish/log
chown -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=on

Varnish添加到服务自启动

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.jpg
HTTP/1.1 200 OK
Server: nginx/1.5.8
Content-Type: jpg
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000, public
Date: Thu, 16 Jan 2014 02:53:00 GMT
X-Varnish: 32806 22
Age: 349
Via: 1.1 varnish
Content-Length: 2807
Connection: keep-alive


[root@vm-250 ~]# curl -I http://10.10.18.244/pics/1.jpg
HTTP/1.1 200 OK
Server: nginx/1.5.8
Content-Type: jpg
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000, public
Date: Thu, 16 Jan 2014 06:28:11 GMT
X-Varnish: 32798 3
Age: 15
Via: 1.1 varnish
X-Cache: MISS from www.xikang.com
Content-Length: 2807
Connection: 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.jpg
HTTP/1.1 200 OK
Server: nginx/1.5.8
Content-Type: jpg
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000, public
Date: Thu, 16 Jan 2014 06:39:33 GMT
-------------------------
X-Varnish: 32774 3
Age: 6
-------------------------
X-Cache: MISS from www.xikang.com
Content-Length: 2807
Connection: keep-alive


检测性能:
root@d1:/usr/local/varnish/etc# /usr/local/varnish/bin/varnishstat  -n /usr/local/varnish/cache/
Uptime mgt:   0+00:11:00
Uptime child: 0+00:11:00

  NAME                                                                               CURRENT       CHANGE      AVERAGE       AVG_10      AVG_100     AVG_1000
MAIN.uptime                                                                              660         1.00         1.00         1.00         1.00         1.00
MAIN.sess_conn                                                                            10         0.00          .           0.00         0.00         0.00
MAIN.client_req                                                                           97         0.00          .           0.00         0.00         0.00
MAIN.cache_hit                                                                            93         0.00          .           0.00         0.00         0.00
MAIN.cache_miss                                                                            4         0.00          .           0.00         0.00         0.00
MAIN.backend_conn                                                                          2         0.00          .           0.00         0.00         0.00
MAIN.backend_reuse                                                                         2         0.00          .           0.00         0.00         0.00
MAIN.backend_toolate                                                                       1         0.00          .           0.00         0.00         0.00
MAIN.backend_recycle                                                                       4         0.00          .           0.00         0.00         0.00
MAIN.fetch_length                                                                          4         0.00          .           0.00         0.00         0.00
MAIN.pools                                                                                 2         0.00          .           2.00         2.00         2.00
MAIN.threads                                                                             200         0.00          .         200.00       200.00       200.00
MAIN.threads_created                                                                     200         0.00          .           0.00         0.00         0.00
MAIN.n_object                                                                              4         0.00          .           4.00         4.00         4.00
MAIN.n_objectcore                                                                          6         0.00          .           6.00         6.00         6.00
MAIN.n_objecthead                                                                          6         0.00          .           6.00         6.00         6.00
MAIN.n_backend                                                                             1         0.00          .           1.00         1.00         1.00
MAIN.s_sess                                                                               10         0.00          .           0.00         0.00         0.00
MAIN.s_req                                                                                97         0.00          .           0.00         0.00         0.00
MAIN.s_fetch                                                                               4         0.00          .           0.00         0.00         0.00
MAIN.s_hdrbytes                                                                        30909         0.00        46.00         0.00         0.00         0.00
MAIN.s_bodybytes                                                                     2142154         0.00      3245.00         0.00         0.00         0.00
MAIN.backend_req                                                                           4         0.00          .           0.00         0.00         0.00
MAIN.n_vcl                                                                                 1         0.00          .           0.00         0.00         0.00
MAIN.bans                                                                                  1         0.00          .           1.00         1.00         1.00
MGT.uptime                                                                               660         1.00         1.00         1.00         1.00         1.00
SMA.s0.c_req                                                                               7         0.00          .           0.00         0.00         0.00
SMA.s0.c_bytes                                                                       1993075         0.00      3019.00         0.00         0.00         0.00
SMA.s0.g_alloc                                                                             7         0.00          .           7.00         7.00         7.00
SMA.s0.g_bytes                                                                       1993075         0.00          .     1993075.00   1993075.00   1993075.00
SMA.s0.g_space                                                                    1071748749         0.00          .   1071748749.0 1071748749.0 1071748749.0
0

4.0版本的配置文件语法与以往的语法有一定的区别,需要注意一下。

官方地址:https://www.varnish-cache.org/docs/trunk/index.html