13

I can't figure out, for the life of me, how to get varnish to ignore caching of 500 internal server errors. Basically, if someone hits varnish and is returned a 500 internal server error, I want varnish to not cache that page (set a 0s ttl/grace period?). I'm using varnish 3.0.3 and here's my VCL. By default, I want to cache the pages for 30 days.

sub vcl_fetch {
    # Set 30-day TTL
    set beresp.ttl = 2592000 s;
    set beresp.grace = 15d; /* The max amount of time to keep object in cache */

    if (beresp.status == 301 || beresp.status == 302) {
            return (hit_for_pass);
    }

    # Serve pages from the cache should we get a sudden error and re-check in one minute
    if (beresp.status >= 500) {
      set beresp.grace = 1s;
      set beresp.ttl = 1s;
      return (hit_for_pass);
    }

    # Unset the "etag" header (suggested)
    unset beresp.http.etag;

    return(deliver);
}

So, in english: if a 500 internal server is returned... the X-CACHE should show a MISS. When I refresh the page, if it is still 500 internal server, then it should again show a MISS. If the page is successfully delivered, it should show a HIT.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Herman
  • 242
  • 1
  • 2
  • 10

1 Answers1

17

By default Varnish will only cache the following status codes[1]:

  • 200: OK
  • 203: Non-Authoritative Information
  • 300: Multiple Choices
  • 301: Moved Permanently
  • 302: Moved Temporarily
  • 307: Temporary Redirect
  • 410: Gone
  • 404: Not Found

Note that the first time the page is successfully delivered you will still get a MISS

[1] http://book.varnish-software.com/3.0/VCL_Basics.html#the-initial-value-of-beresp-ttl

Fabio Montefuscolo
  • 2,258
  • 2
  • 21
  • 18
NITEMAN
  • 1,236
  • 10
  • 11
  • 1
    Hm ... I do not understand why Varnish would cache the 404 error. We have been burned by this - the resource is back up but the user does not see it. – Leonid Aug 03 '16 at 18:58
  • 4
    @Leonid, The 404 is generally cached by reverse proxies as it does not indicate an upstream server malfunctioning: rather the request was received and handled correctly, but the requested resource does not exist. – mickeybob Nov 28 '16 at 00:34
  • @NITEMAN This is not true at least for Varnish 4 on. See: https://github.com/mattiasgeniar/varnish-4.0-configuration-templates/issues/24 – Mohsen Jul 06 '20 at 14:40
  • @Mohsen Note that I was refering to Varnish default behaviour (overrideable), name it [builtin.vcl](https://github.com/varnishcache/varnish-cache/blob/master/bin/varnishd/builtin.vcl), and it's still true today: https://github.com/varnishcache/varnish-cache/blob/master/doc/sphinx/users-guide/vcl-built-in-subs.rst#berespttl--berespgrace--berespkeep – NITEMAN Jul 08 '20 at 11:42