Concurrency Level: 100
Time taken for tests: 4.685 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Non-2xx responses: 10000
Total transferred: 4600000 bytes
HTML transferred: 2390000 bytes
Requests per second: 2134.44 [#/sec] (mean)
Time per request: 46.851 [ms] (mean)
Time per request: 0.469 [ms] (mean, across all concurrent requests)
Transfer rate: 958.83 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 4.8 0 51
Processing: 10 45 7.4 46 67
Waiting: 1 44 7.8 46 67
Total: 12 47 6.5 46 89
Percentage of the requests served within a certain time (ms)
50% 46
66% 47
75% 48
80% 49
90% 53
95% 58
98% 63
99% 67
100% 89 (longest request)
[root@Varnish Desktop]# yum install varnish
[root@Varnish Desktop]# vim /etc/varnish/varnish.params
RELOAD_VCL=1
VARNISH_VCL_CONF=/etc/varnish/default.vcl
VARNISH_LISTEN_PORT=6081
VARNISH_ADMIN_LISTEN_ADDRESS=10.10.0.23
VARNISH_ADMIN_LISTEN_PORT=6082
VARNISH_SECRET_FILE=/etc/varnish/secret
VARNISH_STORAGE="file,/data/cache,1G" #需要先创建好/data目录
VARNISH_USER=varnish
VARNISH_GROUP=varnish
[root@Varnish Desktop]# vim /etc/varnish/default.vcl #修改配置文件,添加VCL规则
vcl 4.0;
import directors;
probe web_healthchk { #定义后端健康监测机制
.url="/index.html";
.interval=2s;
.timeout=1s;
.window=5;
.threshold=3;
}
backend RS1 { #定义后端RS1
.host="10.10.0.21";
.port="80";
.probe=web_healthchk;
}
backend RS2 { #定义后端RS2
.host="10.10.0.22";
.port="80";
.probe=web_healthchk;
}
sub vcl_init { #初始化服务器
new WEBGROUP=directors.round_robin();
WEBGROUP.add_backend(RS1);
WEBGROUP.add_backend(RS2);
}
acl PURGERS { #定义可用于purge操作的ip来源
"127.0.0.1";
"10.10.0.0"/24;
}
sub vcl_recv {
if(req.http.Authorization || req.http.Cookie) { #认证及cookie不缓存
return(pass);
}
if(req.method != "GET" && req.method != "HEAD") { #除了get和head以外的请求方法不缓存
return(pass);
}
if(req.url ~ "index.php") { #动态资源不缓存
return(pass);
}
if(req.method == "PURGE") { #purge方法清理缓存
if(client.ip ~ PURGERS) {
return(purge);
}
}
if(req.http.X-Forward-For) { #为发往后端主机添加的请求报文添加X-Forward-For的首部
set req.http.X-Forward-For = req.http.X-Forward-For+","+client.ip;
}else {
set req.http.X-Forward-For = client.ip;
}
set req.backend_hint = WEBGROUP.backend(); #调用服务器组
return(hash);
}
sub vcl_hash {
hash_data(req.url);
}
sub vcl_backend_response { #自定义缓存时长
if(bereq.url ~ "\.(jpg|jpeg|gif|png)$") {
set beresp.ttl = 1d;
}
if(bereq.url ~ "\.(html|css|js)$") {
set beresp.ttl = 12h;
}
if(beresp.http.Set-Cookie) {
set beresp.grace = 600s;
return(deliver);
}
}
sub vcl_deliver {
if(obj.hits > 0) { #为响应报文添加X-Cache的首部,标识缓存是否命中
set resp.http.X-Cache = "Hit from "+server.ip;
}else {
set resp.http.X-Cache = "Miss";
}
}
[root@Varnish Desktop]# systemctl start varnish.service
[root@Varnish Desktop]# ss -tan
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:6081 *:*
LISTEN 0 10 10.10.0.23:6082 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::6081 :::*