cyberforce_
cyberforce_

Reputation: 21

Nginx $connections_active per server block

Is it possible to have the $connections_active from the stub nginx per server block? That way i can know how many requests per second each website is doing. This only does for global connections accross the entire proxy. If not how can i do it?

I'm using openresty and lua programming.

Upvotes: 2

Views: 354

Answers (1)

Pascal Pixel Rigaux
Pascal Pixel Rigaux

Reputation: 702

Here is how you can do it with nginx&lua:

lua_shared_dict live_hosts 1M;

rewrite_by_lua_block {
  local dict = ngx.shared.live_hosts;
  if (not ngx.var.http_counted) then
     ngx.req.set_header("counted", "true");
     dict:incr(ngx.var.host, 1, 0);
  end
}

log_by_lua_block {
  local dict = ngx.shared.live_hosts;
  dict:incr(ngx.var.host, -1);
}

You can then access ngx.shared.live_hosts:get('foo.com')

NB: the counted added header is necessary to handle nginx internal redirect: in that case rewrite_by_lua is called twice whereas log_by_lua is called only once. ngx.ctx can not be used (it is cleared in case of internal redirect)

Upvotes: 1

Related Questions