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、后端服务器状态检查,如发生故障,继续以旧缓存内容服务。
下面是配置文件内容:
- backend wp1
- {
- .host = "127.0.0.1";
- .port = "8000";
- .probe = {
- .url = "/";
- .timeout = 1s;
- .interval = 60s;
- .window = 1;
- .threshold = 1;
- }
- }
- acl purge {
- "127.0.0.1";
- "www.centos.bz";
- }
- sub vcl_recv {
- if (req.request == "PURGE") {
- if (!client.ip ~ purge) {
- error 405 "Not allowed.";
- }
- return (lookup);
- }
- }
- # Do the PURGE thing
- sub vcl_hit {
- if (req.request == "PURGE") {
- purge;
- error 200 "Purged.";
- }
- }
- sub vcl_miss {
- if (req.request == "PURGE") {
- purge;
- error 200 "Purged.";
- }
- }
- sub vcl_recv {
- ## always cache these images & static assets
- if (req.request == "GET" && req.url ~ ".(css|js|gif|jpg|jpeg|bmp|png|ico|img|tga|wmf|html|htm)$") {
- remove req.http.cookie;
- return(lookup);
- }
- if (req.request == "GET" && req.url ~ "(xmlrpc.php|wlmanifest.xml)") {
- remove req.http.cookie;
- return(lookup);
- }
- ## get real ip address
- if (req.http.x-forwarded-for) {
- set req.http.X-Forwarded-For =
- req.http.X-Forwarded-For + ", "+ client.ip;
- } else {
- set req.http.X-Forwarded-For = client.ip;
- }
- ##never cache POST requests
- if (req.request == "POST")
- {
- set req.backend = wp1;
- return(pass);
- }
- ### do not cache these files:
- ##never cache the admin pages, or the server-status page
- if (req.request == "GET" && (req.url ~ "(wp-admin|wp-login|server-status)"))
- {
- return(pipe);
- }
- #DO cache this ajax request
- if(req.http.X-Requested-With == "XMLHttpRequest" && req.url ~ "recent_reviews")
- {
- return (lookup);
- }
- #dont cache ajax requests
- 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)")
- {
- return (pass);
- }
- if (req.http.Cookie && req.http.Cookie ~ "wordpress_") {
- set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=", "; wpjunk=");
- }
- ### don’t cache authenticated sessions
- if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID|comment_author_)") {
- return(pass);
- }
- ### parse accept encoding rulesets to make it look nice
- if (req.http.Accept-Encoding) {
- if (req.http.Accept-Encoding ~ "gzip") {
- set req.http.Accept-Encoding = "gzip";
- } elsif (req.http.Accept-Encoding ~ "deflate") {
- set req.http.Accept-Encoding = "deflate";
- } else {
- # unkown algorithm
- remove req.http.Accept-Encoding;
- }
- }
- if (req.backend.healthy) {
- set req.grace = 120s; /* Only enable if you don’t mind slightly stale content */
- } else {
- set req.grace = 24h;
- }
- return(lookup);
- }
- sub vcl_fetch {
- set beresp.grace = 24h; /* Keep at longest used in vcl_recv */
- set beresp.ttl = 1h;
- }
wordpress varnish vcl配置文件
</p