因为nginx无论是开启模块还是添加模块都需要重新编译, 我们首先做一些准备工作。

测试环境

  • 操作系统: CentOS 7.2

安装依赖

这些依赖是我这个环境下的, 你可以根据自己的环境安装对应的依赖。

yum install -y patch
yum install -y gd gd-devel
yum install -y libxslt-devel
yum install perl-ExtUtils-Embed

第三方依赖

下载第三方依赖,并解压到目录/etc/nginx/third-modules/下。 https://github.com/vozlt/nginx-module-vts

下载nginx源码

wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar zxvf nginx1.20.2.tar.gz
cd nginx-1.20.2

编译nginx

感谢@Jeff的细心整理。

./configure \
--with-ld-opt="-Wl,-rpath,/usr/local/lib" \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/var/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-compat \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-select_module \
--with-poll_module \
--with-file-aio \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_perl_module=dynamic \
--with-stream=dynamic \
--with-mail=dynamic \
--with-http_xslt_module=dynamic \
--add-module=/etc/nginx/third-modules/nginx-module-vts

验证

至此 编译完成,输入nginx -V查看编译参数,如果有以下参数则表示编译成功。

--add-module=/etc/nginx/third-modules/nginx-module-vts

vts 配置

进入/etc/nginx ,编辑nginx.conf 输入以下内容

user  nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
	vhost_traffic_status_zone;
    vhost_traffic_status_filter on;
    vhost_traffic_status_filter_by_set_key $status $server_name;

    sendfile        on;

    keepalive_timeout  65;
    include conf.d/*.conf;
}

创建conf.d 目录, 编辑一个test.conf 输入以下内容

upstream test {
    server 127.0.0.1:18788 weight=10 max_fails=2 fail_timeout=5s;
}

server {
        listen 80;
        server_name 192.168.11.201;

		location /vts_status {
			vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }

		location / {
                        add_header Content-Type text/plain;
                        return 200 "User-agent: *\nDisallow: /\n";
		}

		location /test {
            proxy_pass_header Authorization;
            proxy_pass_header Accept;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Scheme $scheme;
            proxy_set_header Authorization $http_authorization;
            proxy_set_header Accept $http_accept;
            proxy_pass http://test;
		}
}

启动nginx

nginx -t # 检查配置文件
  nginx #启动nginx

访问 http://192.168.11.201/vts_status 查看状态 

nginx vts 监控数据采集演示_vts

categraf input.prometheus插件

配置 conf/input.prometheus/nginx.toml 内容如下

[[instances]]
urls = [
  "http://192.168.11.201/vts_status/format/prometheus",
]

url_label_key = "instance"
url_label_value = "{{.Host}}"

启动categraf 测试,可以看到已经可以采集到指标了 

nginx vts 监控数据采集演示_vts_02

vts指标比较全面,推荐使用vts方式采集。

大盘链接

根据指标整理的监控大盘见 https://github.com/flashcatcloud/categraf/blob/main/inputs/nginx_vts/dashboards.json

关于作者

本文作者孔飞,Flashcat工程师,文章内容是Flashcat技术团队共同沉淀的结晶。我们会持续输出监控、稳定性保障相关的技术文章,文章可转载,转载请注明出处,尊重技术人员的成果。