问题描述
我在切片服务器前使用 varnish 来缓存 mapBox 切片。为了移除旧图块,我打算使用禁令来有效地移除大量缓存的图块。我的问题是 varnish 仍然使用缓存的对象(至少响应中的 age
表明了这一点)并且不联系后端。
我首先请求 http://varnish/5/3/4.pbf,然后添加带有 curl -X BAN -H 'X-Purge-Regex: 5/3/4.pbf' varnish
或 varnishadm
的禁令,然后是 ban obj.http.url ~ 5/3/4.pbf
,然后请求 http ://varnish/5/3/4.pbf 再次。
一开始我的禁止列表是空的:
Present bans:
1610117471.434488 1 C
使用 curl -X BAN -H 'X-Purge-Regex: 5/3/4.pbf' varnish
<!DOCTYPE html>
<html>
<head>
<title>200 Ban added</title>
</head>
<body>
<h1>Error 200 Ban added</h1>
<p>Ban added</p>
<h3>Guru Meditation:</h3>
<p>XID: 8</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
并出现在禁止列表中
Present bans:
1610117369.028870 0 - obj.http.url ~ 5/3/4.pbf
1610117307.220739 1 C
再次请求http://varnish/5/3/4.pbf后,ban列表显示使用了ban
Present bans:
1610117471.434488 1 - obj.http.url ~ 5/3/4.pbf
但是响应的年龄不是 0,因为它仍然是第一个请求的对象。
一段时间后,禁令被解除:
Present bans:
1610117471.434488 1 C
我的 vcl_recv
看起来像这样,但错误可能在其他地方,因为它也不适用于 varnishadm
:
sub vcl_recv {
unset req.http.cookie;
# Allowing PURGE from localhost
if (req.method == "BAN"||req.method == "PURGE") {
if (!client.ip ~ purge) {
return(synth(405,"Not allowed."));
}
if (req.method == "BAN") {
ban("obj.http.url ~ " + req.http.X-Purge-Regex);
# Throw a synthetic page so the
# request won't go to the backend.
return(synth(200,"Ban added"));
}
if (req.method == "PURGE") {
return (purge);
}
}
}
我也尝试使用 https://stackoverflow.com/a/61507014 中的 vcl_purge
但这似乎对禁止 (?) 没有帮助。
我使用 X-Purge-Regex
标头是为了不用担心必须像 https://stackoverflow.com/a/38526921 中那样转义特殊字符,但只是像 obj.http.url ~ 0
这样的禁令是行不通的。
我使用的是 varnish 6.5 和 vcl 4.0。
禁止请求
* << Request >> 54
- Begin req 53 rxreq
- Timestamp Start: 1610121483.345437 0.000000 0.000000
- Timestamp Req: 1610121483.345437 0.000000 0.000000
- VCL_use boot
- ReqStart 192.168.48.2 50882 http
- ReqMethod BAN
- ReqURL /
- ReqProtocol HTTP/1.1
- ReqHeader Host: varnish-volatile
- ReqHeader User-Agent: curl/7.64.0
- ReqHeader Accept: */*
- ReqHeader X-Purge-Regex: 0
- ReqHeader X-Forwarded-For: 192.168.48.2
- VCL_call RECV
- VCL_acl MATCH purge "importer"
- VCL_return synth
- VCL_call HASH
- VCL_return lookup
- RespProtocol HTTP/1.1
- RespStatus 200
- RespReason Ban added
- RespHeader Date: Fri,08 Jan 2021 15:58:03 GMT
- RespHeader Server: Varnish
- RespHeader X-Varnish: 54
- VCL_call SYNTH
- RespHeader Content-Type: text/html; charset=utf-8
- RespHeader Retry-After: 5
- VCL_return deliver
- Timestamp Process: 1610121483.347281 0.001844 0.001844
- RespHeader Content-Length: 246
- Storage malloc Transient
- Filters
- RespHeader Accept-Ranges: bytes
- RespHeader Connection: keep-alive
- Timestamp Resp: 1610121483.347557 0.002120 0.000276
- ReqAcct 98 0 98 218 246 464
- End
添加禁止后获取
* << Request >> 32806
- Begin req 32805 rxreq
- Timestamp Start: 1610121552.733872 0.000000 0.000000
- Timestamp Req: 1610121552.733872 0.000000 0.000000
- VCL_use boot
- ReqStart 192.168.48.1 55176 http
- ReqMethod GET
- ReqURL /public.sNow_db/0/0/0.pbf
- ReqProtocol HTTP/1.1
- ReqHeader Host: localhost:8090
- ReqHeader User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:84.0) Gecko/20100101 Firefox/84.0
- ReqHeader Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
- ReqHeader Accept-Language: en-US,en;q=0.5
- ReqHeader Accept-Encoding: gzip,deflate
- ReqHeader DNT: 1
- ReqHeader Connection: keep-alive
- ReqHeader Upgrade-Insecure-Requests: 1
- ReqHeader Pragma: no-cache
- ReqHeader Cache-Control: no-cache
- ReqHeader X-Forwarded-For: 192.168.48.1
- VCL_call RECV
- ReqUnset Host: localhost:8090
- ReqHeader host: localhost:8090
- VCL_return hash
- ReqUnset Accept-Encoding: gzip,deflate
- ReqHeader Accept-Encoding: gzip
- VCL_call HASH
- VCL_return lookup
- Hit 28 601789.331504 10.000000 0.000000
- VCL_call HIT
- VCL_return deliver
- RespProtocol HTTP/1.1
- RespStatus 200
- RespReason OK
- RespHeader content-encoding: gzip
- RespHeader content-type: application/x-protobuf
- RespHeader date: Fri,08 Jan 2021 15:09:02 GMT
- RespHeader vary: Accept-Encoding
- RespHeader X-Varnish: 32806 28
- RespHeader Age: 3010
- RespHeader Via: 1.1 varnish (Varnish/6.5)
- VCL_call DELIVER
- VCL_return deliver
- Timestamp Process: 1610121552.734070 0.000197 0.000197
- Filters
- RespHeader Accept-Ranges: bytes
- RespHeader Content-Length: 295
- RespHeader Connection: keep-alive
- Timestamp Resp: 1610121552.734217 0.000345 0.000147
- ReqAcct 414 0 414 272 295 567
- End
重现问题
重现错误:
git clone https://github.com/Baschdl/varnish-ban-setup.git && cd varnish-ban-setup
docker-compose up
- 打开 http://localhost:8092/5/3/1.pbf
docker-compose exec varnish varnishadm ban obj.http.url ~ pbf
- 再次打开http://localhost:8092/5/3/1.pbf,你会得到旧的对象
解决方法
您发布的 obj.http.url ~ 5/3/4.pbf
禁令与 url
响应标头匹配。
记住: URL 是请求标头,而不是响应标头。没有理由恐慌,你所做的完全有道理,并且与所谓的禁止潜伏者的范围有关。
禁止潜伏者
ban lurker 是一个线程,它异步处理禁令列表上的禁令,并将对象与禁令进行匹配,以便从缓存中删除对象的模式。
ban lurker 不在请求范围内操作,而只知道对象范围。
为了成功匹配请求信息,可以添加请求上下文作为响应头。这就是您通过 obj.http.url
那么为什么禁令不起作用?
您的禁令无效的原因是您没有在 VCL 文件中设置 obj.http.url
。结果,禁止潜伏者无法匹配任何对象。
如何解决问题
解决方案很简单:在后端响应上下文中设置缺失的标头,如下图所示:
sub vcl_backend_response {
set beresp.http.url = bereq.url;
set beresp.http.host = bereq.http.host;
}
当后端响应时,就在对象存储在缓存中之前,我们可以设置丢失的标头。
之后,禁止潜伏者将能够将禁止表达式与正确的对象进行匹配,并将它们从缓存中删除。
不要忘记对象不会立即匹配:它们仅在到达 ban_lurker_age
时才会被移除,默认设置为 1 分钟。