lua-nginx-module icon indicating copy to clipboard operation
lua-nginx-module copied to clipboard

Logging Subrequests body / uri using access_log

Open psykro opened this issue 6 years ago • 2 comments

Versions: lua-nginx-module: 0.10.15 nginx core: 1.16.0

Hello,

First of all congrats for this really nice module!

I'm trying to use the lua-module to generate a log file containing the full requests and responses with the body and headers.

I currently have the hereafter script, which basically set variables, those variables are afterward used in a nginx log_format directive and printed by access_log (taken from there https://www.hardill.me.uk/wordpress/2018/03/14/logging-requests-and-response-with-nginx/):

lua_need_request_body on; 
set $resp_body "";
body_filter_by_lua_block {
  local resp_body = string.sub(ngx.arg[1], 1, 1000)
  ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
  if ngx.arg[2] then
     ngx.var.resp_body = ngx.ctx.buffered
  end
}
set $req_header "";
set $resp_header "";
header_filter_by_lua_block {
  local h = ngx.req.get_headers()
  for k, v in pairs(h) do
      ngx.var.req_header =  ngx.var.req_header .. k .. ": " .. v .. "\n"
  end
  local rh = ngx.resp.get_headers()
  for k, v in pairs(rh) do
      ngx.var.resp_header = ngx.var.resp_header .. k .. ": " .. v .. "\n"
  end
}

This is working, however now i'd like to execute this script from a location which is called by a subrequest. We are not using lua for our subrequests, we have made our own module (which launches multiple subrequests and works fine). The problem i'm facing is that the above script will print the body of the response sent to the client and not the one from the subrequest. I'm sure this lua script is called because multiple subrequests are made to this location and the header variables are concatenated each time (this is not what i want but it allowed me to see that the script is getting called for each subrequest).

Here is the nginx location where i define this script:

location ~/mylocation/([^/]*)/(.*) {
   internal;
   proxy_set_header Host $1;
   proxy_pass http://$1/$2$is_args$args;
   include log_requests.lua;
}

This is to be used only for debugging in case of emergency therefore i do not care of the performance of the script.

Thanks for your help !

Maël

psykro avatar Jul 24 '19 09:07 psykro

FYI, i fixed my problem by sending my subrequest to another proxy which has the lua script on it. This proxy sends the request to the original upstream and logs request/response

psykro avatar Jul 30 '19 14:07 psykro

We would like to do the same thing, without the extra proxy :1st_place_medal: Is it possible?

maxramqvist avatar Mar 21 '22 14:03 maxramqvist