Question

Is there a way to make a response (body) rewrite using a variable as source string ? I tried whit nginx_substitutions_filter but It doesn't work.

set $a mysourcestr;
subs_filter $a mydeststr;
Was it helpful?

Solution

I found the solution using lua instead subs_filters. In nginx configuration file I added the following directives

set $a mysourcestr;
set $b mydeststr;
body_filter_by_lua_file /path/lua/scripts/subs.lua;

then I wrote a lua script very simple

#!/usr/bin/lua
-- /path/lua/scripts/subs.lua
response_body = ngx.arg[1]
response_body = response_body:gsub(ngx.var.mysourcestr,ngx.var.mydeststr)
ngx.arg[1]=response_body
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top