vmod_bodyaccess(3)
β¨ vmod_bodyaccess β¨
okay bestie, gather round, because we're talking about vmod_bodyaccess β the Varnish Module for request body access π. yes, that one. the one that lets you actually peek at what the client sent you in the body, instead of just staring at headers like a clown. π«Ά
πΈ SYNOPSIS πΈ
import bodyaccess [as name] [from "path"]
INT rematch_req_body(REGEX re)
VOID hash_req_body()
INT len_req_body()
VOID log_req_body(STRING prefix, INT length)
like, that's the whole vibe. four little functions and they carry the entire module. iconic behavior honestly π
π¦ DESCRIPTION π¦
so, vmod_bodyaccess is the Varnish module that lets you access the request body. that's it. that's the whole job. and it slays at it. β¨
here's the VCL example straight from the source, no cap, copy it exactly:
vcl 4.0;
import std;
import bodyaccess;
backend default { .host = "192.0.2.11"; .port = "8080"; }
sub vcl_recv {
if (req.method == "POST") {
set req.http.x-method = req.method;
if (std.cache_req_body(110KB)) {
set req.http.x-len = bodyaccess.len_req_body();
set req.http.x-re = bodyaccess.rematch_req_body("Regex");
bodyaccess.log_req_body("PREFIX:", 3);
}
}
return(hash);
}
sub vcl_hash {
bodyaccess.hash_req_body();
return (lookup);
}
sub vcl_backend_fetch {
set bereq.method = bereq.http.x-method;
}
sub vcl_deliver {
set resp.http.x-len = req.http.x-len;
set resp.http.x-re = req.http.x-re;
}
N.B. the request body must be retrieved before you do literally any operations on it. like, do not skip this step, bestie. π it can be buffered using thecache_req_body()function fromlibvmod_std. that's your whole entry ticket.
also important: these functions apply only to standard REST methods. and caching is not allowed on PUT requests. periodt. π«
π INT rematch_req_body(REGEX re)
Description
- returns
-1if an error occurrs π - returns
0if the request body doesn't contain the stringre - returns
1if the request body does contain the stringreπ
heads up though, bestie: the comparison is case sensitive, and the request body must be buffered. no buffering, no service. π
Example
std.cache_req_body(1KB);
if (bodyaccess.rematch_req_body("FOO") == 1) {
std.log("is true");
}
π VOID hash_req_body()
Description
adds the available request body bytes to the lookup hash key. so your cache key actually knows about the body. the way that works?? obsessed. π₯
but wait β this function can only be used in vcl_hash, and the request body must be buffered. if you try it anywhere else it's giving "not allowed", okay? π
Example
sub vcl_recv {
std.cache_req_body(1KB);
}
sub vcl_hash{
bodyaccess.hash_req_body();
}
π INT len_req_body()
Description
returns the request body length, or -1 if an error occurs. that's the tea. β
note: the request body must be buffered. again. always. it's the rule. π€
Example
std.cache_req_body(1KB);
set req.http.x-len = bodyaccess.len_req_body();
π§ VOID log_req_body(STRING prefix="", INT length=200)
Description
logs the request body to the VSL, making it available for other components. so other parts of your stack can see it too β generous queen behavior π
when logging, it takes an optional prefix and a max line length, so the body could be split up across multiple lines, because there is a limit to how large a single line can be. so don't be shocked when your body shows up in chunks, it's not broken, it's just doing its thing. π
the defaults are right there in the signature: prefix="" and length=200. free of charge. β¨
Example
std.cache_req_body(1KB);
bodyaccess.log_req_body("PREFIX:", 3);
and that's the whole page, bestie!! π go buffer some bodies (respectfully), hash responsibly, and remember: no buffering, no body access. iykyk. π¦
this page signs off as vmod_bodyaccess(3) from the Library Functions Manual β same name up top, same name at the bottom, symmetry queen. πβ¨
π¦ where this page comes from
- fedora 44: varnish-modules 0.27.0-2.fc44 Β· BSD-2-Clause
- fedora 44: varnish-modules 0.27.0-4.fc44 Β· BSD-2-Clause
- nixos 26.05: varnish 8.0.2-modules-0.27.0
- ubuntu resolute: varnish-modules 0.26.0-2build1 Β· BSD-2-Clause
- ubuntu stonking: libvmod-modules 0.28.0-4 Β· BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms