之前在生产环境用的varnish 4.0 ,前几天因新购买google云服务器,安装varnish时候才发现4.0 版本官方yum源已经不能使用了。yum安装过程中,解析xml会返回404。
官网说明如下:
2017-11-15 - Security Advisory: (Unlikely) data leak?
Certain uncommon configurations of Varnish may leak data in synthetic responses from vcl_backend_error{}
We have released Varnish Varnish Cache 4.1.9 and Varnish Cache 5.2.1 to fix this issue.
1. 添加yum源
更新yum后,安装依赖包后 ,执行:yum install varnish即可安装
安装可能遇到的错误:
错误一.
https://packagecloud.io/varnishcache/varnish51/el/6/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 7 – “Failed to connect to 2600:1fa0:c020:101:34db:140
yum使用了ipv6地址更新内容报错。需要关闭ipv6,有2处需要修改:
1. 在/etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
2./etc/yum.conf
ip_resolve=4
3.执行sysctl -p 使配置生效
错误二.
This could mean a connectivity issue in your environment, such as the requirement to configure a proxy,
or a transparent proxy that tampers with TLS security, or an incorrect system clock.
…..
Error: Cannot retrieve repository metadata (repomd.xml) for repository: varnishcache_varnish41. Please verify its path and try again.
网上有种说法是修改:/etc/yum.conf 添加一条记录 sslverify=false,但我添加后依然无效,依然报错.
后发现原来是系统时间不对,执行对时:/usr/sbin/ntpdate pool.ntp.org 后,即可解决
2.修改varnish启动配置
配置文件:/etc/sysconfig/varnish
设置http服务的,ip+端口:
VARNISH_LISTEN_ADDRESS=0.0.0.0
VARNISH_LISTEN_PORT=80
缓存大小,默认为内存缓存。
VARNISH_STORAGE_SIZE=4096M (一般设置为总内存的一半)
配置文件:/etc/init.d/varnishncsa
完毕注意:必须按此日志格式,程序才可以正确运行
LOG_FORMAT=”\”%t %s %h+%{X-Real-IP}i \”%r\” %I %O \”%{Varnish:hitmiss}x\” \”%{Varnish:handling}x\” %D \”%{Referer}i\” \”%{User-agent}i\”\””
DAEMON_OPTS=”-a -w $logfile -D -F $LOG_FORMAT -P $pidfile”
说明:经测试,在nginx https 代理下,须用 X-Real-IP才可以正常记录ip以及访客ip(X-Real-IP需在https 配置)。不能使用%h+%{X-Forwarded-For}i,因其 在线上环境记录ip格式如下:*.*.*.*+*.*.*.* 127.0.0.1,多出的 127.0.0.1 会导致日志分析程序出错.
3.修改时区为东八区:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
4.日志切割:
配置文件:/etc/logrotate.d/varnish
/var/log/varnish/varnishncsa.log {
daily
rotate 7
dateext
missingok
size=1k
postrotate
/bin/kill -HUP `cat /var/run/varnishncsa.pid /run/varnishncsa/varnishncsa.pid 2>/dev/null` 2> /dev/null || true
endscript
}
说明:
红色字体是手工添加的配置
size=1k ,日志文件达到1KB就可以切割了,默认好像是1MB
dateext, 日志文件切割后以日期命名,例如:varnishncsa.log-20180208
系统自动每天调用,不必添加定时任务