title: How to NOT cache 500 Internal Server Errors in Varnish
body: <p>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.</p>
  
  <pre><code>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 &gt;= 500) {
        set beresp.grace = 1s;
        set beresp.ttl = 1s;
        return (hit_for_pass);
      }
  
      # Unset the "etag" header (suggested)
      unset beresp.http.etag;
  
      return(deliver);
  }
  </code></pre>
  
  <p>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.</p>