清漆5使用卷曲清洗

时间:2017-12-13 10:58:56

标签: curl varnish-vcl purge

我正在尝试使用curl命令从varnish内存中清除单个文件:

curl -X PURGE http://cdn.b1.com/path/path/XXXX.png

我确实获得了200状态代码

HTTP/1.1 200 Purged
Date: Wed, 13 Dec 2017 00:25:29 GMT
Server: Varnish
X-Varnish: 32790
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 240
Accept-Ranges: bytes
Connection: keep-alive

但是当我尝试通过浏览器或curl GET请求获取文件时,我得到了一个带有HIT响应的旧文件,并且命中次数增加了

在清除方法检查后,在 vcl_recv 部分中使用ban("req.url ~ "+req.url);此命令似乎有效,我需要立即解决PURGE

这是我的自定义vcl:

#vcl version
vcl 4.0;

#b1 aws s3 origin
backend b1 {
.host = "b1.s3.amazonaws.com";
.port = "80";
}

#b1-uploads aws s3 origin
backend b1-uploads {
.host = "b1-uploads.s3.amazonaws.com";
.port = "80";
}

#b1-front aws s3 origin
backend b1-front {
.host = "b1-front.s3.amazonaws.com";
.port = "80";
}

#b2 aws s3 origin
backend b2 {
.host = "b2.s3.amazonaws.com";
.port = "80";
}

#b2-uploads aws s3 origin
backend b2-uploads {
.host = "b2-uploads.s3.amazonaws.com";
.port = "80";
}

#b2-front aws s3 origin
backend b2-front {
.host = "b2-front.s3.amazonaws.com";
.port = "80";
}

#Add purge rules
acl purge {
"localhost";
"127.0.0.1";
"devapi.b1.com";
"api.b1.com";
"devapi.b2.com";
"api.b2.com";
"cdn2.b2.com";
}


#Actions upon request recv
sub vcl_recv {

#Check if specific encoding requestes
if (req.http.Accept-Encoding) {
  if(req.http.Accept-Encoding ~ "gzip"){
        set req.http.Accept-Encoding = "gzip";
  } elsif (req.http.Accept-Encoding ~ "deflate" &&
        req.http.user-agent !~ "MSIE") {
        set req.http.Accept-Encoding = "deflate";
  } else {
        # Unkown algorithm
unset req.http.Accept-Encoding;
  }

}


#Check purger IP
if (req.method == "PURGE") {
 if (!client.ip ~ purge) {
 return(synth(405, "This IP is not allowed to send PURGE requests."));
   }
#Force cache miss
 set req.hash_always_miss = true;
#ban("req.url ~ "+req.url); //The ban command is working
return (purge);
 }

#check ban method
if(req.method == "BAN"){
     if (!client.ip ~ purge) {
        return(synth(405, "This IP is not allowed to send PURGE requests."));
   }
ban("req.url ~ "+req.url);
}

#Change X-Forwarded header
if (req.restarts == 0) {
 if (req.http.X-Forwarded-For) {
 set req.http.X-Forwarded-For = client.ip;
   }
 }

#Route backends
if (req.http.host == "cdn.b1.com") {
    set req.backend_hint = b1;
    set req.http.host = "b1.s3.amazonaws.com";
  }
if (req.http.host == "uploads.b1.com") {
    set req.backend_hint = b1-uploads;
    set req.http.host = "b1-uploads.s3.amazonaws.com";
  }
if (req.http.host == "front.b1.com") {
    set req.backend_hint = b1-front;
    set req.http.host = "b1-front.s3.amazonaws.com";
  }

if (req.http.host == "cdn.b2.com") {
    set req.backend_hint = b2;
    set req.http.host = "b2.s3.amazonaws.com";
  }
if (req.http.host == "cdn2.b2.com") {
    set req.backend_hint = b2;
    set req.http.host = "b2.s3.amazonaws.com";
  }

  if (req.http.host == "uploads.b2.com") {
    set req.backend_hint = b2-uploads;
    set req.http.host = "b2-uploads.s3.amazonaws.com";
  }
if (req.http.host == "front.b2.com") {
    set req.backend_hint = b2-front;
    set req.http.host = "b2-front.s3.amazonaws.com";
  }


#Unset all cookies
unset req.http.Cookie;

#Unset Accept-Lang
unset req.http.Accept-Language;

#Unset User-Agent
unset req.http.User-Agent;

#Unset Cache-Control
unset req.http.Cache-Control;

#Unset Authorization
unset req.http.Authorization;

#Unset vary parameter
unset req.http.vary;

#Unset referer
unset req.http.Referer;

}

#Action upon purge
#  sub vcl_purge {
      #set req.method = "GET";
#       ban("req.http.host == " + req.http.host +
 #           " && req.url == " + req.url);
#       set req.http.X-Purger = "Purged";

 #     return(restart);
 #}

#Handle communication with the backend
sub vcl_backend_response {
 set beresp.ttl = 24h;
 set beresp.grace = 1h;

#Enable gzip compression for origin responses
set beresp.do_gzip = true;
#set beresp.http.X-Cache = "ZIP";

}


#Handle delivred responses
sub vcl_deliver {
if (req.http.X-Purger) {
set resp.http.X-Purger = req.http.X-Purger;
  }

if (obj.hits > 0) { # Add debug header to see if it's a HIT/MISS and the number of hits, disable when not needed
    set resp.http.X-Arkia-Cache = "HIT";
  } else {
    set resp.http.X-Arkia-Cache = "MISS";
  }

  # obj.hits behaviour changed in 4.0, now it counts per objecthead, not per object
  # and obj.hits may not be reset in some cases where bans are in use. See bug 1492 for details.
  # So take hits with a grain of salt
  set resp.http.X-Arkia-Cache-Hits = obj.hits;

#Set remaining ttl as a client cache
set resp.http.Cache-Control =  "max-age="+obj.ttl;

#Add Corss Origin
set resp.http.Access-Control-Allow-Origin = "*";

# Remove some headers: Apache version & OS
  unset resp.http.Server;
  unset resp.http.X-Drupal-Cache;
  unset resp.http.X-Varnish;
  unset resp.http.Via;
  unset resp.http.Link;
  unset resp.http.X-Generator;
  unset resp.http.age;

}

我是否错过了重要的事情?

0 个答案:

没有答案