{"id":391,"date":"2009-08-24T11:24:40","date_gmt":"2009-08-24T11:24:40","guid":{"rendered":"https:\/\/www.mohanjith.net\/blog\/?p=391"},"modified":"2010-04-30T09:47:47","modified_gmt":"2010-04-30T09:47:47","slug":"using-custom-error-pages-in-varnish","status":"publish","type":"post","link":"https:\/\/mohanjith.net\/blog\/2009\/08\/using-custom-error-pages-in-varnish.html","title":{"rendered":"Using custom error pages in Varnish"},"content":{"rendered":"<p>While playing around with the <a href=\"\/blog\/2009\/08\/fancy-http-error-pages-5xx.html\">fancy error pages<\/a> I wanted to use them with <a href=\"http:\/\/varnish.projects.linpro.no\/\">Varnish<\/a> 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. <a href=\"http:\/\/pastebin.com\/f1fa0f6a1\">Here<\/a> is the whole vcl_error sub. You will notice that we fall back to default Varnish error page for anything other than 5XX errors.<\/p>\n<p>[code lang=&#8217;c&#8217;]sub vcl_error {<br \/>\nset obj.http.Content-Type = &#8220;text\/html; charset=utf-8&#8221;;<\/p>\n<p>if ( obj.status >= 500 &#038;&#038; obj.status <= 505) {\nC{\n#include <stdio.h><br \/>\n#include <string.h><\/p>\n<p>FILE * pFile;<br \/>\nchar content [100];<br \/>\nchar page [10240];<br \/>\nchar fname [50];<\/p>\n<p>page[0] = &#8216;\\0&#8217;;<br \/>\nsprintf(fname, &#8220;\/var\/www\/errors\/%d.html&#8221;, VRT_r_obj_status(sp));<\/p>\n<p>pFile = fopen(fname, &#8220;r&#8221;);<br \/>\nwhile (fgets(content, 100, pFile)) {<br \/>\nstrcat(page, content);<br \/>\n}<br \/>\nfclose(pFile);<br \/>\nVRT_synth_page(sp, 0, page, &#8220;<!-- XID: \", VRT_r_req_xid(sp), \" -->&#8220;, vrt_magic_string_end);<br \/>\n}C<br \/>\n} else {<br \/>\nsynthetic {&#8221;<br \/>\n<?xml version=\"1.0\" encoding=\"utf-8\"?><br \/>\n<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN\"\n\"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd\"><br \/>\n<html><br \/>\n<head><br \/>\n<title>&#8220;} obj.status &#8221; &#8221; obj.response {&#8220;<\/title><br \/>\n<\/head><br \/>\n<body><\/p>\n<h1>Error &#8220;} obj.status &#8221; &#8221; obj.response {&#8220;<\/h1>\n<p>&#8220;} obj.response {&#8220;<\/p>\n<h3>Guru Meditation:<\/h3>\n<p>XID: &#8220;} req.xid {&#8220;<\/p>\n<address>\n<a href=\"http:\/\/www.varnish-cache.org\/\">Varnish<\/a><br \/>\n<\/address>\n<p><\/body><br \/>\n<\/html><br \/>\n&#8220;};<br \/>\n}<\/p>\n<p>return (deliver);<br \/>\n}[\/code]<\/p>\n<p>Hope someone will find this useful as I had to put some effort to find out all the internal Varnish function names<\/p>\n<div id=\"fb-like\" style=\"\"><iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https:\/\/mohanjith.net\/blog\/2009\/08\/using-custom-error-pages-in-varnish.html&amp;layout=standard&amp;show_faces=true&amp;width=300&amp;action=like&amp;font=&amp;colorscheme=light&amp;locale=en_US\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:300px; height:30px\"><\/iframe><\/div>","protected":false},"excerpt":{"rendered":"<p>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 &#8230; <a title=\"Using custom error pages in Varnish\" class=\"read-more\" href=\"https:\/\/mohanjith.net\/blog\/2009\/08\/using-custom-error-pages-in-varnish.html\" aria-label=\"More on Using custom error pages in Varnish\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[209],"tags":[459,211],"class_list":["post-391","post","type-post","status-publish","format-standard","hentry","category-errors","tag-errors","tag-varnish"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5lUHm-6j","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":23,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":394,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions\/394"}],"wp:attachment":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/categories?post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/tags?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}