gpt4 book ai didi

caching - Varnish :缓存 OPTIONS/CORS 请求

转载 作者:行者123 更新时间:2023-12-03 17:47:09 24 4
gpt4 key购买 nike

documentation 所述,哈希键是根据请求的 Host header 或 IPURL 计算的:

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 请求具有相同的 HostIPHTTP GET
curl -H "Origin: https://www.example.com" -I \
-H "Access-Control-Request-Method: GET" \
-X OPTIONS --verbose \
https://backend.server.example/rest/endpoint

同样优选的是缓存关于 Origin header 的响应,该 header 也是 CORS 请求的一部分。

最佳答案

请尝试以下方法。

为了确保可以完全缓存OPTIONS请求方法,您将需要从return过程中调用vcl_recv语句,以便完全不运行built-in VCLvcl_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;
}

为了使缓 stub 据Origin头值的不同而不同,您将放置以下内容:

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.
}

关于caching - Varnish :缓存 OPTIONS/CORS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43998474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com