wordpress varnish vcl配置文件

如需使用varnish作wordpress缓存,已经开发有一键包https://www.centos.bz/lampv/
使用如下配置文件,建议安装wordpress插件Varnish HTTP Purge来自动清除缓存,此配置文件实现的功能如下:
1、varnish作为前端,使用80端口
2、允许127.0.0.1和www.centos.bz清除缓存
3、在缓存之前,删除常见静态文件的cookie。
4、http.x-forwarded-for获取真实IP。
5、不缓存wordpress后台页面,不缓存已登录的用户和保留评论者cookie。
6、后端服务器状态检查,如发生故障,继续以旧缓存内容服务。
下面是配置文件内容:

  1. backend wp1
  2. {
  3. .host = "127.0.0.1";
  4. .port = "8000";
  5. .probe = {
  6.                 .url = "/";
  7.                 .timeout = 1s;
  8.                 .interval = 60s;
  9.                 .window = 1;
  10.                 .threshold = 1;
  11.         }
  12. }
  13. acl purge {
  14. "127.0.0.1";
  15. "www.centos.bz";
  16. }
  17. sub vcl_recv {
  18.           if (req.request == "PURGE") {
  19.                 if (!client.ip ~ purge) {
  20.                         error 405 "Not allowed.";
  21.                 }
  22.                 return (lookup);
  23.         }
  24. }
  25. # Do the PURGE thing
  26. sub vcl_hit {
  27.         if (req.request == "PURGE") {
  28.                 purge;
  29.                 error 200 "Purged.";
  30.         }
  31. }
  32.  
  33. sub vcl_miss {
  34.         if (req.request == "PURGE") {
  35.                 purge;
  36.                 error 200 "Purged.";
  37.         }
  38. }
  39. sub vcl_recv {
  40.     ## always cache these images & static assets
  41.     if (req.request == "GET" && req.url ~ "\.(css|js|gif|jpg|jpeg|bmp|png|ico|img|tga|wmf|html|htm)$") {
  42.       remove req.http.cookie;
  43.       return(lookup);
  44.     }
  45.     if (req.request == "GET" && req.url ~ "(xmlrpc.php|wlmanifest.xml)") {
  46.       remove req.http.cookie;
  47.       return(lookup);
  48.     }
  49. ## get real ip address
  50. if (req.http.x-forwarded-for) {
  51.  set req.http.X-Forwarded-For =
  52.  req.http.X-Forwarded-For + ", "+ client.ip;
  53.  } else {
  54.  set req.http.X-Forwarded-For = client.ip;
  55.  }
  56.     ##never cache POST requests
  57.     if (req.request == "POST")
  58.     {
  59.       set req.backend = wp1;
  60.       return(pass);
  61.     }
  62.  
  63.     ### do not cache these files:
  64.     ##never cache the admin pages, or the server-status page
  65.     if (req.request == "GET" && (req.url ~ "(wp-admin|wp-login|server-status)"))
  66.     {
  67.       return(pipe);
  68.     }
  69.  
  70.     #DO cache this ajax request
  71.     if(req.http.X-Requested-With == "XMLHttpRequest" && req.url ~ "recent_reviews")
  72.     {
  73.       return (lookup);
  74.     }
  75.  
  76.     #dont cache ajax requests
  77.     if(req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~ "(control.php|wp-comments-post.php|wp-login.php|bb-login.php|bb-reset-password.php|register.php)")
  78.     {
  79.         return (pass);
  80.     }
  81.  
  82.     if (req.http.Cookie && req.http.Cookie ~ "wordpress_") {
  83.         set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=", ";t cache authenticated sessions
  84.     if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID|comment_author_)") {
  85.         return(pass);
  86.     }
  87.  
  88.     ### parse accept encoding rulesets to make it look nice
  89.     if (req.http.Accept-Encoding) {
  90.         if (req.http.Accept-Encoding ~ "gzip") {
  91.         set req.http.Accept-Encoding = "gzip";
  92.         } elsif (req.http.Accept-Encoding ~ "deflate") {
  93.         set req.http.Accept-Encoding = "deflate";
  94.         } else {
  95.         # unkown algorithm
  96.         remove req.http.Accept-Encoding;
  97.         }
  98.     }
  99. if (req.backend.healthy) {
  100.                 set req.grace = 120s; /* Only enable if you don't mind slightly stale content */
  101.         } else {
  102.                 set req.grace = 24h;
  103.         }
  104.  
  105.     return(lookup);
  106. }
  107. sub vcl_fetch {
  108. set beresp.grace = 24h; /* Keep at longest used in vcl_recv */
  109. set beresp.ttl = 1h;
  110. }
标签:WordPress 发布于:2019-10-14 15:00:20