安装:
yum install epel-release rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm yum install varnish gcc varnish-libs-devel-4.0.3 -y
稍微配置:
vi /etc/sysconfig/varnish 修改listen port为80
vi /etc/varnish/default.vcl 添加backend web server
web缓存核心技术:
http://mp.weixin.qq.com/s?__biz=MzIyMDA1MzgyNw==&mid=2651968866&idx=1&sn=25554c31eac32e78664b962412f515de&chksm=8c349d4ebb431458bef22609db7e9e76d33035ad92c31cbb2d201d13ce94bb8203c69ab9fb33&scene=21#wechat_redirect
引用自: http://467754239.blog.51cto.com/4878013/1577216
# Configure VCL probe static_chk { #静态网页的健康状态检查 .url = "/test.html"; .interval = 2s; .timeout = 2s; .expected_response = 200; } probe dynamic_chk { #动态网页的健康状态检查 .url = "/test.php"; .interval = 2s; .timeout = 2s; .expected_response = 200; } backend apache01 { #静态请求负载均衡1 .host = "192.168.0.108"; .port = "80"; .probe = static_chk; } backend apache02 { #静态请求负载均衡2 .host = "192.168.0.110"; .port = "80"; .probe = static_chk; } backend nginx01 { #动态请求分发地址 .host = "192.168.0.100"; .port = "80"; .probe = dynamic_chk; } director myload random { #分发器和调度算法 .retries = 2; { .backend = apache01; .weight = 1; } { .backend = apache02; .weight = 1; } } sub vcl_recv { set req.http.X-Forward-For = client.ip; #记录客户端的ip地址 if (req.url ~ ".(html)$" ) { return(lookup); } if (req.url ~ ".(php)$") { #如果请求的是php文件则将请求发送给nginx代理的服务器 set req.backend = nginx01; }else{ set req.backend = myload; #如果请求的是html文件则将请求发送给myload代理的服务器 } } sub vcl_fetch { if (req.request == "GET" && req.url ~ ".(html|jpg|jpeg)$") { set beresp.ttl = 3600s; } } sub vcl_deliver { #记录命中和未命中的变量设定 if (obj.hits > 0) { set resp.http.X-Cache = "HIT from" + " " + server.ip; } else { set resp.http.X-Cache = "MISS"; } return(deliver); }
引用自: http://www.cnblogs.com/xiaocen/p/3715266.html
# # This is an example VCL file for Varnish. # # It does not do anything by default, delegating control to the # builtin VCL. The builtin VCL is called when there is no explicit # return statement. # # See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ # and http://varnish-cache.org/trac/wiki/VCLExamples for more examples. # Marker to tell the VCL compiler that this VCL has been adapted to the # new 4.0 format. vcl 4.0; import directors; probe backend_healthcheck { # 创建健康监测 .url = /health.html; .window = 5; .threshold = 2; .interval = 3s; } backend web1 { # 创建后端主机 .host = "static1.lnmmp.com"; .port = "80"; .probe = backend_healthcheck; } backend web2 { .host = "static2.lnmmp.com"; .port = "80"; .probe = backend_healthcheck; } backend img1 { .host = "img1.lnmmp.com"; .port = "80"; .probe = backend_healthcheck; } backend img2 { .host = "img2.lnmmp.com"; .port = "80"; .probe = backend_healthcheck; } vcl_init { # 创建后端主机组,即directors new web_cluster = directors.random(); web_cluster.add_backend(web1); web_cluster.add_backend(web2); new img_cluster = directors.random(); img_cluster.add_backend(img1); img_cluster.add_backend(img2); } acl purgers { # 定义可访问来源IP "127.0.0.1"; "192.168.0.0"/24; } sub vcl_recv { if (req.request == "GET" && req.http.cookie) { # 带cookie首部的GET请求也缓存 return(hash); } if (req.url ~ "test.html") { # test.html文件禁止缓存 return(pass); } if (req.request == "PURGE") { # PURGE请求的处理 if (!client.ip ~ purgers) { return(synth(405,"Method not allowed")); } return(hash); } 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; } if (req.http.host ~ "(?i)^(www.)?lnmmp.com$") { # 根据不同的访问域名,分发至不同的后端主机组 set req.http.host = "www.lnmmp.com"; set req.backend_hint = web_cluster.backend(); } elsif (req.http.host ~ "(?i)^images.lnmmp.com$") { set req.backend_hint = img_cluster.backend(); } return(hash); } sub vcl_hit { # PURGE请求的处理 if (req.request == "PURGE") { purge; return(synth(200,"Purged")); } } sub vcl_miss { # PURGE请求的处理 if (req.request == "PURGE") { purge; return(synth(404,"Not in cache")); } } sub vcl_pass { # PURGE请求的处理 if (req.request == "PURGE") { return(synth(502,"PURGE on a passed object")); } } sub vcl_backend_response { # 自定义缓存文件的缓存时长,即TTL值 if (req.url ~ ".(jpg|jpeg|gif|png)$") { set beresp.ttl = 7200s; } if (req.url ~ ".(html|css|js)$") { set beresp.ttl = 1200s; } if (beresp.http.Set-Cookie) { # 定义带Set-Cookie首部的后端响应不缓存,直接返回给客户端 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"; } }
http://xiaodong88.blog.51cto.com/1492564/1304988/ varnish
https://www.varnish-cache.org/docs/4.0/whats-new/upgrading.html#directors-have-been-moved-to-the-vmod-directors 4.0改动
4.0改动后新的负载均衡配置:
vcl 4.0; import directors; backend apache1 { .host = "10.188.12.201"; .port = "80"; } backend apache2 { .host = "10.188.12.203"; .port = "80"; } sub vcl_init { new cluster1 = directors.round_robin(); cluster1.add_backend(apache1); cluster1.add_backend(apache2); } sub vcl_recv { set req.backend_hint = cluster1.backend(); }