Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to NOT cache 500 Internal Server Errors in Varnish

Tags:

varnish

vcl

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.

like image 949
Herman Avatar asked May 29 '13 08:05

Herman


1 Answers

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

like image 125
NITEMAN Avatar answered Sep 28 '22 03:09

NITEMAN