Backend
A backend
declaration creates an origin server in VCL code.
This can also be achieved via an API call, using the CLI, or using the web interface. For more information about using backends, see our guide to integrating with backend servers.
TIP: It's usually better to create backends using the API, CLI, or web interface, rather than using custom VCL code. This will offer better validation, and enable a number of features not available to VCL-defined backends, including shielding. Learn more.
Syntax
The following examples show the syntax of a backend definition in VCL with various properties:
backend backend_name {
# Required to be set for all VCL defined backends .dynamic = true; .share_key = "YOUR_SERVICE_ID";
# Server location .host = "storage.googleapis.com"; .port = "443"; .ssl = true; .ssl_cert_hostname = "storage.googleapis.com"; .ssl_check_cert = always; .ssl_sni_hostname = "storage.googleapis.com";
# Timeouts and limits .between_bytes_timeout = 10s; .connect_timeout = 1s; .first_byte_timeout = 15s; .max_connections = 200;
# Host header override .host_header = "storage.googleapis.com"; .always_use_host_header = true;
# Protected properties .bypass_local_route_table = true;
# Health check .probe = { .dummy = false; # Boolean value determines the behavior of the probe. # `true` performs DNS lookups only. # `false` performs DNS lookups and HTTP health checks. .request = "HEAD / HTTP/1.1" "Host: storage.googleapis.com" "Connection: close"; .expected_response = 200; .interval = 60s; # Send a check every 60s .timeout = 2s; # Allow up to 2s for the backend to respond to the check .window = 5; # Keep a history of 5 checks .initial = 4; # Start with 4 successful checks in the history .threshold = 4; # 4 of the recent checks must be successful for backend to be healthy }}
A backend name is alphanumeric and may not start with a number (most backends created via the API, CLI or web interface will be prefixed with F_
in VCL to prevent a leading digit). Non-alphanumeric characters will be converted to _
.
Property descriptions are the same as those described in the API reference, with the following exceptions:
VCL backend property | API equivalent | Note |
---|---|---|
.dynamic | None | Must be set to true in VCL |
.share_key | None | Allows health checks to be consolidated [Learn more] |
.host | address , hostname , ipv4 and ipv6 | |
.ssl | use_ssl | |
.between_bytes_timeout | between_bytes_timeout | See specifying durations |
.connect_timeout | connect_timeout | See specifying durations |
.first_byte_timeout | first_byte_timeout | See specifying durations |
.bypass_local_route_table | None | See bypassing local routing |
| .probe
| Health check API | In the API, health checks are created independently of backends. Learn more about health checks. |
Backend hostname requirements
VCL backend definitions include several fields that can specify a hostname:
.host
.ssl_hostname
.ssl_cert_hostname
.ssl_sni_hostname
RFC guidelines
Hostnames are validated according to the following RFC guidelines:
- RFC 952 Name:
A name is composed of alphabet (A-Z), digits (0-9), minus sign (-), and period (.). The periods are only used as delimiters for the components of the domain name (i.e., labels). - RFC 1123 §2.1 Host names and numbers:
The first letter of a host name may be either a letter or a digit. It should be possible to enter either a host domain name or an IP address in dotted-decimal (#.#.#.#
) form. - RFC 1035 §2.3.4 Size limits:
Labels (delimited by the '.' character) must be 63 octets or fewer and domain names must be 255 octets or fewer. - RFC 1034 §3.1 Name space specifications and terminology:
A zero-length label is reserved for root. Since all domain names end at the root, they are permitted to add a trailing '.' to terminate the name. - RFC 3696 §2 Restrictions on domain (DNS) names:
Top-level domain names cannot be all numeric (e.g.,www.example.123
).
Fastly-specific concessions
- Fastly allows the use of the underscore character in the hostname because of its acceptance by popular browsers and its historical use in web hosting services.
- Fastly allows the use of a wildcard in the
.ssl_hostname
,.ssl_cert_hostname
, and.ssl_sni_hostname
fields.
Specifying durations
Durations of time in VCL backend properties are expressed using RTIME
literals, while the API takes a number in milliseconds.
Backend healthcheck probes
Healthcheck probes monitor DNS changes and the "health state" of the backend for which the probe was defined. Probe healthcheck requests sent from the Fastly client contain the User-Agent "Varnish/2.1+fastly (healthcheck)". If the .ssl
field value is set to true
on the backend, the probe request for the backend is transmitted over TLS.
Healthcheck probes have the following properties:
Field | Description |
---|---|
.dummy | A boolean flag that controls probe behavior. When set to true , the probe only performs DNS resolution of the host if it is a dynamic hostname. When set to false , the probe will perform DNS resolution of the dynamic hostname, as well as transmit periodic HTTP requests to the backend. If not set, defaults to false . |
.request | The HTTP request string for the probe. This property is mutually exclusive with .url . If not set, then .url is used for the probe request. |
.url | The relative URL path to send the probe. An HTTP GET request is constructed from .url and the backend's host_header field value - if defined - otherwise the backend's host field value. This property is mutually exclusive with the .request field. If not set, defaults to / . |
.expected_response | The expected HTTP status code that qualifies as a "healthy status" from the backend. Must be between 100 and 999 . If not set, defaults to 200 . |
.timeout | The timeout to apply to the probe request. Must be a non-negative value followed by a time unit (ms , s , or m ) and must fall between 500ms and 5m . If timeout is <500ms , it is updated to 500ms . This value is also used to timeout DNS queries. If not set or set to 0 , defaults to 2s . |
.interval | The interval at which probe requests are sent to the backend. Must be 0.5s or greater. If not set, defaults to 5s . |
.threshold | The number of probe requests within the specified .window that must succeed in order to determine the backend as "healthy". Must be 64 or less. If .threshold is defined, then .window must also be defined. Must be less than or equal to the .window field value. If not set, defaults to 3 . |
.window | The sliding window of probe requests to evaluate when determining if the backend is "healthy". Must be 64 or less. If .window is defined, then .threshold must also be defined. Must be greater than or equal to the .threshold field value. If not set, defaults to 8 . |
.initial | The artificial number of successful probes to evaluate when the VCL is activated. If not set, defaults to one less than the .threshold value (or 0 if .threshold is set to 0 ). |
In the following example, a probe request is created for my_backend. When the VCL is activated, two probe requests are considered to have succeeded. Three out of five requests, however, are needed to determine if the backend is "healthy". A probe request to www.example.com/website-healthcheck.txt will be sent every fifteen seconds with a timeout of 5s. If the backend responds with a 200 status code for three out of five requests, the backend will be determined as "healthy."
backend my_backend { .host = "www.example.com" .probe = { .request = "HEAD /website-healthcheck.txt HTTP/1.1" "Host: www.example.com" "Connection: close"; .interval = 15s; .initial = 2; .threshold = 3; .window = 5; .dummy = false; }}
Using share_key
to reduce health check load
Backends with identical definitions, including the health check (.probe
property in VCL), will share the same health check process. However, since this behavior can be unexpected, the share_key
property is automatically set to the service ID. This ensures that two backends added to two different services will perform health checks independently, even if they are otherwise identical.
However, consolidating health checks for all identical backends is usually a good idea. To do this, set the share_key
to something that is consistent across multiple services in your account. If the backends are also identical in all other ways, they will share the same health check, reducing the amount of health check traffic to your origin. Learn more about health checks
Bypassing local routing
By default, Fastly cache servers will handle any request from a Fastly service to a backend that is also hosted by Fastly by internally routing within the same machine, except for shielding requests (which target a specific POP). This situation normally arises as a result of service chaining. Bypassing local routing will prompt Fastly to resolve any Fastly-hosted backends using public DNS, which may result in the request being handled by a different cache server (and rarely, perhaps even a different POP).
IMPORTANT: Local route bypass is a protected feature which must be explicitly allowed on your service by a Fastly employee before the route bypass setting will take effect. Contact Fastly support to make a request.
Usage
A backend is assigned to a request by setting the value of req.backend
in VCL:
set req.backend = backend_name;