【问题标题】:Nginx $connections_active per server blockNginx $connections_active 每个服务器块
【发布时间】:2019-03-22 15:59:50
【问题描述】:

是否可以让每个服务器块的存根 nginx 中的 $connections_active ?这样我就可以知道每个网站每秒有多少请求。这仅适用于跨整个代理的全局连接。如果没有,我该怎么办?

我正在使用 openresty 和 lua 编程。

【问题讨论】:

标签: nginx proxy lua


【解决方案1】:

以下是使用 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不能使用(内部重定向时清除)

【讨论】:

  • 我已经搜索了几个小时的解决方案,我想知道为什么谷歌没有让我来到这里。谢谢顺便说一句!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多