配置说明
Node1:varnish服务端,IP:192.168.1.131 CentOS7.2
Node2: 测试客户端 IP:192.168.1.132 CentOS7.2
1、安装varnish服务
安装varnish
[iyunv@Node1 ~]# yum -y install varnish #需要EPEL源
[iyunv@Node1 ~]# cd /etc/varnish/
[iyunv@Node1 varnish]# vim varnish.params
[iyunv@Node1 varnish]# vim default.vcl
修改
.host = "127.0.0.1"; #约17行
为
.host = "192.168.1.132";
修改
.port = "8080"; #约18行
为
.port = "80";
[iyunv@node1 ~]# systemctl start varnish.service
2、准备客户端
[iyunv@Node2 ~]# yum -y install httpd
[iyunv@Node2 ~]# for i in {1..10};do echo "Page $i on Web1" > /var/www/html/test$i.html;done
[iyunv@Node2 ~]# systemctl start httpd.service
3、测试
在浏览器打开
http://192.168.1.131:6081/test1.html
[iyunv@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
测试1
[iyunv@node1 ~]# cd /etc/varnish/
[iyunv@node1 varnish]# cp default.vcl test.vcl
[iyunv@node1 varnish]# vim test.vcl
在sub vcl_recv添加如下内容(约26行左右)
if (req.method == "PRI") {
/* We do not support SPDY or HTTP/2.0 */
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") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.method != "GET" && req.method != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
return (hash);
[iyunv@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test1 test.vcl
200
VCL compiled.
vcl.list
200
active 0 boot
available 0 test1
vcl.use test1
200
VCL 'test1' now active
vcl.show test1
200
测试2
[iyunv@node1 varnish]# vim test.vcl
在sub vcl_deliver(约60行左右) 段添加如下内容:
if (obj.hits>0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
[iyunv@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test2 test.vcl
200
VCL compiled.
vcl.use test2
200
VCL 'test2' now active
测试结果
[iyunv@node3 ~]# curl http://192.168.1.131:6081/test5.html
Page 5 on Web1
[iyunv@node3 ~]# curl http://192.168.1.131:6081/test5.html
Page 5 on Web1
[iyunv@node3 ~]# curl http://192.168.1.131:6081/test5.html
Page 5 on Web1
[iyunv@node3 ~]# curl -I http://192.168.1.131:6081/test5.html
HTTP/1.1 200 OK
Date: Sat, 10 Sep 2016 13:36:31 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 09 Sep 2016 09:55:12 GMT
ETag: "f-53c102475b461"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 32784 13
Age: 35
Via: 1.1 varnish-v4
X-Cache: HIT
Connection: keep-alive
总结:由于该网页之前被访问过,故X-Cache的值为HIT
[iyunv@node3 ~]# curl -I http://192.168.1.131:6081/test6.html
HTTP/1.1 200 OK
Date: Sat, 10 Sep 2016 13:37:56 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 09 Sep 2016 09:55:12 GMT
ETag: "f-53c102475b461"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 32786
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS
Connection: keep-alive
总结:由于该网页之前未被访问过,故X-Cache的值MISS
[iyunv@node3 ~]# curl -I http://192.168.1.131:6081/test6.html
HTTP/1.1 200 OK
Date: Sat, 10 Sep 2016 13:37:56 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 09 Sep 2016 09:55:12 GMT
ETag: "f-53c102475b461"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 15 32787
Age: 25
Via: 1.1 varnish-v4
X-Cache: HIT
Connection: keep-alive
总结:网页被访问过后,X-Cache的值就变为HIT
测试3(引入变量)
[iyunv@node1 varnish]# vim test.vcl
在sub vcl_deliver(约65行左右) 段添加如下内容:
if (obj.hits>0) {
set resp.http.X-Cache = "HIT from " + server.ip;
} else {
set resp.http.X-Cache = "MISS from " + server.ip;
}
[iyunv@node1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test3 test.vcl
200
VCL compiled.
vcl.use test3
200
VCL 'test3' now active
[iyunv@node3 ~]# curl -I http://192.168.1.131:6081/test7.html
HTTP/1.1 200 OK
Date: Sat, 10 Sep 2016 14:09:05 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 09 Sep 2016 09:55:12 GMT
ETag: "f-53c102475b461"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 32789
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from 192.168.1.131
Connection: keep-alive
[iyunv@node3 ~]# curl -I http://192.168.1.131:6081/test7.html
HTTP/1.1 200 OK
Date: Sat, 10 Sep 2016 14:09:05 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Fri, 09 Sep 2016 09:55:12 GMT
ETag: "f-53c102475b461"
Content-Length: 15
Content-Type: text/html; charset=UTF-8
X-Varnish: 17 32790
Age: 33
Via: 1.1 varnish-v4
X-Cache: HIT from 192.168.1.131
Connection: keep-alive
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com