VCL-steps
Built-in subroutines#
- Manual section: 7
DESCRIPTION#
Various built-in subroutines are called during processing of client and backend requests as well as upon vcl.load and vcl.discard.
See Varnish Processing States for a detailed graphical overview of the states and how they relate to core code functions and VCL subroutines.
Built-in subroutines always terminate with a return (<action>), where <action> determines how processing continues in the request processing state machine.
The behaviour of actions is identical or at least similar across subroutines, so differences are only documented where relevant.
Common actions are documented in VCL Actions in the next section. Actions specific to only one or some subroutines are documented in VCL Steps.
A default behavior is provided for all Varnish Processing States in the Built-in VCL code.
VCL Actions#
Actions are used with the return(<action>) keyword, which returns control from subroutines back to varnish. The action determines how processing in varnish continues as shown in Varnish Processing States.
Common actions are documented here, while additional actions specific to only one or some subroutines are documented in the next section VCL Steps as well as which action can be used from which built in subroutine.
Common actions for the client and backend side#
fail(err)#
Transition to vcl_synth on the client side as for
return(synth(503, "VCL Failed")), but with any request state changes undone as ifstd.rollback()was called and forcing a connection close. The optional string argument err is logged to VSL under a VCL_Error tag.Returning fail from vcl_synth sends a minimal response with a 500 status.
Intended for fatal errors, for which only minimal error handling is possible.
Common actions for the client side#
synth(status code, reason)#
Transition to vcl_synth with
resp.statusandresp.reasonbeing preset to the arguments ofsynth().
pass#
Switch to pass mode, making the current request not use the cache and not putting its response into it. Control will eventually pass to vcl_pass.
pipe#
Switch to pipe mode. Control will eventually pass to vcl_pipe.
restart#
Restart the transaction. Increases the
req.restartscounter.If the number of restarts is higher than the max_restarts parameter, control is passed to vcl_synth as for
return(synth(503, "Too many restarts"))For a restart, all modifications to
reqattributes are preserved except forreq.restartsandreq.xid, which need to change by design.
Common actions for the backend side#
abandon#
Abandon the backend request. Unless the backend request was a background fetch, control is passed to vcl_synth on the client side with
resp.statuspreset to 503.
VCL Steps#
Client side#
vcl_recv#
Called at the beginning of a request, after the complete request has been received and parsed, after a restart or as the result of an ESI include.
Its purpose is to decide whether or not to serve the request, possibly modify it and decide on how to process it further. A backend hint may be set as a default for the backend processing side.
The vcl_recv subroutine may terminate with calling return() on one of the following keywords:
fail(err)see fail(err) section above
synth(status code, reason)see synth(status code, reason) section above
restartsee restart section above
passsee pass section above
pipesee pipe section above
hashContinue processing the object as a potential candidate for
caching. Passes the control over to vcl_hash.
purgePurge the object and it’s variants. Control passes through
vcl(label)Switch to vcl labelled label.
This will roll back the request as if
std.rollback(req)wascalled and continue vcl processing in vcl_recv of the vcl
labelled label as if it was the active vcl.
The
vcl(label)return is only valid while thereq.restartscount is zero and if used from the active vcl.
See the vcl.label command in varnish-cli.
vcl_pipe#
Called upon entering pipe mode. In this mode, the request is passed on to the backend, and any further data from both the client and backend is passed on unaltered until either end closes the connection. Basically, Varnish will degrade into a simple TCP proxy, shuffling bytes back and forth. For a connection in pipe mode, no other VCL subroutine will ever get called after vcl_pipe.
The vcl_pipe subroutine may terminate with calling return() with one of the following keywords:
fail(err)see fail(err) section above
synth(status code, reason)see synth(status code, reason) section above
pipeProceed with pipe mode.
vcl_pass#
Called upon entering pass mode. In this mode, the request is passed on to the backend, and the backend’s response is passed on to the client, but is not entered into the cache. Subsequent requests submitted over the same client connection are handled normally.
The vcl_pass subroutine may terminate with calling return() with one of the following keywords:
fail(err)see fail(err) section above
synth(status code, reason)see synth(status code, reason) section above
restartsee restart section above
fetchProceed with pass mode - initiate a backend request.
vcl_hash#
Called after vcl_recv to create a hash value for the request. This is used as a key to look up the object in Varnish.
The vcl_hash subroutine may terminate with calling return() with one of the following keywords:
fail(err)see fail(err) section above
lookupLook up the object in cache.
Control passes to vcl_purge when coming from a
purgereturn in vcl_recv.
Otherwise control passes to the next subroutine depending on the
result of the cache lookup:
a hit: pass to vcl_hit
a miss or a hit on a hit-for-miss object (an object with
obj.uncacheable == true): pass to vcl_miss
- a hit on a hit-for-pass object (for which
pass(DURATION)had beenpreviously returned from
vcl_backend_response): pass to
vcl_purge#
Called after the purge has been executed and all its variants have been evicted.
The vcl_purge subroutine may terminate with calling return() with one of the following keywords:
fail(err)see fail(err) section above
synth(status code, reason)see synth(status code, reason) section above
restartsee restart section above
vcl_miss#
Called after a cache lookup if the requested document was not found in the cache or if vcl_hit returned fetch.
Its purpose is to decide whether or not to attempt to retrieve the document from the backend. A backend hint may be set as a default for the backend processing side.
The vcl_miss subroutine may terminate with calling return() with one of the following keywords:
fail(err)see fail(err) section above
synth(status code, reason)see synth(status code, reason) section above
restartsee restart section above
passsee pass section above
fetchRetrieve the requested object from the backend. Control will
eventually pass to vcl_backend_fetch.
