2

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
4
  • 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 Commented Aug 23, 2017 at 13:37
  • 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. Commented Aug 23, 2017 at 16:37
  • Do you found "something that doesn't block the nginx worker" for sending data over http? Commented Aug 12, 2019 at 7:23
  • Unfortunately no. Still have the blocking operation. Commented Aug 12, 2019 at 13:37

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

The bad thing here is that you will BLOCK nginx worker until response recevied.
I'm afraid so. I prefer proxy_pass to ngx.capture - because I'm dealing with files. But this means, I only get $body_bytes_sent available in log_by_lua which does not allow cosockets. I would prefer something that doesn't block the nginx worker, but couldn't find anything else. If you have anything else, even just ideas, would love to hear it! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.