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=", "; wpjunk=");
  84.     }
  85.     ### don’t cache authenticated sessions
  86.     if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID|comment_author_)") {
  87.         return(pass);
  88.     }
  89.  
  90.     ### parse accept encoding rulesets to make it look nice
  91.     if (req.http.Accept-Encoding) {
  92.         if (req.http.Accept-Encoding ~ "gzip") {
  93.         set req.http.Accept-Encoding = "gzip";
  94.         } elsif (req.http.Accept-Encoding ~ "deflate") {
  95.         set req.http.Accept-Encoding = "deflate";
  96.         } else {
  97.         # unkown algorithm
  98.         remove req.http.Accept-Encoding;
  99.         }
  100.     }
  101. if (req.backend.healthy) {
  102.                 set req.grace = 120s; /* Only enable if you don’t mind slightly stale content */
  103.         } else {
  104.                 set req.grace = 24h;
  105.         }
  106.  
  107.     return(lookup);
  108. }
  109. sub vcl_fetch {
  110. set beresp.grace = 24h; /* Keep at longest used in vcl_recv */
  111. set beresp.ttl = 1h;
  112. }

wordpress varnish vcl配置文件

</p

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-06-07 08:34:52
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[加我微信]
chaoneo
注:点击复制微信号并打开微信APP,添加好友后进行聊天。
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: