VCL配置如下:
#后端web服务器指向 backend WwwBackend { .host = "192.168.206.15"; .port = "80"; } #允许清理缓存IP段、主机 acl purge { "127.0.0.1"; "192.168.134.0"/24; } sub vcl_recv { if (req.request == "PURGE") {#purge请求清空缓存 if (!client.ip ~ purge) { error 405 "Not allowed."; } return (lookup); } if (req.http.user-agent ~ "(?i)iPhone|(?i)iPod|(?i)series60/50|(?i)Android|(?i)symbian|(?i)IEmobile") {#移动终端访问跳转 error 750 "Moved Temporarily"; } else if (req.http.host ~ "^leo.com") {#一级域名301跳转 error 751 "Moved Permanently"; } else if (req.http.host ~ "^www.leo.com") { if(req.url == "/" || req.url ~ "^(?i)/home.mvc|^(?i)/about.mvc|^(?i)/news.mvc|^(?i)/html/f5") {#站点有效页面、目录(可不做要求) set req.backend = WwwBackend; if (req.request != "GET" && req.request != "HEAD") { return (pass); } else { return (lookup); } } else { error 751 "Url Redirect"; } } else { error 404 "Wrong Host"; return (lookup); } #压缩 if (req.http.Accept-Encoding) { if (req.http.Accept-Encoding ~ "gzip") { set req.http.Accept-Encoding = "gzip"; } else if (req.http.Accept-Encoding ~ "deflate") { set req.http.Accept-Encoding = "deflate"; } else { remove req.http.Accept-Encoding; } } } sub vcl_hit { if (req.request == "PURGE") { purge; error 200 "Purged."; } } sub vcl_miss { if (req.request == "PURGE") { purge; error 200 "Purged."; } } sub vcl_fetch { if (req.request == "GET") { unset beresp.http.set-cookie; set beresp.ttl= 900s;#缓存时间 } } sub vcl_error { if (obj.status == 750) { set obj.http.Location = "http://m.leo.com"; set obj.status = 302; return(deliver); } else if (obj.status == 751) { set obj.http.Location = "http://www.leo.com"; set obj.status = 301; return(deliver); } }注:
1、利用(?i)可忽略大小写
2、关于pipe和pass的区别,见http://yeelone.blog.51cto.com/1476571/772369
转载于:https://www.cnblogs.com/LeoZ/archive/2012/06/07/2540176.html
相关资源:JAVA上百实例源码以及开源项目