varnish 配置
环境: Centos 7.4 Varnish4.0
varnish :172.20.100.10 web1:172.20.100.11 web2: 172.20.100.12
1、安装varnish
[root@localhost ~]# yum install varnish -y
2、编辑配置
[root@localhost ~]# vim /etc/varnish/varnish.params
RELOAD_VCL=1 VARNISH_VCL_CONF=/etc/varnish/varnish.vcl #规则配置文件,默认名为default.vcl VARNISH_LISTEN_PORT=80 #监控端口,默认为6081 VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 #管理监听地址 VARNISH_ADMIN_LISTEN_PORT=6082 #管理监听端口 VARNISH_SECRET_FILE=/etc/varnish/secret #管理共享密钥文件 VARNISH_STORAGE="malloc,256M" #Varnish 4中默认使用malloc(即内存)作为缓存对象存储方式; VARNISH_USER=varnish #用户id VARNISH_GROUP=varnish
3、配置vcl
[root@localhost ~]# egrep -v "^$|#" /etc/varnish/default.vcl >/etc/varnish/varnish.vcl
- 简单varnish实例
[root@localhost ~]# vim /etc/varnish/varnish.vcl
vcl 4.0; backend web_server { .host = "172.20.100.11"; .port = "80"; } sub vcl_recv { set req.backend_hint = web_server; } sub vcl_backend_response { }
#添加响应X-Cache首部,显示缓存是否命中(可选)
sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT from " + server.ip; } else { set resp.http.X-Cache = "MISS"; } }
- varnish缓存多个网站
[root@localhost ~]# vim /etc/varnish/varnish.vcl
vcl 4.0; backend web_server1 { .host = "172.20.100.11"; .port = "80"; } backend web_server2 { .host = "host-44-94-206-116.cdn-hk.com"; .port = "80"; } sub vcl_recv { if (req.http.host ~ "(?i)^(bbs.)?lncs.net$") { set req.http.host = "lncs.net"; set req.backend_hint = web_server1; } elsif (req.http.host ~ "(?i)^www.lncs.net$") { set req.backend_hint = web_server2; return(hash); } } sub vcl_backend_response { } sub vcl_deliver { if(obj.hits > 0){ set resp.http.X-Cache = "HIT from"+req.http.host; set resp.http.X-Cache-Hits = obj.hits; }else{ set resp.http.XCache = "MISS from"+req.http.host; } return (deliver); }
4、测试
- 简单varnish
[root@qunniao3 ~]# curl -I 172.20.100.10 HTTP/1.1 200 OK Date: Thu, 17 May 2018 18:41:36 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Thu, 17 May 2018 16:38:11 GMT ETag: "e-56c69760a59c0" Content-Length: 14 Content-Type: text/html; charset=UTF-8 X-Varnish: 11 3 Age: 22 Via: 1.1 varnish-v4 X-Cache: HIT from 172.20.100.10 Connection: keep-alive
- varnish多个网站
[root@qunniao3 ~]# vim /etc/hosts
172.20.100.10 www.lncs.net 172.20.100.10 bbs.lncs.net
[root@qunniao3 ~]# curl -I www.lncs.net
HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Vary: Accept-Encoding Server: Microsoft-IIS/7.5 Link: <http://www.qunniao.net/index.php?rest_route=/>; rel="https://api.w.org/" X-Frame-Options: SAMEORIGIN Set-Cookie: _d_id=4e0f07b1799ce3547109146293e640; Path=/; HttpOnly Date: Thu, 17 May 2018 11:42:44 GMT X-Varnish: 2 Age: 0 Via: 1.1 varnish-v4 XCache: MISS fromwww.lncs.net Connection: keep-alive
[root@qunniao3 ~]# curl -I bbs.lncs.net
HTTP/1.1 200 OK Date: Thu, 17 May 2018 19:40:21 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Thu, 17 May 2018 16:38:11 GMT ETag: "e-56c69760a59c0" Content-Length: 14 Content-Type: text/html; charset=UTF-8 X-Varnish: 32770 Age: 0 Via: 1.1 varnish-v4 XCache: MISS fromlncs.net Connection: keep-alive
温馨提示:如无特殊说明,本站文章均为作者原创,转载请注明出处!