Varnish调优
目的
通过调优Varnish的配置文件,可以有效的提高服务器的性能。
方法
- 配置Varnish的配置文件default.vcl中的default,vcl_recv,vcl_backend_response,vcl_pipe,vcl_pass,vcl_hash,vcl_hit,vcl_miss,vcl_deliver模块。文件路径:“/usr/local/varnish/config/default.vcl”。配置示例见default.vcl配置文件示例。配置项说明如表 Varnish配置项说明所示。
表1 Varnish配置项说明 配置项
说明
default
配置通信IP及端口。
vcl_recv
用于接收和处理请求;当请求到达Varnish,通过判断请求的数据来决定如何处理请求。
vcl_backend_response
获得后端主机的响应后,调用。
vcl_pipe
用于将请求直接传递至后端主机,并将后端响应原封不动返回给客户端。
vcl_pass
用于将请求直接传递给后端主机,但后端主机的响应并不缓存,而是直接返回给客户端。
vcl_hash
在vcl_recv调用后为请求创建一个hash值时,调用。此hash值将作为Varnish中hash表的key。
vcl_hit
在缓存中找到请求的内容后自动调用。
vcl_miss
在缓存中没有找到请求的内容后自动调用。用于判断是否需要从后端服务器获取内容。
vcl_deliver
将在缓存中找到的请求的内容发送给客户端前调用。
- 对Varnish进程进行绑核并启动。
以物理机1P场景为例,使用numactl设置CPU与内存的亲和性。命令参数说明参考表 命令参数说明。
1
numactl -C 0-2,4-10,12-18,20-26,28-34,36-42,44-47 --membind=0,1 /usr/local/varnish/sbin/varnishd -P /tmp/varnish.pid -a :12345 -T 127.0.0.1:6082 -s malloc,100GB -f /usr/local/varnish/config/default.vcl -t 600 -p thread_pool_max=5000 -p thread_pools=10 -p thread_pool_destroy_delay=3 -p timeout_idle=75 -p thread_pool_min=700
表2 命令参数说明 参数名称
参数说明
-C
进程绑定CPU。
--membind
分配进程的内存,其中0,1表示分配的内存在node0和node1上。
-P
Varnish进程pid文件存放路径。
-a
表示Varnish对httpd的监测地址及端口。
-T
设定Varnish的telnet管理地址及其端口。
-s
指定Varnish缓存存放的方式,常用的方式有:“-s file,<dir_or_file>,<size>”。
-f
指定Varnish的配置文件位置。
-t
指定默认的TTL值。
-p thread_pool_max
每个线程池创建的最大线程数。
-p thread_pools
设置线程池个数。
-p thread_pool_destroy_delay
摧毁线程的延迟时间,也就是摧毁之前需要犹豫一下的时间。
-p timeout_idle
设置保持链接的空闲时长。
-p thread_pool_min
每个线程池创建的最小线程数。
default.vcl配置文件示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | # This is an example VCL file for Varnish. # Marker to tell the VCL compiler that this VCL has been adapted to the # new 4.0 format. vcl 4.0; import directors; # Default backend definition. Set this to point to your content server. backend default { .host = "xxx.xxx.xx.xx"; .port = "80"; } sub vcl_recv { if (req.http.x-forwarded-for) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip; } else { set req.http.X-Forwarded-For = client.ip; } ##if request type is not GET、HEAD、PUT、POST、TRACE、OPTIONS、DELETE,acess “pipe” mode if (req.method != "GET" && req.method != "HEAD" && req.method != "PUT" && req.method != "POST" && req.method != "TRACE" && req.method != "OPTIONS" && req.method != "DELETE") { /* Non-RFC2616 or CONNECT which is weird. */ return (pipe); } ##if request type is not GET or HEAD,it will access pass mode if (req.method != "GET" && req.method != "HEAD") { return (pass); } if (req.http.Authorization || req.http.Cookie) { return (pass); } } sub vcl_backend_response { set beresp.ttl = 120m; } sub vcl_pipe { return (pipe); } sub vcl_pass { if (req.method == "PURGE"){ return(synth(502,"PURGE on a passed object")); } } sub vcl_hash { hash_data(req.url); if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } return (lookup); } sub vcl_hit { return (deliver); } sub vcl_miss { return (fetch); } sub vcl_deliver { set req.http.Connection = "keep-alive"; if (obj.hits > 0){ set resp.http.X-Cache = "HIT from" + server.ip; } else{ set resp.http.X-Cache = "MISS"; } } |