- Flow of lua nginx module directives: https://openresty-reference.readthedocs.io/en/latest/Directives/
body_filter_by_lua
documentation: https://github.com/openresty/lua-nginx-module#body_filter_by_lua- Following sample is from this site: https://groups.google.com/d/msg/openresty-en/5PWmL2f70x8/R2DUMsNUAgAJ
http {
proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:60m max_size=1G;
server {
listen 8080;
location /replace-body {
proxy_pass http://127.0.0.1:$server_port/;
proxy_cache cache;
proxy_cache_valid 200 30s;
# Reset the response's content_length, so that Lua can generate a
# body with a different length.
header_filter_by_lua '
ngx.header.content_length = nil
';
body_filter_by_lua '
local ctx = ngx.ctx
if ctx.buffers == nil then
ctx.buffers = {}
ctx.nbuffers = 0
end
local data = ngx.arg[1]
local eof = ngx.arg[2]
local next_idx = ctx.nbuffers + 1
if not eof then
if data then
ctx.buffers[next_idx] = data
ctx.nbuffers = next_idx
-- Send nothing to the client yet.
ngx.arg[1] = nil
end
return
elseif data then
ctx.buffers[next_idx] = data
ctx.nbuffers = next_idx
end
-- Yes, we have read the full body.
-- Make sure it is stored in our buffer.
assert(ctx.buffers)
assert(ctx.nbuffers ~= 0, "buffer must not be empty")
-- And send a new body
ngx.arg[1] = "Cool... " .. table.concat(ngx.ctx.buffers)
';
}
}
}
To try it out:
$ curl localhost:8080/replace-body
Cool... <html><head><title>It works!</title></head><body>It
works!</body></html>
$ curl localhost:8080/replace-body
Cool... <html><head><title>It works!</title></head><body>It
works!</body></html>
$ curl localhost:8080/replace-body
Cool... <html><head><title>It works!</title></head><body>It
works!</body></html>