我想使用清漆缓存410页

时间:2019-08-06 15:08:58

标签: varnish

我将Varnish设置为网站的缓存服务器,我想使用Varnish缓存410页。 我在backend_response.vcl文件中使用了以下配置,但没有得到缓存

 <body>

  <div>
    <input type="button" name="" value="Hello"></input>
    <input type="button" name="" value="Good Job"></input>
    <input type="button" name="" value="Have a nice day"></input>
  </div>

  <!-- A form for sending/ saving data  -->
  <form class="" method="post">
    <textarea rows="4" cols="50">this is you current text</textarea>
    <input type="submit" name="" value="submit">
  </form>

  <script type="text/javascript">
    const buttons = document.querySelector('div') // Selecting all the buttons
    const form = document.querySelector('form textarea') // Selecting my textarea

    buttons.addEventListener('click', e => {
      // when a button is clicked add the value to the textarea
      form.value += ' ' + e.target.value
      console.log(form.value, e.target.value);
    })
  </script>
</body>

1 个答案:

答案 0 :(得分:0)

默认情况下,清漆缓存410个响应,其中包括see here

您的410响应可能带有一些Set-Cookie /负数Cache-Control,因此内置了VCL kicks in

sub vcl_backend_response {
    if (bereq.uncacheable) {
        return (deliver);
    } else if (beresp.ttl <= 0s ||
      beresp.http.Set-Cookie ||
      beresp.http.Surrogate-control ~ "(?i)no-store" ||
      (!beresp.http.Surrogate-Control &&
        beresp.http.Cache-Control ~ "(?i:no-cache|no-store|private)") ||
      beresp.http.Vary == "*") {
        # Mark as "Hit-For-Miss" for the next 2 minutes
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
    }
    return (deliver);
}

在代码中return (deliver);时,您将绕过内置VCL的执行,但是,仅当TTL为负时才这样做。由于它对您不起作用,因此仅表示您的410响应发送否定的Cache-Control

要在TTL为负的情况下强制执行缓存,并且仅对410执行此操作,您将执行以下操作:

sub vcl_backend_response {
    if (beresp.status == 410) {
        if (beresp.ttl <= 0s) {
            set beresp.ttl = 60s;
        } 
        return (deliver);
    }
}

在此配置中,您将缓存否则无法缓存的410响应长达1分钟;而可缓存的410响应(源自Cache-Control的TTL)将按照该标头中的指示进行缓存。

相关问题