跳转至

statsd: NGINX 模块用于将统计信息发送到 statsd

安装

您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:

  • RedHat Enterprise Linux 7、8、9 和 10
  • CentOS 7、8、9
  • AlmaLinux 8、9
  • Rocky Linux 8、9
  • Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-statsd
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-statsd

通过在 /etc/nginx/nginx.conf 的顶部添加以下内容来启用该模块:

load_module modules/ngx_http_statsd_module.so;

本文档描述了 nginx-module-statsd v0.0.1,于 2020 年 2 月 24 日发布。


一个用于将统计信息发送到 statsd 的 nginx 模块。

以下是如何使用 nginx-statsd 模块:

http {

    # 设置您要发送统计信息的服务器。
    statsd_server your.statsd.server.com;

    # 随机抽样 10% 的请求,以免压垮您的 statsd 服务器。
    # 默认发送所有 statsd(100%)。
    statsd_sample_rate 10; # 10% 的请求


    server {
        listen 80;
        server_name www.your.domain.com;

        # 每当有请求命中此服务器时,将 "your_product.requests" 增加 1。
        statsd_count "your_product.requests" 1;

        location / {

            # 当此位置被访问时,将键增加 1。
            statsd_count "your_product.pages.index_requests" 1;

            # 仅当 $request_completion 设置为某个值时,将键增加 1。
            statsd_count "your_product.pages.index_responses" 1 "$request_completion";

            # 发送一个计时到 "your_product.pages.index_response_time",其值等于
            # 从上游服务器返回的值。如果该值为 0 或空字符串,
            # 则不会发送。因此,无需添加测试。
            statsd_timing "your_product.pages.index_response_time" "$upstream_response_time";

            # 根据自定义头的值增加一个键。仅在
            # 自定义头存在于上游响应中时发送该值。
            statsd_count "your_product.custom_$upstream_http_x_some_custom_header" 1
                "$upstream_http_x_some_custom_header";

            proxy_pass http://some.other.domain.com;
        }
    }
}

GitHub

您可以在 nginx-module-statsd 的 GitHub 仓库 中找到此模块的其他配置提示和文档。