VCL-steps·续

vcl_hit#

Called when a cache lookup is successful. The object being hit may be stale: It can have a zero or negative ttl with only grace or keep time left.

The vcl_hit 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

restart

see restart section above

pass

see pass section above

deliver

Deliver the object. If it is stale, a background fetch to refresh

it is triggered.

vcl_deliver#

Called before any object except a vcl_synth result is delivered to the client.

The vcl_deliver 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

restart

see restart section above

deliver

Deliver the object to the client.

vcl_synth#

Called to deliver a synthetic object. A synthetic object is generated in VCL, not fetched from the backend. Its body may be constructed using the synthetic() function.

A vcl_synth defined object never enters the cache, contrary to a vcl_backend_error defined object, which may end up in cache.

The subroutine may terminate with calling return() with one of the following keywords:

fail(err)

see fail(err) section above

restart

see restart section above

deliver

Directly deliver the object defined by vcl_synth to the client

without calling vcl_deliver.

Backend Side#

vcl_backend_fetch#

Called before sending the backend request. In this subroutine you typically alter the request before it gets to the backend.

The vcl_backend_fetch subroutine may terminate with calling return() with one of the following keywords:

fail(err)

see fail(err) section above

abandon

see abandon section above

fetch

Fetch the object from the backend.

error(status code, reason)

Transition to vcl_backend_error with beresp.status and

beresp.reason being preset to the arguments of error() if

arguments are provided.

Before calling vcl_backend_fetch, Varnish core prepares the bereq backend request as follows:

  • Unless the request is a pass,
    • set bereq.method to GET and bereq.proto to HTTP/1.1 and
    • set bereq.http.Accept_Encoding to gzip if http_gzip_support is enabled.
  • If there is an existing cache object to be revalidated, set bereq.http.If-Modified-Since from its Last-Modified header and/or set bereq.http.If-None-Match from its Etag header
  • Set bereq.http.X-Varnish to the current transaction id (vxid)

These changes can be undone or modified in vcl_backend_fetch before the backend request is issued.

In particular, to cache non-GET requests, req.method needs to be saved to a header or variable in vcl_recv and restored to bereq.method. Notice that caching non-GET requests typically also requires changing the cache key in vcl_hash e.g. by also hashing the request method and/or request body.

HEAD request can be satisfied from cached GET responses.

vcl_backend_refresh#

This subroutine is called after a legitimate 304 response is received from the backend so that you can chose how to handle the object revalidation.

The default is to apply the RFC logic which is to merge the new headers with the stale ones.

The returned value only affects response headers, the body that is delivered is always the one from the stale object.

See vcl_refresh_* for additional explanations.

merge

Merge the headers we got from the backend response with

the ones we had in the cached object.

obj_stale

Return the stale object’s headers without performing the RFC merge logic.

beresp

Return the beresp headers as they were at the end of this subroutine

without executing the RFC merge logic. Any content-length and

content-encoding headers present in the 304 beresp will be removed and

replaced by the stale object ones if available. Etag and Last-Modified

headers are also copied from the stale object to allow future revalidations.

fail

see fail(err) section above

abandon

see abandon section above

retry

Retry the backend transaction. Increases the retries counter.

If the number of retries is higher than max_retries ,

control will be passed to vcl_backend_error.

error(status code, reason)

Transition to vcl_backend_error with beresp.status and

beresp.reason being preset to the arguments of error() if

arguments are provided.

vcl_backend_response#

Called after the response headers have been successfully retrieved from the backend.

The vcl_backend_response subroutine may terminate with calling return() with one of the following keywords:

fail(err)

see fail(err) section above

abandon

see abandon section above

deliver

For a 304 response, create an updated cache object.

Otherwise, fetch the object body from the backend and initiate

delivery to any waiting client requests, possibly in parallel

(streaming).

retry

Retry the backend transaction. Increases the retries counter.

If the number of retries is higher than max_retries ,

control will be passed to vcl_backend_error.

pass(duration)

Mark the object as a hit-for-pass for the given duration. Subsequent

lookups hitting this object will be turned into passed transactions,

as if vcl_recv had returned pass.

error(status code, reason)

Transition to vcl_backend_error with beresp.status and

beresp.reason being preset to the arguments of error() if

arguments are provided.

vcl_backend_error#

This subroutine is called if we fail the backend fetch or if max_retries has been exceeded.

Returning with abandon does not leave a cache object.

If returning with deliver and beresp.uncacheable == false, a synthetic cache object is generated in VCL, whose body may be constructed using the synthetic() function.

Since these synthetic objects are cached in these special circumstances, be cautious with putting private information there. If you really must, then you need to explicitly set beresp.uncacheable to true in vcl_backend_error.

The vcl_backend_error subroutine may terminate with calling return() with one of the following keywords:

fail(err)

see fail(err) section above

abandon

see abandon section above

deliver

Deliver and possibly cache the object defined in

vcl_backend_error as if it was fetched from the backend , also

referred to as a “backend synth”.

retry

Retry the backend transaction. Increases the retries counter.

If the number of retries is higher than max_retries ,

vcl_synth on the client side is called with resp.status

preset to 503.

During vcl.load / vcl.discard#

vcl_init#

Called when VCL is loaded, before any requests pass through it. Typically used to initialize VMODs.

The vcl_init subroutine may terminate with calling return() with one of the following keywords:

ok

Normal return, VCL continues loading.

fail(err)

Abort loading of this VCL.

vcl_fini#

Called when VCL is discarded only after all requests have exited the VCL. Typically used to clean up VMODs.

The vcl_fini subroutine may terminate with calling return() with one of the following keywords:

ok

Normal return, VCL will be discarded.

SEE ALSO#

This document is licensed under the same license as Varnish itself. See LICENSE for details.

  • Copyright (c) 2006 Verdens Gang AS
  • Copyright (c) 2006-2021 Varnish Software AS