试用varnish

简介:

反向代理服务器,适用于对静态资源的访问,比如图片,可通过缓存提高访问速度。

官网https://www.varnish-cache.org/

 

试用心得:

原来项目的图片是通过tomcat来访问的,图片访问量占到所有请求的20%左右。并且图片不多,决大部分都是重复访问。于是想使用缓存来减少磁盘IO的消耗。很自然就用到了varnish,比较新、性能也很强劲的缓存服务器,squid的替换者。

 

不过在redhat企业版5.2rhel5.2)上安装颇费了周折,原因就是rhel5最高只支持varnish 2.0.6。我之前安装过3.0.12.1.6,各种编译报错,解决不了。忽然看到官网的这么一句话:

This means that we can not update Varnish in EPEL so the latest version there is Varnish 2.0.6.

 

安装成功后做了一个和tomcat对比的压力测试,固定访问一张图片的性能提升60%左右。

 

安装:

tar xf varnish-2.0.6.tar.gz
cd varnish-2.0.6
./autogen.sh
./configure --prefix=~/varnish
make && make install

使用的是普通账号安装,安装在家目录的varnish

 

配置:

vi ~/varnish/etc/varnish/default.vcl
backend default {
    .host = "127.0.0.1";
    .port = "28080";
}
sub vcl_recv {
     if(req.url ~ "\.(png|gif|jpg)$") {
        return (lookup);
    }

    return (pass);
}

如果是图片,先查询缓存,其他直接转发到后端服务器。

 

启动:

varnishd -f ~/varnish/etc/varnish/default.vcl -s malloc,128M -a 0.0.0.0:8000 -T 192.168.102.62:8001

监听端口8000,分配128m的内存

 

输出日志:

varnishncsa -w ~/logs/varnish/varnish.log &

访问日志输出在~/logs/varnish/varnish.log

优化Linux内核参数(使用root账号):

vi /etc/sysctl.conf

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

生效:

sysctl -p

 

优化参数:

参考https://www.varnish-cache.org/trac/wiki/Performance

thread_pool_add_delay=2

thread_pools=<Number of cpu cores>

thread_pool_min=<800/number of cpu cores>

thread_pool_max=4000

session_linger=100

 

4cpu的命令:

varnishd -f ~/varnish/etc/varnish/default.vcl -s malloc,128M -a 0.0.0.0:8000 -T 192.168.102.62:8001 -p thread_pool_add_delay=2 -p thread_pools=4 -p thread_pool_min=200 -p thread_pool_max=4000 -p session_linger=100

在我的测试中,比默认参数性能提高10%左右。

压力测试:

提升60%

线程数

200

循环次数

500

总次数

10w

 

每秒请求数

平均响应时间(毫秒)

tomcat

4109

48

varnish

6864

29

提升

67%

47%

线程数

500

循环次数

200

总次数

10w

 

每秒请求数

平均响应时间(毫秒)

tomcat

4109

121

varnish

6505

76

提升

58%

37%

线程数

800

循环次数

125

总次数

10w

 

每秒请求数

平均响应时间(毫秒)

tomcat

3904

204

varnish

6098

131

提升

56%

35%

 

参考:

http://blog.s135.com/post/313/

猜你喜欢

转载自flysnowxf.iteye.com/blog/1172835