【去除干扰环境】关停:本机的httpd,haproxy,nginx iptables
【varnish 优秀的缓存服务器】
特点反向代理,负载均衡,健康状态检查
连接弱于nginx,nginx比varnish反向代理好
【看看】
curl -I www.sohu.com Cache-Control: no-transform, max-age=120
【安装】
varnish-3.0.4-1.el6.x86_64.rpm varnish-libs-3.0.4-1.el6.x86_64.rpm rpm -ivh varnish-*
【启动服务】
service varnish restart
【端口监听】
[root@localhost home]# netstat -tnlp | grep "var" tcp 0 0 0.0.0.0:6081 0.0.0.0:* LISTEN 9876/varnishd tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 9875/varnishd tcp 0 0 :::6081 :::* LISTEN 9876/varnishd
【使用基于本机内存做缓存大小】
vim /etc/sysconfig/varnish
修改
VARNISH_STORAGE_SIZE=1G # # # Backend storage specification VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}" *********************修改为************************************************** VARNISH_STORAGE_SIZE=256M # # # Backend storage specification VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}" VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}" *****************************************************************************
【重启服务】service varnish restart
【本机做缓存,后台web服务】
vim /etc/varnish/default.vcl
【改】
backend default { .host = "127.0.0.1"; .port = "80"; } backend default { .host = "192.168.3.5"; .port = "80"; } # service varnish restart
【确保后台web服务 已启动】
http://172.16.0.102 失败 http://172.16.0.102:6081 这才是正常的
【后面两台当负载均衡】
备份
cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak
修改
vim /etc/varnish/default.vcl
随机访问,加权重
backend server1 { .host = "192.168.3.5"; .port = "80"; } backend server2 { .host = "192.168.3.6"; .port = "80"; } director myload random { { .backend = server1; .weight = 3; } { .backend = server2; .weight = 1; } } sub vcl_recv { set req.backend = myload; }
【去除干扰环境】关停:本机的httpd,haproxy,nginx iptables
【测试OK】http://172.16.0.102:6081/
【varnishadm管理】
varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret
help 帮助,quite退出
【启用日志管理】不建议开启varnish的日志,IO操作影响磁盘
service varnishlog restart service varnishncsa restart cat /var/log/varnish/varnishncsa.log
访问http://172.16.0.102:6081/试试
172.16.0.101 - - [30/May/2015:09:44:53 +0800] "GET http://172.16.0.102:6081/ HTTP/1.1" 200 40 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"
---------------------------------------------------------------------