openresty / openresty/lua-nginx-module

Logging Subrequests body / uri using access_log

Open
#1,568 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the included log_requests.lua configuration and the shown body_filter_by_lua_block and header_filter_by_lua_block handlers, then trace how they are invoked for the internal location's subrequests and how access_log receives their variables. Done means determining the supported behavior for logging each subrequest's body and URI separately.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx
Domain
backend, observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.