Search code examples
varnishvarnish-vcl

Sending custom response back from varnish through VCL


Is there any way to send back custom responses from varnish itself?

if (req.url ~ "^/hello") {
  return "hello world";
}

Solution

  • You would do this with a synthetic response. For example:

    sub vcl_recv {
      if (req.url ~ "^/hello") {
        error 700 "OK";
      }
    }
    
    sub vcl_error {
      if (obj.status == 700) {
        set obj.http.Content-Type = "text/html; charset=utf-8";
        set obj.status = 200;
        synthetic {"
         <html>
           <head>
              <title>Hello!</title>
           </head>
           <body>
              <h1>Hello world!</h1>
           </body>
         </html>
        "};
      }
    }