openresty nginx search for keyword in post request body data
I have been using openresty nginx for simple request forwarding purpose, it is working as expected, i am forwarding each incoming request to another URL using below code ??
location /app/ {
proxy_pass https://example.com/abc/;
proxy_read_timeout 60s;
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Frame-Options "SAMEORIGIN" always;
}
Now my requirement is that before forwarding each request, i want to filter post request body data for specific string/keyword , it should only forwarded to proxy URL https://example.com/abc/ if specific string/keyword is found in post data.
I did following configuration to achieve this ??
` location /wapp/ {
lua_need_request_body on;
set $flag_true 1;
set_by_lua $flag '
local token = ngx.var.arg_token
local request_body = ngx.req.get_body_data()
ngx.req.read_body()
if string.find(request_body, "getMiniAccountStatement") then
return ngx.var.flag_true
end
return "false"
';
if ($flag = 1) {
proxy_pass http://example.com/abc/;
proxy_read_timeout 60s;
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Frame-Options "SAMEORIGIN" always;
more_set_headers 'Server: KCCB';
break;
}
}`
when i am posting request with this configuration i am getting following error ??
failed to run set_by_lua*: set_by_lua:4: API disabled in the context of set_by_lua*
stack traceback: [C]: in function 'read_body' set_by_lua:4: in main chunk, client: ::1, server: somename, request: "POST /wapp/ HTTP/1.1""
can anyone help ? Thanks