Hello!
On Wed, Feb 17, 2016 at 11:33 AM, RJoshi wrote:
> When nginx rejects or delays request due to rate limiting, is there any way
> to get notified in lua handler?
>
> Requirement is to send notification to the admin when requests are
> delayed/rejected. One of the way is to parse logs but would be easier to
> send email through lua handler.
>
You can write an nginx module yourself that exposes a Lua API
function, say, delayed(), in a way similar to ngx_lua_upstream_module
[1], and then check it in your body_filter_by_lua* handler, as in
body_filter_by_lua_block {
local module = require "my.module"
local ctx = ngx.ctx
if not ctx.delayed then
ctx.delayed = module.delayed()
end
}
And then in log_by_lua*, you can check ctx.delayed and take actions as
needed. Sending emails can be done via some lua-resty-smtp library out
there, but be careful with frequent email sending that could bring
your SMTP server down (or triggering frequency control in your SMTP
server).
On the C land, you can just implement the delayed() function as a
simple condition:
return r->connection->write->delayed ? 1 : 0
Alternatively, you can use dynamic tracing tools like systemtap to
sample your online traffic, which is more efficient and much cleaner
(you pay nothing when you are not sampling, for example).
Regards,
-agentzh
[1] https://github.com/openresty/lua-upstream-nginx-module