File: varnish-cli.rst

package info (click to toggle)
varnish 7.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,256 kB
  • sloc: ansic: 104,222; python: 2,679; makefile: 1,303; sh: 1,077; awk: 114; perl: 105; ruby: 41
file content (362 lines) | stat: -rw-r--r-- 10,024 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
..
	Copyright (c) 2011-2021 Varnish Software AS
	SPDX-License-Identifier: BSD-2-Clause
	See LICENSE file for full text of license

.. role:: ref(emphasis)

.. _varnish-cli(7):

===========
varnish-cli
===========

------------------------------
Varnish Command Line Interface
------------------------------

:Manual section: 7

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(1) man page.

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.

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 :ref:`varnishd(1)` for details.

.. _ref_syntax:

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

.. include:: ../include/cli.rst

Backend Pattern
---------------

A backend pattern can be a backend name or a combination of a VCL name
and backend name in "VCL.backend" format.  If the VCL name is omitted,
the active VCL is assumed.  Partial matching on the backend and VCL
names is supported using shell-style wildcards, e.g. asterisk (*).

Examples::

   backend.list def*
   backend.list b*.def*
   backend.set_health default sick
   backend.set_health def* healthy
   backend.set_health * auto


Ban Expressions
---------------

A ban expression consists of one or more conditions.  A condition
consists of a field, an operator, and an argument.  Conditions can be
ANDed together with "&&".

A field can be any of the variables from VCL, for instance req.url,
req.http.host or obj.http.set-cookie.

Operators are "==" for direct comparison, "~" for a regular
expression match, and ">" or "<" for size comparisons.  Prepending
an operator with "!" negates the expression.

The argument could be a quoted string, a regexp, or an integer.
Integers can have "KB", "MB", "GB" or "TB" appended for size related
fields.


.. _ref_vcl_temperature:

VCL Temperature
---------------

A VCL program goes through several states related to the different
commands: it can be loaded, used, and later discarded. You can load
several VCL programs and switch at any time from one to another. There
is only one active VCL, but the previous active VCL will be maintained
active until all its transactions are over.

Over time, if you often refresh your VCL and keep the previous
versions around, resource consumption will increase, you can't escape
that. However, most of the time you want to pay the price only for the
active VCL and keep older VCLs in case you'd need to rollback to a
previous version.

The VCL temperature allows you to minimize the footprint of inactive
VCLs. Once a VCL becomes cold, Varnish will release all the resources
that can be be later reacquired. You can manually set the temperature
of a VCL or let varnish
automatically handle it.

EXAMPLES
========

Load a multi-line VCL using shell-style *here document*::

    vcl.inline example << EOF
    vcl 4.0;

    backend www {
        .host = "127.0.0.1";
        .port = "8080";
    }
    EOF

Ban all requests where req.url exactly matches the string /news::

    ban req.url == "/news"

Ban all documents where the serving host is "example.com" or
"www.example.com", and where the Set-Cookie header received from the
backend contains "USERID=1663"::

    ban req.http.host ~ "^(?i)(www\\.)?example\\.com$" && obj.http.set-cookie ~ "USERID=1663"

AUTHORS
=======

This manual page was originally written by Per Buer and later modified
by Federico G. Schwindt, Dridi Boukelmoune, Lasse Karstensen and
Poul-Henning Kamp.

SEE ALSO
========

* :ref:`varnishadm(1)`
* :ref:`varnishd(1)`
* :ref:`vcl(7)`
* For API use of the CLI: The Reference Manual.