W3TC Varnish反向代理配置文件
昨天在装varnish cache,参考着各路教程一路都挺顺利,就是最后运行的时候开始报错了
Job for varnish.service failed because the control process exited with error code. See "systemctl status varnish.service" and "journalctl -xe" for details.
继续发现是VCL配置文件的问题,用默认的default.vcl
就能正常启动。但是默认的配置基本不支持W3TC的purge,总不能每次都到后台手动清理吧,于是把之前报错的W3TC的varnish-sample-config.vcl
还原,看具体哪里报错。
又经过了一番搜索找到了这篇Upgrading to Varnish 4,发现问题的原因是W3TC的配置文件是给varnish 3.0用的,而我装的4.0并不兼容旧版的VCL,所以对照着报错内容和升级指导把W3TC的sample config修改了一下。
#添加vcl版本申明 vcl 4.0; backend default { .host = "127.0.0.1"; .port = "8080"; } acl purge { # Web server with plugin which will issue PURGE requests "localhost"; } sub vcl_recv { #这几句语法有变,同时最后的vcl_hit和vcl_miss部分也不再需要了 # if (req.request == "PURGE") { # if (!client.ip ~ purge) { # error 405 "Not allowed."; # } if (req.method == "PURGE") { if (!client.ip ~ purge) { return(synth(405,"Not allowed.")); } ban("req.url ~ ^" + req.url + "$ && req.http.host == " + req.http.host); } #新版默认启用压缩,所以这部分不需要了 # Normalize content-encoding # if (req.http.Accept-Encoding) { # if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|lzma|tbz)(\?.*|)$") { # remove req.http.Accept-Encoding; # } elsif (req.http.Accept-Encoding ~ "gzip") { # set req.http.Accept-Encoding = "gzip"; # } elsif (req.http.Accept-Encoding ~ "deflate") { # set req.http.Accept-Encoding = "deflate"; # } else { # remove req.http.Accept-Encoding; # } # } # Remove cookies and query string for real static files if (req.url ~ "\.(bz2|css|flv|gif|gz|ico|jpeg|jpg|js|lzma|mp3|mp4|pdf|png|swf|tbz|tgz|txt|zip)(\?.*|)$") { unset req.http.cookie; set req.url = regsub(req.url, "\?.*$", ""); } if (req.url ~ "wp-(login|admin|comments-post.php|cron.php)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") { return (pass); } #语法更改 return (lookup); return (hash); } #这部分语法有变 #sub vcl_fetch { # Don't cache backend # if (req.url ~ "wp-(login|admin|comments-post.php|cron.php)" || # req.url ~ "preview=true" || # req.url ~ "xmlrpc.php") { # Dont modify anything, it's (pass) object # } else { # unset beresp.http.set-cookie; # if (beresp.status == 307) { # Don't cache temporary redirects like ?repeat=w3tc # set beresp.ttl = 0h; # } else if (req.url ~ "\.(bz2|css|flv|gif|gz|ico|jpeg|jpg|js|lzma|mp3|mp4|pdf|png|swf|tbz|tgz|txt|zip)$") { # set beresp.ttl = 30d; # } else { # set beresp.ttl = 4h; # } # } #} sub vcl_backend_response { # Don't cache backend if (bereq.url ~ "wp-(login|admin|comments-post.php|cron.php)" || bereq.url ~ "preview=true" || bereq.url ~ "xmlrpc.php") { # Dont modify anything, it's (pass) object } else { unset beresp.http.set-cookie; if (beresp.status == 307) { # Don't cache temporary redirects like ?repeat=w3tc set beresp.ttl = 0h; } else if (bereq.url ~ "\.(bz2|css|flv|gif|gz|ico|jpeg|jpg|js|lzma|mp3|mp4|pdf|png|swf|tbz|tgz|txt|zip)$") { set beresp.ttl = 30d; } else { set beresp.ttl = 4h; } } } #sub vcl_hit { # if (req.request == "PURGE") { # purge; # error 200 "Purged."; # } #} #sub vcl_miss { # if (req.request == "PURGE") { # purge; # error 200 "Purged."; # } #}
没有注释的干净版本
vcl 4.0; backend default { .host = "127.0.0.1"; .port = "8080"; } acl purge { # Web server with plugin which will issue PURGE requests "localhost"; } sub vcl_recv { if (req.method == "PURGE") { if (!client.ip ~ purge) { return(synth(405,"Not allowed.")); } ban("req.url ~ ^" + req.url + "$ && req.http.host == " + req.http.host); } # Remove cookies and query string for real static files if (req.url ~ "\.(bz2|css|flv|gif|gz|ico|jpeg|jpg|js|lzma|mp3|mp4|pdf|png|swf|tbz|tgz|txt|zip)(\?.*|)$") { unset req.http.cookie; set req.url = regsub(req.url, "\?.*$", ""); } if (req.url ~ "wp-(login|admin|comments-post.php|cron.php)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") { return (pass); } return (hash); } sub vcl_backend_response { # Don't cache backend if (bereq.url ~ "wp-(login|admin|comments-post.php|cron.php)" || bereq.url ~ "preview=true" || bereq.url ~ "xmlrpc.php") { # Dont modify anything, it's (pass) object } else { unset beresp.http.set-cookie; if (beresp.status == 307) { # Don't cache temporary redirects like ?repeat=w3tc set beresp.ttl = 0h; } else if (bereq.url ~ "\.(bz2|css|flv|gif|gz|ico|jpeg|jpg|js|lzma|mp3|mp4|pdf|png|swf|tbz|tgz|txt|zip)$") { set beresp.ttl = 30d; } else { set beresp.ttl = 4h; } } }