varnish安装维护
原贴:http://www.hiadmin.com/varnish%E5%AE%89%E8%A3%85%E7%BB%B4%E6%8A%A4/@H_403_24@
varnish安装维护
各文档所在位置@H_403_24@ 日志文件 /var/log/varnish/varnish.log@H_403_24@ 可执行程序 /usr/local/varnish/bin@H_403_24@ 缓冲文件 /var/vcache@H_403_24@ 配置文件 /usr/local/varnish/vcl.conf@H_403_24@ 启动参数 /etc/sysconfig/varnish@H_403_24@ 启动脚本 /etc/rc.d/init.d/varnish@H_403_24@ /etc/rc.d/init.d/varnishlog
创建www用户和组,以及Varnish缓存文件存放目录(/var/vcache):@H_403_24@ /usr/sbin/groupadd www -g 48@H_403_24@ /usr/sbin/useradd -u 48 -g www www@H_403_24@ mkdir -p /var/vcache@H_403_24@ chmod +w /var/vcache@H_403_24@ chown -R www:www /var/vcache
创建日志文件,并授予www用户权限访问@H_403_24@ mkdir -p /var/log/varnish@H_403_24@ chmod +w /var/log/varnish@H_403_24@ chown -R www:www /var/log/varnish
可能需要安装如下包@H_403_24@ [CENTOS]yum install ncurses-devel@H_403_24@ [UBUNTU]apt-get install libncurses5-dev
编译安装@H_403_24@ ./configure –prefix=/usr/local/varnish@H_403_24@ make@H_403_24@ make install
编辑配置文件@H_403_24@ vi /usr/local/varnish/vcl.conf@H_403_24@ view plaincopy to clipboardprint?@H_403_24@ backend myblogserver {@H_403_24@ set backend.host = “192.168.0.5″;@H_403_24@ set backend.port = “80″;@H_403_24@ }
acl purge {@H_403_24@ “localhost”;@H_403_24@ “127.0.0.1″;@H_403_24@ “192.168.1.0″/24;@H_403_24@ }
sub vcl_recv {@H_403_24@ if (req.request == “PURGE”) {@H_403_24@ if (!client.ip ~ purge) {@H_403_24@ error 405 “Not allowed.”;@H_403_24@ }@H_403_24@ lookup;@H_403_24@ }
if (req.http.host ~ “^www.hiadmin.com”) {@H_403_24@ set req.backend = myblogserver;@H_403_24@ if (req.request != “GET” && req.request != “HEAD”) {@H_403_24@ pipe;@H_403_24@ }@H_403_24@ elseif(req.url ~ “/.(PHP|cgi)($|/?)”) {@H_403_24@ pass;@H_403_24@ }@H_403_24@ else {@H_403_24@ lookup;@H_403_24@ }@H_403_24@ }@H_403_24@ else {@H_403_24@ error 404 “Cache Server”;@H_403_24@ lookup;@H_403_24@ }@H_403_24@ }
sub vcl_hit {@H_403_24@ if (req.request == “PURGE”) {@H_403_24@ set obj.ttl = 0s;@H_403_24@ error 200 “Purged.”;@H_403_24@ }@H_403_24@ }
sub vcl_miss {@H_403_24@ if (req.request == “PURGE”) {@H_403_24@ error 404 “Not in cache.”;@H_403_24@ }@H_403_24@ }
sub vcl_fetch {@H_403_24@ if (req.request == “GET” && req.url ~ “/.(txt|js)$”) {@H_403_24@ set obj.ttl = 3600s;@H_403_24@ }@H_403_24@ else {@H_403_24@ set obj.ttl = 30d;@H_403_24@ }@H_403_24@ }
backend myblogserver {@H_403_24@ set backend.host = “192.168.0.5″;@H_403_24@ set backend.port = “80″;@H_403_24@ }
acl purge {@H_403_24@ “localhost”;@H_403_24@ “127.0.0.1″;@H_403_24@ “192.168.1.0″/24;@H_403_24@ }
sub vcl_recv {@H_403_24@ if (req.request == “PURGE”) {@H_403_24@ if (!client.ip ~ purge) {@H_403_24@ error 405 “Not allowed.”;@H_403_24@ }@H_403_24@ lookup;@H_403_24@ }
if (req.http.host ~ “^www.hiadmin.com”) {@H_403_24@ set req.backend = myblogserver;@H_403_24@ if (req.request != “GET” && req.request != “HEAD”) {@H_403_24@ pipe;@H_403_24@ }@H_403_24@ elseif(req.url ~ “/.(PHP|cgi)($|/?)”) {@H_403_24@ pass;@H_403_24@ }@H_403_24@ else {@H_403_24@ lookup;@H_403_24@ }@H_403_24@ }@H_403_24@ else {@H_403_24@ error 404 “Cache Server”;@H_403_24@ lookup;@H_403_24@ }@H_403_24@ }
sub vcl_hit {@H_403_24@ if (req.request == “PURGE”) {@H_403_24@ set obj.ttl = 0s;@H_403_24@ error 200 “Purged.”;@H_403_24@ }@H_403_24@ }
sub vcl_miss {@H_403_24@ if (req.request == “PURGE”) {@H_403_24@ error 404 “Not in cache.”;@H_403_24@ }@H_403_24@ }
sub vcl_fetch {@H_403_24@ if (req.request == “GET” && req.url ~ “/.(txt|js)$”) {@H_403_24@ set obj.ttl = 3600s;@H_403_24@ }@H_403_24@ else {@H_403_24@ set obj.ttl = 30d;@H_403_24@ }@H_403_24@ }
启动varnish@H_403_24@ ulimit -SHn 51200@H_403_24@ /usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
启动varnishncsa记录访问日志@H_403_24@ /usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/log/varnish/varnish.log &
优化linux内核@H_403_24@ vi /etc/sysctl.conf@H_403_24@ 添加以下内容@H_403_24@ view plaincopy to clipboardprint?@H_403_24@ net.ipv4.tcp_fin_timeout = 30@H_403_24@ net.ipv4.tcp_keepalive_time = 300@H_403_24@ net.ipv4.tcp_syncookies = 1@H_403_24@ net.ipv4.tcp_tw_reuse = 1@H_403_24@ net.ipv4.tcp_tw_recycle = 1@H_403_24@ net.ipv4.ip_local_port_range = 5000 65000
net.ipv4.tcp_fin_timeout = 30@H_403_24@ net.ipv4.tcp_keepalive_time = 300@H_403_24@ net.ipv4.tcp_syncookies = 1@H_403_24@ net.ipv4.tcp_tw_reuse = 1@H_403_24@ net.ipv4.tcp_tw_recycle = 1@H_403_24@ net.ipv4.ip_local_port_range = 5000 65000
执行优化@H_403_24@ sysctl -p
编辑varnish启动参数@H_403_24@ # vi /etc/sysconfig/varnish@H_403_24@ 修改
配置启动脚本@H_403_24@ # vi $home/varnish-1.1.2/redhat/varnish.initrc@H_403_24@ 修改@H_403_24@ DAEMON=”/usr/local/varnish/sbin/varnishd”@H_403_24@ # cp varnishlog.initrc /etc/rc.d/init.d/varnish
配置varnishlog启动脚本@H_403_24@ # vi $home/varnish-1.1.2/redhat/varnishlog.initrc@H_403_24@ 修改@H_403_24@ DAEMON=”/usr/local/varnish/bin/varnishlog”@H_403_24@ LOGFILE=”/var/log/varnish/varnish.log”@H_403_24@ CACHEFILE=”/var/vcache”@H_403_24@ DAEMON_OPTS=”-a -n ${CACHEFILE} -w ${LOGFILE} -D -P $PIDFILE”@H_403_24@ # chmod 755 varnishlog.initrc@H_403_24@ # cp varnishlog.initrc /etc/rc.d/init.d/varnishlog
启动varnish@H_403_24@ /etc/rc.d/init.d/varnish start@H_403_24@ /etc/rc.d/init.d/varnishlog start
查看varnish状态@H_403_24@ /usr/local/varnish/bin/varnishstat -n /var/vcache
通过varnish管理端口进行管理@H_403_24@ /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500
通过正则清楚缓存@H_403_24@ /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge 正则表达式
/*附录摘自leftleg.hzpub.com*/@H_403_24@ *********************************************************************************************@H_403_24@ man page意译如下:
VCL语法比较简单,和C类似,if(){}的形式,=和==的区别,!、&&和||等等。但/符号没有特别的意思。@H_403_24@ VCL里除了用==、!、&&、||做逻辑判断意外,还可以用~来表示与正则表达式或ACL的匹配。@H_403_24@ VCL其实只是配置,并不是真正的编程语言,没有循环,没有自定义变量。
声明Backend@H_403_24@ backend 名称 {@H_403_24@ set backend.host = “域名”;@H_403_24@ set backend.port = “端口”;@H_403_24@ }@H_403_24@ 比如@H_403_24@ backend www {@H_403_24@ set backend.host = “www.example.com”;@H_403_24@ set backend.port = “http”;@H_403_24@ }@H_403_24@ 声明的Backend可以用在判断请求针对哪个后端服务器@H_403_24@ if (req.http.host ~ “^(www.)?example.com$”) {@H_403_24@ {@H_403_24@ set req.backend = www;@H_403_24@ }
声明ACL@H_403_24@ acl 名称 {@H_403_24@ “IP”;@H_403_24@ “IP子网”/反掩码位数;@H_403_24@ ! “IP或IP子网”/反掩码位数;@H_403_24@ }@H_403_24@ 比如@H_403_24@ acl local {@H_403_24@ “locahost”; /* myself */@H_403_24@ “10.0.0.1″/8; /* and everyone on the local network */@H_403_24@ ! “10.0.0.23″; /* except for the dialin router */@H_403_24@ }@H_403_24@ 判断ACL也很简单@H_403_24@ if (client.ip ~ local) {@H_403_24@ pipe;@H_403_24@ }
还可以定义子程序@H_403_24@ sub pipe_if_local {@H_403_24@ if (client.ip ~ local) {@H_403_24@ pipe;@H_403_24@ }@H_403_24@ }@H_403_24@ 用call来调用@H_403_24@ call pipe_if_local;
内置的例程@H_403_24@ vcl_recv@H_403_24@ 有请求到达后成功接收并分析时被调用,一般以以下几个关键字结束。@H_403_24@ error code [reason] 返回code给客户端,并放弃处理该请求@H_403_24@ pass 进入pass模式,把控制权交给vcl_pass@H_403_24@ pipe 进入pipe模式,把控制权交给vcl_pipe@H_403_24@ lookup 在缓存里查找被请求的对象,根据查找结果把控制权交给vcl_hit或vcl_miss
vcl_pipe@H_403_24@ 进入pipe模式时被调用。请求被直接发送到backend,后端和客户端之间的后继数据不进行处理,只是简单传递,直到一方关闭连接。一般以以下几个关键字结束。@H_403_24@ error code [reason]@H_403_24@ pipe
vcl_pass@H_403_24@ 进入pass模式时被调用。请求被送到后端,后端应答数据送给客户端,但不进入缓存。同一连接的后继请求正常处理。一般以以下几个关键字结束。@H_403_24@ error code [reason]@H_403_24@ pass
vcl_hash@H_403_24@ 目前不使用
vcl_hit@H_403_24@ 在lookup以后如果在cache中找到请求的内容事调用。一般以以下几个关键字结束。@H_403_24@ error code [reason]@H_403_24@ pass@H_403_24@ deliver 将找到的内容发送给客户端,把控制权交给vcl_deliver.
vcl_miss@H_403_24@ lookup后但没有找到缓存内容时调用,可以用于判断是否需要从后端服务器取内容。一般以以下几个关键字结束。@H_403_24@ error code [reason]@H_403_24@ pass@H_403_24@ fetch 从后端取得请求的内容,把控制权交给vcl_fetch.
vcl_fetch@H_403_24@ 从后端取得内容后调用。一般以以下几个关键字结束。@H_403_24@ error code [reason]@H_403_24@ pass@H_403_24@ insert 将取到的内容插入缓存,然后发送给客户端,把控制权交给vcl_deliver
vcl_deliver@H_403_24@ 缓存内容发动给客户端前调用。一般以以下几个关键字结束。@H_403_24@ error code [reason]@H_403_24@ deliver 内容发送给客户端
vcl_timeout@H_403_24@ 在缓存内容到期前调用。一般以以下几个关键字结束。@H_403_24@ fetch 从后端取得该内容@H_403_24@ discard 丢弃该内容
vcl_discard@H_403_24@ 由于到期或者空间不足而丢弃缓存内容时调用。一般以以下几个关键字结束。@H_403_24@ discard 丢弃@H_403_24@ keep 继续保留在缓存里
如果这些内置例程没有被定义,则执行缺省动作
一些内置的变量@H_403_24@ now 当前时间,标准时间点(1970?)到现在的秒数
backend.host 后端的IP或主机名@H_403_24@ backend.port 后端的服务名或端口
请求到达后有效的变量@H_403_24@ client.ip 客户端IP@H_403_24@ server.ip 服务端IP@H_403_24@ req.request 请求类型,比如GET或者HEAD或者POST@H_403_24@ req.url 请求的URL@H_403_24@ req.proto 请求的HTTP版本号@H_403_24@ req.backend 请求对应的后端@H_403_24@ req.http.header 对应的HTTP头
往后段的请求时有效的变量@H_403_24@ bereq.request 比如GET或HEAD@H_403_24@ bereq.url URL@H_403_24@ bereq.proto 协议版本@H_403_24@ bereq.http.header HTTP头
从cache或后端取到内容后有效的变量@H_403_24@ obj.proto HTTP协议版本@H_403_24@ obj.status HTTP状态代码@H_403_24@ obj.response HTTP状态信息@H_403_24@ obj.valid 是否有效的HTTP应答@H_403_24@ obj.cacheable 是否可以缓存的内容,也就是说如果HTTP返回是200、203、300、301、302、404、410并且有非0的生存期,则为可缓存@H_403_24@ obj.ttl 生存期,秒@H_403_24@ obj.lastuse 上一次请求到现在间隔秒数
对客户端应答时有效的变量@H_403_24@ resp.proto response的HTTP版本@H_403_24@ resp.status 回给客户端的HTTP状态代码@H_403_24@ resp.response 回给客户端的HTTP状态信息@H_403_24@ resp.http.header HTTP头
变量可以通过set来赋值或通过remove来删除(清空)@H_403_24@ sub vcl_recv {@H_403_24@ if (req.http.host ~ “^(www.)?example.com$”) {@H_403_24@ set req.http.host = “www.example.com”;@H_403_24@ }@H_403_24@ }
sub vcl_fetch {@H_403_24@ remove obj.http.Set-Cookie;@H_403_24@ }
##########关于varnishd的启动@H_403_24@ 进入 /home/admin/varnishd/sbin/,使用 varnishd启动@H_403_24@ 启动参数说明@H_403_24@ -a address:port # varnishd httpd监听地址及其端口@H_403_24@ -b address:port # 后台服务器地址及其端口@H_403_24@ # -b@H_403_24@ # -b ‘:’@H_403_24@ -d # 使用debug模式@H_403_24@ -f file # varnishd 服务器存取规则文件@H_403_24@ -F # Run in foreground@H_403_24@ -h kind[,hashoptions] # Hash specification@H_403_24@ # -h simple_list@H_403_24@ # -h classic [default]@H_403_24@ # -h classic,@H_403_24@ -n dir # varnishd working directory@H_403_24@ -P file # PID file@H_403_24@ -p param=value # 服务器参数,用来优化性能@H_403_24@ -s kind[,storageoptions] # 缓存内容存放方式@H_403_24@ # -s malloc@H_403_24@ # -s file [default: use /tmp]@H_403_24@ # -s file,@H_403_24@ # -s file,@H_403_24@ -t # Default TTL@H_403_24@ -T address:port # telnet管理地址及其端口@H_403_24@ -V # version@H_403_24@ -w int[,int[,int]] # 工作线程数@H_403_24@ # -w@H_403_24@ # -w min,max@H_403_24@ # -w min,max,timeout [default: -w1,1000,120]@H_403_24@ 一般使用varnishd -a address:port -b address:port 其他使用默认即可启动@H_403_24@ 注意:vcl 中指定 后台服务器的话就不用使用-b 参数了@H_403_24@ 4.关于vcl文件的使用说明@H_403_24@ vcl是varnishd的存取策略,即varnishd的配置文件@H_403_24@ #基本格式如下指定后台服务器机器端口@H_403_24@ backend www {@H_403_24@ set backend.host = “www.example.com”;@H_403_24@ set backend.port = “http”;@H_403_24@ }@H_403_24@ #acl访问控制@H_403_24@ acl local {@H_403_24@ “locahost”; /* myself */@H_403_24@ “10.0.0.1″/8; /* and everyone on the local network */@H_403_24@ ! “10.0.0.23″; /* except for the dialin router */@H_403_24@ }@H_403_24@ #如果使用虚拟主机,请参照下面代码@H_403_24@ view plaincopy to clipboardprint?@H_403_24@ backend www {@H_403_24@ set backend.host = “www.example.com”;@H_403_24@ set backend.port = “80″;@H_403_24@ }@H_403_24@ backend images {@H_403_24@ set backend.host = “images.example.com”;@H_403_24@ set backend.port = “80″;@H_403_24@ }@H_403_24@ sub vcl_recv {@H_403_24@ if (req.http.host ~ “^(www.)?example.com$”) {@H_403_24@ set req.backend = www;@H_403_24@ } elsif (req.http.host ~ “^images.example.com”) {@H_403_24@ set req.backend = images;@H_403_24@ } else {@H_403_24@ error 404 “Unknown virtual host”;@H_403_24@ }@H_403_24@ }
backend www {@H_403_24@ set backend.host = “www.example.com”;@H_403_24@ set backend.port = “80″;@H_403_24@ }@H_403_24@ backend images {@H_403_24@ set backend.host = “images.example.com”;@H_403_24@ set backend.port = “80″;@H_403_24@ }@H_403_24@ sub vcl_recv {@H_403_24@ if (req.http.host ~ “^(www.)?example.com$”) {@H_403_24@ set req.backend = www;@H_403_24@ } elsif (req.http.host ~ “^images.example.com”) {@H_403_24@ set req.backend = images;@H_403_24@ } else {@H_403_24@ error 404 “Unknown virtual host”;@H_403_24@ }@H_403_24@ }
#关于cache存在时间设置@H_403_24@ sub vcl_fetch {@H_403_24@ if (obj.ttl < 120s) {@H_403_24@ set obj.ttl = 120s;@H_403_24@ }@H_403_24@ }@H_403_24@ #cache图片等内容配置@H_403_24@ sub vcl_recv {@H_403_24@ if (req.request == “GET” && req.url ~ “/.(gif|jpg||jpeg|tom|swf|css|js)$”) {@H_403_24@ lookup;@H_403_24@ }@H_403_24@ lookup;@H_403_24@ }@H_403_24@ ##########关于vcl文件的使用说明@H_403_24@ vcl是varnishd的存取策略,即varnishd的配置文件@H_403_24@ #基本格式如下指定后台服务器机器端口@H_403_24@ backend www {@H_403_24@ set backend.host = “www.example.com”;@H_403_24@ set backend.port = “http”;@H_403_24@ }@H_403_24@ #acl访问控制@H_403_24@ acl local {@H_403_24@ “locahost”; /* myself */@H_403_24@ “10.0.0.1″/8; /* and everyone on the local network */@H_403_24@ ! “10.0.0.23″; /* except for the dialin router */@H_403_24@ }@H_403_24@ #如果使用虚拟主机,请参照下面代码@H_403_24@ backend www {@H_403_24@ set backend.host = “www.example.com”;@H_403_24@ set backend.port = “80″;@H_403_24@ }@H_403_24@ backend images {@H_403_24@ set backend.host = “images.example.com”;@H_403_24@ set backend.port = “80″;@H_403_24@ }@H_403_24@ sub vcl_recv {@H_403_24@ if (req.http.host ~ “^(www.)?example.com$”) {@H_403_24@ set req.backend = www;@H_403_24@ } elsif (req.http.host ~ “^images.example.com”) {@H_403_24@ set req.backend = images;@H_403_24@ } else {@H_403_24@ error 404 “Unknown virtual host”;@H_403_24@ }@H_403_24@ }@H_403_24@ #关于cache存在时间设置@H_403_24@ sub vcl_fetch {@H_403_24@ if (obj.ttl < 120s) {@H_403_24@ set obj.ttl = 120s;@H_403_24@ }@H_403_24@ }@H_403_24@ #cache图片等内容配置@H_403_24@ sub vcl_recv {@H_403_24@ if (req.request == “GET” && req.url ~ “/.(gif|jpg||jpeg|tom|swf|css|js)$”) {@H_403_24@ lookup;@H_403_24@ }@H_403_24@ lookup;@H_403_24@ }@H_403_24@ ########## 关于服务器 param的设置@H_403_24@ param有以下选项@H_403_24@ user root (0)@H_403_24@ group root (0)@H_403_24@ default_ttl 14400 [seconds]@H_403_24@ thread_pools 1 [pools]@H_403_24@ thread_pool_max 12000 [threads]@H_403_24@ thread_pool_min 4000 [threads]@H_403_24@ thread_pool_timeout 10 [seconds]@H_403_24@ overflow_max 100 [%]@H_403_24@ http_workspace 8192 [bytes]@H_403_24@ sess_timeout 5 [seconds]@H_403_24@ pipe_timeout 60 [seconds]@H_403_24@ send_timeout 20 [seconds]@H_403_24@ auto_restart on [bool]@H_403_24@ fetch_chunksize 128 [kilobytes]@H_403_24@ sendfile_threshold unlimited [bytes]@H_403_24@ vcl_trace off [bool]@H_403_24@ listen_address 172.16.189.1:3128@H_403_24@ listen_depth 1024 [connections]@H_403_24@ srcaddr_hash 1049 [buckets]@H_403_24@ srcaddr_ttl 720 [seconds]@H_403_24@ backend_http11 on [bool]@H_403_24@ client_http11 on [bool]@H_403_24@ ping_interval 3 [seconds]@H_403_24@ 大家可以使用-p参数在启动时候进行配置和优化@H_403_24@ 例如@H_403_24@ /home/admin/varnish/sbin/varnishd -f /etc/varnish/vcl.conf /@H_403_24@ -a 172.16.189.1:3128 /@H_403_24@ -s malloc /@H_403_24@ -p user root -p group root /@H_403_24@ -p default_ttl 14400 -p thread_pool_max 8000 -p send_timeout 20 /@H_403_24@ -p srcaddr_ttl 720 -p backend_http11 on -p client_http11 on /@H_403_24@ -w 4000,12000,10 -T 127.0.0.1:8080
########## 关于varnishd的管理@H_403_24@ 管理功能的启用需要在启动varnishd的时候 启动 -T参数指定 telnet管理使用的地址和端口@H_403_24@ 使用telnet localhost 8080,然后输入help参看相关的管理选项@H_403_24@ 或者使用 /home/admin/varnishd/bin/varnishadm -T localhost:8080 cmd进行管理@H_403_24@ 使用/home/admin/varnishd/bin/varnishstat 来查看varnishd的运行情况
########## 关于log 使用home/admin/varnishd/bin/varnishlog 和varnishncsa查看服务器访问log或者让其输出到文件来记录log