2
votes

I have a similar need to https://github.com/openresty/lua-nginx-module/issues/220

My use case

  1. I'm forwarding files to a remote server, through using proxy_pass.
  2. I need to pass $body_bytes_sent to a remote url, after proxy pass.
  3. I thought about doing a content_by_lua block, with an ngx.capture forwarded to the proxy_pass block, with an ngx.say() returning what came from ngx.capture. Followed by a request with $body_bytes_sent to the remote url. But I need to support streaming, which this wouldn't do. And files can get quite big which is bad for ngx.capture().
  4. I thought about doing a log_by_lua block, but cosockets apis are disabled. https://github.com/openresty/lua-nginx-module#log_by_lua
1
Not think this is a bit overkill, we do something similar, i.e. tracking request information, however, you could use beats to trawl this information and define the log file in NGINX. Maybe post this up to a logstash instance - Nathan Smith
The block that contains the proxy_pass, has an access_by_lua block that queries a microservice. In our setup, the microservice is fed data from log_by_lua, which lets it determine if a particular url should be accessible or not. ---- The remote server inside proxy_pass is out of our control. So we couldn't have done it there. --- Logstash was given some thought. But I thought it would have complicated things, since I would have needed to send a copy of the logs to the microservice. - galeaspablo
Do you found "something that doesn't block the nginx worker" for sending data over http? - Sergey Di
Unfortunately no. Still have the blocking operation. - galeaspablo

1 Answers

0
votes

Install Lua-Curl or another library that does not depend on cosockets. (https://github.com/Lua-cURL/Lua-cURLv3)

If you're using luarocks (comes with openresty), install with

apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev --yes

luarocks install Lua-cURL

Use log_by_lua (e.g. log_by_lua_block, log_by_lua_file), like this.

# some nginx conf

location /a_location_with_proxy_pass {
    proxy_pass https://example.com:443;
    log_by_lua_file /path/to/luafile.lua;
}

Your log_by_lua_file should then do the curl request to the remote server.

local cURL = require 'curl'

curlHandle = cURL.easy{
    url        = 'https://remote_host.com/endpoint',
    post       = true,
    httpheader = {
        'Content-Type: application/json';
    },
    postfields = '{"bytes":' .. ngx.var.body_bytes_sent .. '}'
};
curlHandle:perform();