Varnish Configuration Language (VCL)·续1

The following return actions are supported in vcl_recv:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(synth(status code, reason)): returns synthetic output using the returned status code and reason phrase.
  • return(restart): restarts the transaction, increases the restart counter and fails if max_restarts is reached
  • return(pass): bypass the cache
  • return(pipe): switch to pipe mode
  • return(hash): lookup the object in the cache
  • return(purge): purge the object and its variants from the cache
  • return(vcl(label)): load a labeled VCL configuration and call its vcl_recv subroutine

Built-in VCL for vcl_recv →

vcl_pipe#

The vcl_pipe subroutine is triggered when pipe mode is enabled by calling return(pipe) inside vcl_recv.

As described in the piping flow, pipe mode will degrade the incoming request to plain TCP and no longer consider it to be an HTTP request. The bytes are shuffled over the wire and the connection is closed.

The following return actions are supported in vcl_pipe:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(synth(status code, reason)): returns synthetic output using the returned status code and reason phrase.
  • return(pipe): proceed with pipe mode and send the data directly to the origin server

Built-in VCL for vcl_pipe →

vcl_pass#

The vcl_passsubroutine is triggered when the cache is bypassed by calling return(pass).

As described in the cache bypass flow, uncacheable requests will bypass the cache and send the incoming HTTP request to the origin server. The returned response is not stored in the cache.

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(synth(status code, reason)): returns synthetic output using the returned status code and reason phrase.
  • return(restart): restarts the transaction, increases the restart counter and fails if max_restarts is reached
  • return(fetch): initiate a backend request and transition to vcl_backend_fetch

Built-in VCL for vcl_pass →

vcl_hash#

The vcl_hash subroutine is responsible for creating the hash key and looking up the object in the cache. The subroutine is called directly after vcl_recv and the support return actions are:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(lookup): lookup the object in the cache using the hash key that was created

As described in the cache hit flow, a transition to vcl_hit is made when the object is found.

As described in the cache miss flow, a transition to vcl_miss is made when the object is found.

A transition to vcl_miss also takes place if the object is a Hit-For-Miss object.

As described in the cache bypass flow, a transition to vcl_pass is made when the object bypass the cache.

Built-in VCL for vcl_hash →

vcl_purge#

The vcl_purge subroutine is reached when return(purge) is called in vcl_recv to purge an object from the cache. This is also mentioned in the cache purge flow.

vcl_purge supports the following return actions:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(synth(status code, reason)): returns synthetic output using the returned status code and reason phrase.
  • return(restart): restarts the transaction, increases the restart counter and fails if max_restarts is reached

Built-in VCL for vcl_purge →

vcl_miss#

The vcl_miss subroutine is called when an object was not found in the cache or if the object was considered stale in vcl_hit.

As described in the cache miss flow, vcl_miss will transition to vcl_backend_fetch and fetch the missing or outdated content from the origin server.

vcl_miss supports the following return actions:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(synth(status code, reason)): returns synthetic output using the returned status code and reason phrase.
  • return(restart): restarts the transaction, increases the restart counter and fails if max_restarts is reached
  • return(pass): decide to bypass the cache
  • return(fetch): initiate a backend request and transition to vcl_backend_fetch

Built-in VCL for vcl_miss →

vcl_hit#

The vcl_hit subroutine is called when the cache lookup is successful.

As described in the cache hit flow, vcl_hit will return the cached object to the client by transitioning to vcl_deliver.

vcl_hit supports the following return actions:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(synth(status code, reason)): returns synthetic output using the returned status code and reason phrase.
  • return(restart): restarts the transaction, increases the restart counter and fails if max_restarts is reached
  • return(pass): decide to bypass the cache
  • return(deliver): deliver the object to the client. For stale content a asynchronous backend fetch is triggered while stale content is returned.

Built-in VCL for vcl_hit →

vcl_deliver#

The vcl_deliver subroutine is called just before a response is returned to the requesting client.

