Varnish 清除不起作用。系统日志中有一些错误

Varnish 清除不起作用。系统日志中有一些错误

在我的服务器中,我设置了 varnish。我使用 wordpress 并安装了 w3 total cache 插件。我已设置该插件以清除我的 varnish(varnish 3)缓存。但它不起作用!在我的系统日志中,有一些错误,如下所示...请帮助我!

    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Rd GET / HTTP/1.1
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Wr 101 Unknown request. Type 'help' for more info. all comm$
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Rd Host: 77.81.240.177:6082
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Wr 101 Unknown request. Type 'help' for more info. all comm$
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Rd User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.$
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Wr 101 Unknown request. Type 'help' for more info. all comm$
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Rd Accept: text/html,application/xhtml+xml,application/xml;$
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Wr 101 Unknown request. Type 'help' for more info. all comm$
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Rd Accept-Language: en-us,en;q=0.5
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Wr 101 Unknown request. Type 'help' for more info. all comm$
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Rd Accept-Encoding: gzip, deflate
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Wr 101 Unknown request. Type 'help' for more info. all comm$
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Rd Connection: keep-alive
    Mar 22 21:29:40 kadupul varnishd[3755]: CLI telnet 113.59.222.126 58833 77.81.240.177 6082 Wr 101 Unknown request. Type 'help' for more info. all comm$
    Mar 22 21:30:01 kadupul CRON[3782]: (root) CMD (env MAILTO= SHELL=/bin/bash /usr/local/sbin/bfd -q)

我的 default.vcl (我还在 w3 total cache 插件中添加了 ip 77.81.240.177 作为 varnish 服务器) :

backend default {
    .host = "77.81.240.177";
    .port = "8080";
}

acl purge {
    "localhost";
    "127.0.0.1";
    "77.81.240.177";
}

sub vcl_recv {

    # Add a unique header containing the client address
    remove req.http.X-Forwarded-For;
    #set    req.http.X-Forwarded-For = client.ip;
    set    req.http.X-Forwarded-For = req.http.rlnclientipaddr; 

   if (req.request == "PURGE") {
     if (!client.ip ~ purge) {
       error 405 "Not allowed.";
     }
     return(lookup);
   }
  if (req.http.Accept-Encoding) {
#revisit this list
    if (req.url ~ "\.(gif|jpg|jpeg|swf|flv|mp3|mp4|pdf|ico|png|gz|tgz|bz2)(\?.*|)$") {
      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;
    }
  }
  if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    unset req.http.cookie;
    set req.url = regsub(req.url, "\?.*$", "");
  }
  #if (req.url ~ "(\?|&)(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=”) {
  #  set req.url = regsub(req.url, “\?.*$”, “”);
  #}
  if (req.http.cookie) {
    if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
      return(pass);
    } else {
      unset req.http.cookie;
    }
  }
}

sub vcl_fetch {
  if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
    return (hit_for_pass);
  }
  if ( (!(req.url ~ "(wp-(login|admin)|login)")) || (req.request == "GET") ) {
    unset beresp.http.set-cookie;
   set beresp.ttl = 2h;
  }
  if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set beresp.ttl = 30d;
  } #else {
   # set beresp.do_esi = true;
  #}
}

sub vcl_deliver {
# multi-server webfarm? set a variable here so you can check
# the headers to see which frontend served the request
#   set resp.http.X-Server = "server-01";
   if (obj.hits > 0) {
     set resp.http.X-Cache = "HIT";
   } else {
     set resp.http.X-Cache = "MISS";
   }
}
sub vcl_hit {
  if (req.request == "PURGE") {
    set obj.ttl = 0s;
    error 200 "OK";
  }
}

sub vcl_miss {
  if (req.request == "PURGE") {
    error 404 "Not cached";
  }
}

答案1

假设 Varnish3

if (req.request == "PURGE") {
 if (!client.ip ~ purge) {
   error 405 "Not allowed.";
 }
 return(lookup);
}

这个块是不正确的,当不允许清除的客户端尝试这样做时,它将触发 405 错误,但是如果有效的客户端尝试这样做,其中没有任何内容可以真正清除,而是将从缓存中返回内容。

        if (req.request == "PURGE") {
                if (!client.ip ~ purge) {
                        error 405 "Not allowed.";
                }
                ban("req.url ~ "+req.url);
                error 200 "Purged";
        }

这个块应该可以完成您需要它做的事情。

相关内容