首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Varnish:缓存选项/CORS请求

Varnish:缓存选项/CORS请求
EN

Stack Overflow用户
提问于 2017-05-16 18:08:21
回答 1查看 2.8K关注 0票数 1

documentation中所述,根据Host报头或IP和请求的URL计算哈希键:

代码语言:javascript
复制
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的正确缓存配置应该是什么样子的呢?显然,它的URLHostIP与相应的HTTP GET请求是相同的。

代码语言:javascript
复制
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报头的响应。

EN

回答 1

Stack Overflow用户

发布于 2017-05-16 18:49:14

尝试以下操作。

为了确保OPTIONS请求方法可以被缓存,你需要从你的vcl_recv过程中调用return语句,这样built-in VCLvcl_recv就不会再运行了。

代码语言:javascript
复制
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值而有所不同,您可以将如下内容放入:

代码语言:javascript
复制
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.
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43998474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档