部署略……
修改配置文件
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 78 79 80 81 82 83 84 85
| [root@localhost ~]# vim /usr/local/varnish/default.vcl probe health { # 健康检查 .url = "/"; # 健康检查根目录 .timeout = 5s; # 健康检查超时时间 .interval = 1s; # 间隔时间 .window = 5; # 同时开启5个窗口 .threshold = 3; # 至少保证三个窗口正常,为健康 }
backend web { # 后端服务器, .host = "172.16.1.13"; # 后端服务器地址 .port = "80"; # 后端服务端口 .probe = health; # 调用健康检查 }
sub vcl_recv{ # 接受数据 set req.backend_hint = web; # 命中后端 if(req.http.X-Forwarded-For){ # 判断客户端对varnish的请求投不中是和否包含X-Forwarded-FOr这个字段 set req.http.X-Forwarded-For=req.http.X-Forwarded-For + "," + client.ip; # 如果在客户端对varnish的请求头部中包含了X-Forwarded-For这个字段,就说明中间经过了那些代理服务器,为了能够看到客户端的真是的IP地址,name将会在请求投不中添加上客户端的IP地址 } else{ set req.http.X-Forwarded-For=client.ip; # 如果不存在这个字段,将建立这个字段直接是的客户端的IP地址 } if(req.method != "GET" && req.method != "PUT" && req.method != "POST" && req.method != "DELETE" && req.method != "HEAD" ){ # 如果客户端对varnish的请求方式不正常的,那么则进入错误页面 return(pipe); } if(req.url ~ "\.(html|htm|png|jpg)$"){ # 如果客户端对varnish的请求路径中是静态数据,则进入到hash模块中 return(hash); } if(req.url ~ "\.php$"){ # 如果客户端对varnish的请求路径中过滤出是动态的数据 return(pass); # 则进入到pass 模块中 } return(hash); # 其他的情况都进入到 hash 模块中 }
sub vcl_hash{ # 静态数据模块 hash_data(req.url); # 将会hash客户端对varnish请求的路径 if(req.http.host){ # 如果请求中有域名 hash_data(req.http.host); #则 hash域名 } else{ hash_data(server.ip); # 其余情况都 hash IP地址 } }
sub vcl_pass{ # 动态数据的模块 return(fetch); # 直接进入到后端的模块。 }
sub vcl_pipe{ # 错误数据模块 return(pipe); # 直接结束 }
sub vcl_hit{ # 在varnish中的命中数据的模块 return(deliver); # 直接进入澳结束的模块 }
sub vcl_miss{ # 在varnish中未命中的模块 return(fetch); # 直接进入到后端的模块 }
sub vcl_backend_response{ # 后端数据的模块 if(bereq.url ~ "\.php$"){ # 当varnish对后端的请求中过滤出是动态的数据 set beresp.uncacheable = true; # 则后端对varnish 响应不进行缓存 return(deliver); # 直接进入结束的模块 } if(bereq.url ~ "\.html$"){ # 当varnish对后端的请求中过滤出是静态的数据 set beresp.ttl = 300s; # 则后端对varnish 的响应缓存时间为 300s } return(deliver); # 其余进入结束模块 } sub vcl_deliver{ # 结束的模块 if(obj.hits > 0){ # 如果缓存中的命中的次数大于 0 set resp.http.X-Cache = "hit"; # 则在varnish对客户端的响应头部中建立一行命中~~~ } else{ set resp.http.X-Cache = "miss"; # 其余情况则建立一行错过~~~ } return(deliver); # 正常结束 }
|
检查文件语法并启动
1 2 3
| varnishd -C -f /usr/local/varnish/default.vcl # 检查语法 killall -9 varnishd # 如果之前启动过,需要先停掉服务 varnishd -f /usr/local/varnish/default.vcl # 启动服务
|
启动参数
-a:指定varnish服务的监听端口
-f:指定配置文件
-n:指定varnish的工作目录
-P:指定varnish的pid文件的存放位置
-s:指定varnish缓存数据的文件
-T:指定远程管理varnish的ip和端口
-S:指定远程主机管理varnish主机使用的密钥文件
-p:指定varnish的运行参数
例:-p thread_pools=2 # 指定varnish开启的线程池的数量,可以与cpu核心数一致
-p thread_pool_min=50 # 单个线程池的最小线程数
-p thread_pool_max=5000 # 单个线程池的最大线程数
-p thread_pool_timeout=30 # 每个线程的超时时间(单位s)
lru_interval=30 # 默认缓存时间
验证
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
| [root@localhost ~]# curl -I 172.16.1.12 HTTP/1.1 200 OK Date: Sat, 08 Feb 2020 04:58:02 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Fri, 07 Feb 2020 15:21:52 GMT ETag: "6-59dfdf301c530" Content-Length: 6 Content-Type: text/html; charset=UTF-8 X-Varnish: 2 Age: 0 Via: 1.1 varnish-v4 X-Cache: miss # 第一次访问没有缓存,也就是没有命中,hit<0,返回miss Accept-Ranges: bytes Connection: keep-alive
[root@localhost ~]# curl -I 172.16.1.12 HTTP/1.1 200 OK Date: Sat, 08 Feb 2020 04:58:02 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Fri, 07 Feb 2020 15:21:52 GMT ETag: "6-59dfdf301c530" Content-Length: 6 Content-Type: text/html; charset=UTF-8 X-Varnish: 32770 3 Age: 1 Via: 1.1 varnish-v4 X-Cache: hit # 第二次访问直接访问缓存,也就是命中,hit>0,返回hit Accept-Ranges: bytes Connection: keep-alive
|