Mastering Varnish: Architecture, VCL Functions, and Configuration Guide
This article provides a comprehensive overview of Varnish, covering its high‑performance caching architecture, core processes, built‑in VCL functions, variable usage across request handling stages, and practical configuration examples for optimizing web server response speed.
Varnish is a high‑performance open‑source HTTP accelerator (a caching reverse proxy) that stores responses in memory or files to speed up web servers. Compared with Squid, Varnish offers higher performance, faster speed and easier management.
Varnish Program Structure
Management process: compiles VCL, applies new configuration, monitors Varnish, initializes Varnish, provides a CLI, initializes cache memory structures and partitions memory into blocks of various sizes.
Child/Cache threads:
Acceptor: accepts new connections;
Worker: processes and responds to user requests;
Expiry: cleans expired cache objects.
Log: Shared Memory Log, typically 90 MB, divided into counters and request‑related data.
Cache data management:
Expires: defines cache lifetime;
http validation;
If-Modified-Since/Last-Modified: Varnish asks the backend if the object has changed since cached; returns 304 if unchanged.
If-None-Match/Etag: uses a tag and timestamp to validate cached content.
VCL built‑in functions and processing flow (state engine)
VCL built‑in functions: vcl_recv, vcl_fetch, vcl_pipe, vcl_hash, vcl_pass, vcl_hit, vcl_miss, vcl_deliver, vcl_error.
vcl_recv: handles incoming request, decides routing, backend selection, etc.
vcl_fetch: decides whether to store response in cache or pass it directly.
vcl_pipe: pipes unknown requests directly to backend.
vcl_hash: defines data used for hash key generation.
vcl_pass: forwards request to backend without caching.
vcl_hit: actions when cache object is found.
vcl_miss: actions when cache object is not found.
vcl_deliver: prepares response for client.
vcl_error: synthesizes error responses.
VCL syntax:
//, #, /* */ comment.
sub $NAME defines a function.
No loops supported.
Many built‑in variables.
Supports termination statements without return values.
Domain‑specific language.
Operators: =, ==, ~, !, &&, ||.
Common variables:
Global: .Now, .host, .port.
Request phase: client.ip, server.hostname, server.ip, server.port, req.request, req.url, req.proto, req.backend, req.backend.healthy, req.http.HEADER, req.can_gzip, req.restarts.
Before backend request: bereq.request, bereq.url, bereq.proto, bereq.http.HEADER, bereq.connect_timeout.
After backend response, before caching: beresp.backend.ip, beresp.ttl, beresp.do_stream, beresp.do_gzip, beresp.do_gunzip, beresp.http.HEADER, beresp.proto, beresp.status, beresp.response, beresp.backend.name, beresp.backend.port, beresp.storage.
Cache object: obj.proto, obj.status, obj.response, obj.ttl, obj.hits, obj.http.HEADER.
Hash calculation: req.hash.
Client response: resp.http.HEADER, resp.proto, resp.status, resp.response.
Varnish configuration example:
/etc/sysconfig/varnish defines runtime characteristics.
/etc/varnish/default.vcl defines default VCL state engine.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
