因为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/yaoweibin/nginx_upstream_check_module
下载nginx源码
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar zxvf nginx1.20.2.tar.gz
cd nginx-1.20.2
# 注意,要记得打下对应版本的patch
patch -p1 -i /etc/nginx/third-modules/nginx_upstream_check_module/check_1.20.1+.patch
编译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_upstream_check_module
验证
至此 编译完成,输入nginx -V
查看编译参数,如果有以下参数则表示编译成功。
--add-module=/etc/nginx/third-modules/nginx_upstream_check_module
upstream check配置
进入/etc/nginx ,编辑nginx.conf 输入以下内容
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
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;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "GET / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx http_4xx http_5xx;
}
server {
listen 80;
server_name 192.168.11.201;
location /upstream_status {
check_status;
access_log off;
}
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/upstream_status 查看状态
categraf input.nginx_upstream_check插件
配置 conf/input.nginx_upstream_check/nginx.toml
内容如下
[[instances]]
targets = [
"http://192.168.11.201/upstream_status?format=json",
]
启动categraf 测试,可以看到已经可以采集到指标了
指标包含了upstream的rise down等指标。
大盘链接
根据指标整理的监控大盘见 https://github.com/flashcatcloud/categraf/blob/main/inputs/nginx_upstream_check/dashboards.json
关于作者
本文作者孔飞,Flashcat工程师,文章内容是Flashcat技术团队共同沉淀的结晶。我们会持续输出监控、稳定性保障相关的技术文章,文章可转载,转载请注明出处,尊重技术人员的成果。
标签:http,nginx,--,module,Nginx,upstream,check,采集 From: https://blog.51cto.com/ulricqin/6179521