{"id":884,"date":"2010-02-18T19:48:35","date_gmt":"2010-02-18T23:48:35","guid":{"rendered":"http:\/\/cd34.com\/blog\/?p=884"},"modified":"2012-01-11T11:13:45","modified_gmt":"2012-01-11T16:13:45","slug":"varnish-vcl-inline-c-and-a-random-image","status":"publish","type":"post","link":"https:\/\/cd34.com\/blog\/webserver\/varnish-vcl-inline-c-and-a-random-image\/","title":{"rendered":"Varnish VCL, Inline C and a random image"},"content":{"rendered":"<p>While working with the prototype of a site, I wanted to have a particular panel image randomly chosen when the page was viewed.  While this could be done on the server side, I wanted to move this to Varnish so that Varnish&#8217;s cache would be used rather than piping the request through each time to the origin server.<\/p>\n<p>At the top of \/etc\/varnish\/default.vcl<\/p>\n<pre>C{\r\n  #include &lt;stdlib.h&gt;\r\n  #include &lt;stdio.h&gt;\r\n}C\r\n<\/pre>\n<p>and our vcl_recv function gets the following:<\/p>\n<pre>\r\n  if (req.url ~ \"^\/panel\/\") {\r\n    C{\r\n      char buff[5];\r\n      sprintf(buff,\"%d\",rand()%4);\r\n      VRT_SetHdr(sp, HDR_REQ, \"\\010X-Panel:\", buff, vrt_magic_string_end);\r\n    }C\r\n    set req.url = regsub(req.url, \"^\/panel\/(.*)\\.(.*)$\", \"\/panel\/\\1.ZZZZ.\\2\");\r\n    set req.url = regsub(req.url, \"ZZZZ\", req.http.X-Panel);\r\n  }\r\n<\/pre>\n<p>The above code allows for us to specify the source code in the html document as:<\/p>\n<pre>\r\n&lt;img src=\"\/panel\/random.jpg\" width=\"300\" height=\"300\" alt=\"Panel Image\"\/&gt;\r\n<\/pre>\n<p>Since we have modified the request uri in vcl_recv before the object is cached, subsequent requests for the same modified URI will be served from Varnish&#8217;s cache, without requiring another fetch from the origin server.  Based on the other VCL and preferences, you can specify a long expire time, remove cookies, or do ESI processing.  Since the regexp passes the extension through, we could also randomly choose .html, .css, .jpg or any other extension you desire.<\/p>\n<p>In the directory panel, you would need to have   <\/p>\n<pre>\r\n\/panel\/random.0.jpg\r\n\/panel\/random.1.jpg\r\n\/panel\/random.2.jpg\r\n\/panel\/random.3.jpg\r\n<\/pre>\n<p>which would be served by Varnish when the url \/panel\/random.jpg is requested.<\/p>\n<p>Moving that process to Varnish should cut down on the load from the origin server while making your site look active and dynamic.<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/webserver\/varnish-vcl-inline-c-and-a-random-image\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>While working with the prototype of a site, I wanted to have a particular panel image randomly chosen when the page was viewed. While this could be done on the server side, I wanted to move this to Varnish so that Varnish&#8217;s cache would be used rather than piping the request through each time to [&hellip;]<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/webserver\/varnish-vcl-inline-c-and-a-random-image\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[106,21,105,107],"class_list":["post-884","post","type-post","status-publish","format-standard","hentry","category-webserver","tag-inline-c","tag-varnish","tag-vcl","tag-vcl_recv"],"_links":{"self":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/comments?post=884"}],"version-history":[{"count":12,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/884\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/884\/revisions\/1339"}],"wp:attachment":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/media?parent=884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/categories?post=884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/tags?post=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}