如documentation中所述,根据Host报头或IP和请求的URL计算哈希键:
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}HTTP OPTIONS的正确缓存配置应该是什么样子的呢?显然,它的URL、Host或IP与相应的HTTP GET请求是相同的。
curl -H "Origin: https://www.example.com" -I \
-H "Access-Control-Request-Method: GET" \
-X OPTIONS --verbose \
https://backend.server.example/rest/endpoint同样优选的是高速缓存关于也是CORS请求的一部分的Origin报头的响应。
发布于 2017-05-16 18:49:14
尝试以下操作。
为了确保OPTIONS请求方法可以被缓存,你需要从你的vcl_recv过程中调用return语句,这样built-in VCL的vcl_recv就不会再运行了。
sub vcl_recv {
if (req.method == "PRI") {
/* We do not support SPDY or HTTP/2.0 */
return (synth(405));
}
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.method != "GET" && req.method != "HEAD" && req.method != "OPTIONS") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
if (req.method == "GET" || req.method == "HEAD" || req.method == "OPTIONS") {
set req.http.x-method = req.method;
}
return (hash);
}
sub vcl_backend_fetch {
set bereq.method = bereq.http.x-method;
}为了使缓存根据Origin header值而有所不同,您可以将如下内容放入:
sub vcl_hash {
if (req.http.Origin) {
hash_data(req.http.Origin);
}
# no return here in order to have built-in.vcl do its default behavior of also caching host, URL, etc.
}https://stackoverflow.com/questions/43998474
复制相似问题