- varnish安装与查看:
(1)Node1:
yum info varnish
rpm -ql varnish
yum install varnish -y
rpm -ql varnish
cat /etc/varnish/varnish.params
man varnishd
cd /etc/varnish/
ls - 配置应用程序工作特性(参数):
(1)Node1:
vim varnish.params
VARNISH_STORAGE=“malloc,256M”#基于内存的方式缓存 - 配置主配置文件:
(1)Node1:
vim default.vcl
backend default {
.host = “192.168.184.10”;#后端主机及其监听端口
.port = “80”;
}
systemctl start varnish.service
ss -tnl
iptables –F - 配置web服务:
(1)Node2:
yum install -y httpd
for i in {1…10};do echo “<h1>page $i on web1</h1>” > /var/www/html/test$i.html;done
ls /var/www/html/
service httpd start
ss –tnl - varnish的命令行工具:
(1)Node1:
varnishadm -h
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082#配置varnish的CLI
varnishlog#详细varnish日志
varnishncsa #普通格式varnish日志
varnishtop#各种信息排序
varnishstat#统计信息
二. Varnish状态引擎配置:
- 配置varnish接收及分发报文引擎的规则:
(1)Node1:
Copy /etc/varnish/default.vcl test.vcl
vim /etc/varnish/test.vcl
sub vcl_recv {
if(req.method == “PRI”){
return(synth(405));
}
if(req.method != “GET” &&
req.method != “HEAD” &&
req.method != “PUT” &&
req.method != “POST” &&
req.method != “TRACE” &&
req.method != “OPTIONS” &&
req.method != “DELETE”){
return(pipe);
}
if(req.method != “GET” &&
req.method != “HEAD”){
return(pass);
}
if(req.http.Authorization || req.http.cookie){
return(pass);
}
return(hash);
}
sub vcl_deliver {
if(obj.hits > 0){
set resp.http.X-Cache = “HIT from” + server.ip;
}
else {
set resp.http.X-Cache = “MISS from” + server.ip;
}
} - 用工具编译使用主配置文件vcl:
(1)Node1:
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.list
vcl.load test1 test.vcl
vcl.use test1 - 最后在别的主机上验证:
(1)Node3:
curl -I http://192.168.184.20:6081/test1.html
curl -I http://192.168.184.20:6081/test1.html
三.Varnish配置扩展:
-
强制对某资源请求不检查缓存:
(1)Node1:
cd /etc/varnish/
vim test.vcl
sub vcl_recv {
if(req.url ~ “(?i)^/login” || req.url ~ “(?i)^/admin”){
return(pass);
}
}
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.list
vcl.load test2 test.vcl
vcl.use test2
(2)node2:
mkdir /var/www/html/admin
cd /var/www/html/admin
vim index.html
from admin page
(3)下载几张图片1.jpg -
最后在别的主机上验证:
(1)Node3:
curl -I http://192.168.184.20:6081/admin/index.html
curl -I http://192.168.184.20:6081/admin/index.html -
对特定资源取消其私有cookie标识,并强行设定其varnish缓存时长:
(1)Node1:
vim test.vcl
sub vcl_backend_response {
if(beresp.http.Cache-Control !~ “S-maxage”){
if(bereq.url ~ “(?i).jpg”){
set beresp.http.ttl = 3600;
unset beresp.http.Set-Cookie;
}
if(bereq.url ~ “(?i).css”){
set beresp.http.ttl = 600;
unset beresp.http.Set-Cookie;
}
}
}
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.list
vcl.load test3 test.vcl
vcl.use test3 -
最后在别的主机上验证:
(1)Node3:
curl -I http://192.168.184.20:6081/1.jpg
curl -I http://192.168.184.20:6081/1.jpg
curl -I http://192.168.184.20:6081/test1.html
(2)过一会
curl -I http://192.168.184.20:6081/1.jpg
curl -I http://192.168.184.20:6081/test1.html -
对后端主机做健康状态检测并实现资源分离:
(1)Node1:
vim test.vcl
backend websrv1 {
.host = “192.168.184.10”;
.port = “80”;
.probe = { .url = “/test1.html” ;}
}
backend websrv2 {
.host = “192.168.184.11”;
.port = “80”;
.probe = { .url = “/test1.html” ;}
}
sub vcl_recv {
if(req.url ~ “(?i).(jpg|png|gif)”){
set req.backend_hint = websrv1;
}
else { set req.backend_hint = websrv2;}
}
(2)node3:
yum install -y httpd
for i in {1…10};do echo “<h1>page $i on web2</h1>” > /var/www/html/test$i.html;done
ls /var/www/html/
service httpd start
ss –tnl
下载图片1.jpg
(3)node1:
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.list
vcl.load test4 test.vcl
vcl.use test4
backend.list -
负载均衡(相当于url的负载均衡)
(1)Node1:
vim test.vcl
import directors;
sub vcl_init {
new mycluster = directors.round_robin();
mycluster.add_backend (websrv1);
mycluster.add_backend (websrv2);
}
sub vcl_recv {
set req.backend_hint = mycluster.backend();
}
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.list
vcl.load test5 test.vcl
vcl.use test5 -
在其他主机上验证:
(1)Node3:
curl -I http://192.168.184.20:6081/test1.html
curl -I http://192.168.184.20:6081/test2.html
curl -I http://192.168.184.20:6081/test3.html
curl -I http://192.168.184.20:6081/test4.html -
varnish工具:
(1)Node1:
man varnishlog
man varnishstat
varnishstat -l
varnishstat –f MAN.cache_hint(命中次数)/ MAN.sess_conn(已处理的请求数)/ MAN.sess_drop(与丢弃的请求数)