Nginx 每个 server 块中的 $connections_active

是否有可能从 stub nginx 中获得每个 server 块的 $connections_active?这样我就可以知道每个网站每秒钟有多少请求数。这仅适用于整个代理服务器上的全局连接。 如果不行,我该怎么做?

我正在使用 openresty 和 lua 编程。

点赞
用户3005203
用户3005203

以下是如何使用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);
}

然后,您可以访问ngx.shared.live_hosts:get('foo.com')

注意:添加的counted头是必要的,以处理nginx内部重定向:在这种情况下,rewrite_by_lua会被调用两次,而log_by_lua只会被调用一次。 ngx.ctx无法使用(在内部重定向的情况下,它会被清除)。

2019-01-12 17:36:10