以下内容是debian系统下的方法,其他系统自己看着办。。。:
apt-get install varnish
以下规则针对wordpress- backend default {
- .host = "127.0.0.1";
- .port = "apache后端端口";
- }
- acl purge{
- "127.0.0.1";
- }
- sub vcl_fetch {
- if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
- unset beresp.http.set-cookie;
- set beresp.ttl = 24h;
- }
-
- if (req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)") {
- set beresp.cacheable = false;
- } else {
- set beresp.cacheable = true;
- }
- if (!beresp.cacheable) {
- set beresp.http.X-Cacheable = "NO:Not Cacheable";
- } else if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)" ) {
- set beresp.http.X-Cacheable = "NO:Got Session";
- return(pass);
- } else if ( beresp.http.Cache-Control ~ "private") {
- set beresp.http.X-Cacheable = "NO:Cache-Control=private";
- return(pass);
- } else if ( beresp.ttl < 1s ) {
- set beresp.ttl = 300s;
- set beresp.grace = 300s;
- set beresp.http.X-Cacheable = "YES:Forced";
- } else {
- set beresp.http.X-Cacheable = "YES";
- }
- if (beresp.status == 404 || beresp.status >= 500) {
- set beresp.ttl = 0s;
- }
-
- return(deliver);
- }
-
- sub vcl_hash {
- if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)" ) {
- set req.hash += req.http.Cookie;
- }
- }
-
- # Deliver
- sub vcl_deliver {
- }
- sub vcl_recv {
- set req.url = regsub(req.url, "\?ver=.*$", "");
- set req.url = regsub(req.url, "\?replytocom=.*$", "");
-
- remove req.http.X-Forwarded-For;
- set req.http.X-Forwarded-For = client.ip;
- set req.grace = 120s;
- if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
- unset req.http.Cookie;
- return(lookup);
- }
- set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
- set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
- if (req.http.Cookie ~ "^\s*$") {
- unset req.http.Cookie;
- }
- if (req.request == "PURGE") {
- if (!client.ip ~ purge) {
- error 405 "Not allowed.";
- }
- purge("req.url ~ " req.url " && req.http.host == " req.http.host);
- error 200 "Purged.";
- }
- if (req.request != "GET" && req.request != "HEAD") {
- return( pass );
- } /* We only deal with GET and HEAD by default */
-
- set req.http.cookie = regsub(req.http.cookie, "1231111111111111122222222333333=[^;]+(; )?", "");
- if (req.request == "GET" && (req.url ~ "(wp-admin)")) {
- return(pipe);
- }
- if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID)") {
- return(pass);
- }
- if(req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~ "(wp-comments-post.php|wp-login.php)") {
- return (pass);
- }
- return( lookup );
- }
复制代码 这是varnish的配置,等我吃点东西再发wordpress代码改造。。 |