6

We're caching for a problematic IIS server that sometimes just sends empty responses (0 bytes) instead of proper ones. Caching these responses would be a disaster, and we have no way of fixing the problem as it's not our server. Instead I'd like to instruct Varnish to not cache responses from the backend if they are empty (0 bytes).

Reading the VCL reference (https://www.varnish-cache.org/docs/4.0/reference/vcl.html) I can't see any obvious way of solving this.

Can it be done?

Hubro
  • 56,214
  • 69
  • 228
  • 381

2 Answers2

6

If you wanted to use it as a integer to see if greater then or less then value, use std.

import std; 

if (std.integer(beresp.http.content-length, 0) < 500) {
  #logic here 
}
Teebu
  • 677
  • 7
  • 18
  • 1
    The other option is std.bytes https://varnish-cache.org/docs/trunk/reference/vmod_std.html#std-bytes – mwp Dec 04 '19 at 19:33
3

The size of the response should be available as a HTTP header.

Example (in vcl_backend_response):

if (beresp.http.Content-Length == "0") {
    return(retry);   # Retries the request
}

or:

if (beresp.http.Content-Length == "0") {
    beresp.uncacheable = true;   # Prevents object from being cached
}
Hubro
  • 56,214
  • 69
  • 228
  • 381