---
title: "安装、测试 Varnish"
author: "amornio19840908"
date: "2012-12-27T11:34:05Z"
tags: []
categories: ["互联网"]
slug: "varnish"
draft: false
lastmod: "2012-12-27T15:24:12Z"
id: 1754896
url: "https://amornio.iteye.com/blog/1754896"
---

Varnish的安装依赖pcre
下载获得最新版本的pcre
安装pcre

```java

> tar -xjvf pcre-8.32.tar.bz2
> cd pcre-8.32
> ./configure --prefix=/usr/local/pcre/
> make && make install

```


为Varnish准备目录

```java

> mkdir data
> mkdir data/varnish
> mkdir data/varnish/cache
> mkdir data/varnish/log

```


下载Varnish，安装

```java

> wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
> tar -zxvf varnish-3.0.3.tar.gz
> cd varnish-3.0.3
> export PKG_CONFIG_PATH=/usr/local/pcre/lib/pkgconfig
> ./configure --prefix=/usr/local/varnish --enable-dependency-trackin --enable-debugging-symbols --enable-developer-warnings
> make && make install
> cp redhat/varnish.initrc  /etc/init.d/varnish
> cp redhat/varnish.sysconfig  /etc/sysconfig/varnish

```


注：export PKG_CONFIG_PATH=/usr/local/pcre/lib/pkgconfig 为pcre的安装目录，可根据pcre的实际安装目录自定义。

创建简单配置文件
在/etc目录下创建vcl.conf的配置文件，内容如下

```java

backend default {
     .host = "127.0.0.1"; // 可用hostname，IP，指向需要代理的web server
     .port = "80";
}

```


启动

```java

./sbin/varnishd -f /usr/local/varnish/etc/vcl.conf -s malloc,2G -w 512

```


测试
在浏览器上访问Varnish所在机器的IP，如果配置正确，则会跳转到Varnish代理的Web Server。

管理工具
/bin目录下多个实用工具:
1), varnishstat，可查看miss，hit等实施情况。
2), varnishtop，可查看varnish被使用的top。

关闭

```java

pkill varnish

```


--------------------------------------------------------------------------------
华丽的分割线，下面通过Web Bench对Varnish做压力测试。
--------------------------------------------------------------------------------
Varnish部署的机器
Dell 1950

Web Bench部署的机器
Dell 620

千兆交换机。

---------------------------------------------------------
测试文件大小：288bytes

webbench -c 5000 -t 30

测试结果：
Speed=1904732 pages/min, 18751338 bytes/sec.
Requests: 952366 susceed, 0 failed.

测试分析：
Varnish服务器的CPU达到400%左右
Varnish的监控显示，cache_hit率为100%
Web Bench服务器的网络input达到 57526.0 kbits/sec

说明，varnish服务器的cup跑到极限，Web Bench的网络正常。


----------------------------------------------------------
测试文件大小：33.5KB
webbench -c 5000 -t 30

测试结果：
Speed=225220 pages/min, -24989308 bytes/sec.
Requests: 101581 susceed, 11029 failed.

测试分析：
Varnish服务器的CPU达到70%左右
Varnish的监控显示，cache_hit率为100%
Web Bench服务器的网络input达到 955176.0 kbits/sec

说明，varnish服务器的CUP正常，Web Bench的网络带宽被占满。