Using custom error pages in Varnish

Serve custom HTML error pages from Varnish using an inline C snippet to read files from disk, with fallback to default Varnish pages for non-5xx status codes.

Using custom error pages in Varnish

While playing around with the fancy error pages I wanted to use them with Varnish as well. Since there is no means to include a file or serve a file from Varnish (without serving from a back end server), I went with inline C snippet to read and serve the error pages. Please note that the style sheet and the images are being served from a CDN. Otherwise it will have to be cached prior to the back end server becoming inaccessible. Here is the whole vcl_error sub. You will notice that we fall back to default Varnish error page for anything other than 5XX errors.

sub vcl_error { set obj.http.Content-Type = “text/html; charset=utf-8”;

if ( obj.status >= 500 && obj.status <= 505) { C{ #include #include

FILE * pFile; char content ; char page ; char fname ;

page = ‘\0’; sprintf(fname, “/var/www/errors/%d.html”, VRT_r_obj_status(sp));

pFile = fopen(fname, “r”); while (fgets(content, 100, pFile)) { strcat(page, content); } fclose(pFile); VRT_synth_page(sp, 0, page, “”, vrt_magic_string_end); }C } else { synthetic {“ <?xml version=”1.0” encoding=”utf-8”?> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

"} obj.status " " obj.response {"

Error "} obj.status " " obj.response {"

"} obj.response {"

Guru Meditation:

XID: "} req.xid {"

Varnish

”}; }

return (deliver); }

Hope someone will find this useful as I had to put some effort to find out all the internal Varnish function names

Cookies Contact Privacy Policy Terms