vcl_deliver supports the following return actions:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(synth(status code, reason)): returns synthetic output using the returned status code and reason phrase.
  • return(restart): restarts the transaction, increases the restart counter and fails if max_restarts is reached
  • return(deliver): deliver the object to the client

Built-in VCL for vcl_deliver →

vcl_synth#

The vcl_synth subroutine delivers synthetic content to the client. This is content that did not originate from the origin server, but that was generated in VCL instead.

vcl_synth supports the following return actions:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(restart): restarts the transaction, increases the restart counter and fails if max_restarts is reached
  • return(deliver): deliver the synthetic response to the client

Built-in VCL for vcl_synth →

Backend-side subroutines#

The backend-side subroutines are responsible for interacting with the origin server. There are only three of them and in the diagram they are part of the grey box where the cache miss flow and the cache bypass flow lead to.

The backend-side routines are generally slower than their client-side counterparts, but that is mainly caused by the fact that these subroutines have to wait for data to be sent over the wire.

vcl_backend_fetch#

The vcl_backend_fetch subroutine is responsible for sending HTTP requests to the origin server and triggers the backend fetch.

vcl_backend_fetch supports the following return actions:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(fetch): fetch the object from the backend
  • return(error(status code, reason)): returns a synthetic error under the form of a backend error by transitioning to vcl_backend_error
  • return(abandon): abandon the backend request and transition to vcl_synth unless the request is background fetch. The equivalent of return(synth(503, "VCL Failed"))

Built-in VCL for vcl_backend_fetch →

vcl_backend_response#

The vcl_backend_response subroutine is called when Varnish receives the HTTP response as a result from the backend fetch that took place in vcl_backend_fetch.

In vcl_backend_response a number of checks are made that decide whether or not the object is stored in the cache and how long is it cached for.

As described in the cache bypass flow, content that is deemed uncacheable is marked as Hit-For-Miss.

vcl_backend_response supports the following return actions:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(deliver): fetch the object body from the backend and deliver content to any waiting client request. This can be done in parallel and streaming is supported
  • return(retry): retry the backend transaction, increases the retries counter and fails if max_retries is reached
  • return(error(status code, reason)): returns a synthetic error under the form of a backend error by transitioning to vcl_backend_error
  • return(pass(duration)): enables Hit-For-Pass for uncacheable objects, which causes subsequent requests for this resource to transition from vcl_recv to vcl_pass.
  • return(abandon): abandon the backend request and transition to vcl_synth unless the request is background fetch. The equivalent of return(synth(503, "VCL Failed"))

Built-in VCL for vcl_backend_response →

vcl_backend_error#

The vcl_backend_error subroutine is called when a backend fetch failure occurs or when the max_retries counter has been exceeded.

Inside vcl_backend_error, a synthetic response is created using an output template that is identical to the output template that is generated by the vcl_synth subroutine.

The only difference is that vcl_backend_error is a backend-side subroutine, which means other variables are parsed. Whereas vcl_synth parses in variables like resp.status and resp.reason, the vcl_backend_error parses in beresp.status and beresp.reason.

vcl_backend_error supports the following return actions:

  • return(fail): triggers a fatal error. The equivalent of return(synth(503, "VCL Failed"))
  • return(deliver): deliver the synthetic error message as if it was fetched by the backend
  • return(retry): retry the backend transaction, increases the retries counter and fails if max_retries is reached

Built-in VCL for vcl_backend_error →

VCL initialization subroutines#

vcl_init#

The vcl_init subroutine is called when the VCL is loaded, beofre any requests are received. This subroutine is mainly used to initialize Varnish modules.

vcl_init supports the following return actions:

  • return(ok): VCL initialization was successful
  • return(fail): VCL initialization aborted

Built-in VCL for vcl_init →

vcl_fini#

The vcl_fini subroutine is called when the VCL is discarded after all the incoming requests have exited the VCL. You would use this subroutine to handle Varnish modules that require cleanup.

The only return action that vcl_fini supports is return(ok).

Built-in VCL for vcl_fini →

VCL syntax#

VCL code is executed inside the various subroutines that we just covered. But before you can start writing VCL, you need to be familiar with its syntax.