Varnish Command Line Interface
Description#
Varnish has a command line interface (CLI) which can control and change most of the operational parameters and the configuration of Varnish, without interrupting the running service.
The CLI can be used for the following tasks:
Configuration
You can upload, change and delete VCL files from the CLI.
Parameters
You can inspect and change the various parameters Varnish has available through the CLI. The individual parameters are documented in the varnishd reference documentation.
Bans
Bans are filters that are applied to keep Varnish from serving stale content. When you issue a ban Varnish will not serve any banned object from cache, but rather re-fetch it from its backend servers.
Process management
You can stop and start the cache (child) process though the CLI. You can also retrieve the latest stack trace if the child process has crashed.
TLS certificate management
You can load, discard, reload, list and roll back TLS certificates.
If you invoke varnishd(1) with -T, -M or -d the CLI will be available. In debug mode (-d) the CLI will be in the foreground, with -T you can connect to it with varnishadm or telnet and with -M varnishd will connect back to a listening service pushing the CLI to that service. Please see varnishd for details.
Syntax#
The Varnish CLI is similar to another command line interface, the Bourne Shell. Commands are usually terminated with a newline, and they may take arguments. The command and its arguments are tokenized before parsing, and as such arguments containing spaces must be enclosed in double quotes.
It means that command parsing of
help banner
is equivalent to
"help" banner
because the double quotes only indicate the boundaries of the help token.
Within double quotes you can escape characters with \ (backslash). The \n, \r, and \t get translated to newlines, carriage returns, an tabs. Double quotes and backslashes themselves can be escaped with \” and \\ respectively.
To enter characters in octals use the \nnn syntax. Hexadecimals can be entered with the \xnn syntax.
Commands may not end with a newline when a shell-style here document (here-document or heredoc) is used. The format of a here document is:
<< word
here document
word
word can be any continuous string chosen to make sure it doesn’t appear naturally in the following here document. Traditionally EOF or END is used.
Quoting pitfalls#
Integrating with the Varnish CLI can be sometimes surprising when quoting is involved. For instance in Bourne Shell the delimiter used with here documents may or may not be separated by spaces from the << token:
cat <<EOF
hello
world
EOF
hello
world
With the Varnish CLI, the << and EOF tokens must be separated by at least one blank:
vcl.inline boot <<EOF
106 258
Message from VCC-compiler:
VCL version declaration missing
Update your VCL to Version 4 syntax, and add
vcl 4.0;
on the first line of the VCL files.
('<vcl.inline>' Line 1 Pos 1)
<<EOF
##---
Running VCC-compiler failed, exited with 2
VCL compilation failed
With the missing space, the here document can be added and the actual VCL can be loaded:
vcl.inline test << EOF
vcl 4.0;
backend be {
.host = "localhost";
}
EOF
200 14
VCL compiled.
A big difference with a shell here document is the handling of the << token. Just like command names can be quoted, the here document token keeps its meaning, even quoted:
vcl.inline test "<<" EOF
vcl 4.0;
backend be {
.host = "localhost";
}
EOF
200 14
VCL compiled.
When using a front-end to the Varnish-CLI like varnishadm, one must take into account the double expansion happening. First in the shell launching the varnishadm command and then in the Varnish CLI itself. When a command’s parameter require spaces, you need to ensure that the Varnish CLI will see the double quotes:
varnishadm param.set cc_command '"my alternate cc command"'
Change will take effect when VCL script is reloaded
Otherwise if you don’t quote the quotes, you may get a seemingly unrelated error message:
varnishadm param.set cc_command "my alternate cc command"
Unknown request.
Type 'help' for more info.
Too many parameters
Command failed with error code 105
If you are quoting with a here document, you must wrap it inside a shell multi-line argument:
varnishadm vcl.inline test '<< EOF
vcl 4.0;
backend be {
.host = "localhost";
}
EOF'
VCL compiled.
Another difference with a shell here document is that only one here document can be used on a single command line. For example, it is possible to do this in a shell script:
#!/bin/sh
cat << EOF1 ; cat << EOF2
hello
EOF1
world
EOF2
The expected output is:
hello
world
With the Varnish CLI, only the last parameter may use the here document form, which greatly restricts the number of commands that can effectively use them. Trying to use multiple here documents only takes the last one into account.
For example:
command argument << EOF1 << EOF2
heredoc1
EOF1
heredoc2
EOF2
This conceptually results in the following command line:
"command""argument""<<""EOF1""heredoc1\nEOF1\nheredoc2\n"
Other pitfalls include variable expansion of the shell invoking varnishadm but this is not directly related to the Varnish CLI. If you get the quoting right you should be fine even with complex commands.
JSON#
A number of commands with informational responses support a -j parameter for JSON output, as specified below. The top-level structure of the JSON response is an array with these first three elements:
- A version number for the JSON format (integer)
- An array of strings that comprise the CLI command just received
- The time at which the response was generated, as a Unix epoch time in seconds with millisecond precision (floating point)
The remaining elements of the array form the data that are specific to the CLI command, and their structure and content depend on the command.
For example, the response to status -j just contains a string in the top-level array indicating the state of the child process ("running", "stopped" and so forth):
[ 2, ["status", "-j"], 1538031732.632, "running"]
The JSON responses to other commands may have longer lists of elements, which may have simple data types or form structured objects.
JSON output is only returned if command execution was successful. The output for an error response is always the same as it would have been for the command without the -j parameter.
Commands#
auth #
Authenticate.
backend.list [-j] [-p] [<backend_pattern>]#
List backends.
-p also shows probe status.
-j specifies JSON output.
Unless -j is specified for JSON output, the output format is five columns of dynamic width, separated by white space with the fields:
-
Backend name
-
Admin: How health state is determined:
-
healthy: Sethealthythroughbackend.set_health.sick: Setsickthroughbackend.set_health.probe: Health state determined by a probe or some other dynamic mechanism.deleted: Backend has been deleted, but not yet cleaned up.
Admin has precedence over Health
- Probe
X/Y: X out of Y checks have succeeded
X and Y are backend specific and may represent probe checks, other backends or any other metric.
If there is no probe or the director does not provide details on probe check results, 0/0 is output.
-
Health: Probe health state
-
healthysick
If there is no probe, healthy is output.
- Last change: Timestamp when the health state last changed.
The health state reported here is generic. A backend’s health may also depend on the context it is being used in (e.g. the object’s hash), so the actual health state as visible from VCL (e.g. using std.healthy()) may differ.
For -j, the object members should be self explanatory, matching the fields described above. probe_message has the format [X, Y, "state"] as described above for Probe. JSON Probe details (-j -p arguments) are director specific.
backend.set_health <backend_pattern> [auto|healthy|sick]#
Set health status of backend(s) matching <backend_pattern>.
- With
auto, the health status is determined by a probe or some other dynamic mechanism, if any healthysets the backend as usablesicksets the backend as unusable
ban [&& …]#
Mark obsolete all objects where all the conditions match.
See BOOL ban(STRING) for details
ban.list [-j]#
List the active bans.
Unless -j is specified for JSON output, the output format is:
- Time the ban was issued.
- Objects referencing this ban.
Cif ban is completed = no further testing against it.- if
lurkerdebugging is enabled:Rfor req.* testsOfor obj.* tests- Pointer to ban object
- Ban specification
Durations of ban specifications get normalized, for example “7d” gets changed into “1w”.
banner#
Print welcome banner.
help [-j|]#
Show command/protocol help.
-j specifies JSON output.
panic.clear [-z]#
Clear the last panic, if any, -z will clear related varnishstat counter(s)
