那么深沉 发表于 2024-5-26 05:43:59

prometheus监控nginx并实现可视化的操作指南

一、概述

Nginx是一款高性能的Web服务器,被广泛应用于各类的网站和应用程序中。为了保证Nginx的正常工作,我们需要对其进行监控和管理。
1.1 prometheus 监控nginx两种方式

prometheus 监控nginx有两种方式。

[*]一种是通过nginx_exporter监控,需要开启nginx_stub_status,主要是nginx自身的status信息,metrics数据现对较少;
[*]另外一种使用nginx-vts-exporter监控,但是需要在编译nginx的时候添加nginx-module-vts模块,监控数据较多,提供了包含server、upstream以及cache的相关监控指标,指标更丰富,所以如下采用nginx-vts-exporter实现对nginx的监控。

1.2 版本信息

如下第一种方式:nginx-vts-exporter
版本
地址
nginx
1.25.4
http://nginx.org/download/nginx-1.25.4.tar.gz
vts
0.2.2
Releases · vozlt/nginx-module-vts · GitHub
nginx-vts-exporter
0.10.3
Releases · sysulq/nginx-vts-exporter · GitHub

二、 安装nginx-module-vts


2.1 下载编译

# 下载编译环境
yum -y install gcc gcc-c++ autoconf automake make pcre pcre-devel zlib-devel lrzsztreevimwgetnet-tools openssl openssl-devel

# 下载
wget https://github.com/vozlt/nginx-module-vts/archive/refs/tags/v0.2.2.zip
wget http://nginx.org/download/nginx-1.25.4.tar.gz
tar -zxvf nginx-1.25.4.tar.gz
# 进入编译的安装目录   
cd /home/nginx-1.25.4
# 下载解压nginx-module-vts 预编译 需要新增--add-module=
./configure--prefix=/usr/local/nginx\
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre --with-file-aio \
--with-http_realip_module \
--add-module=/opt/nginx-module-vts-0.2.2

# 编译(这里只make,不要make install ,不然会覆盖,如果新装的可以继续make install)
make && make install配置nginx.conf
vim /usr/local/nginx/conf/nginx.conf

    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;

    server {
      listen       80;
      server_namelocalhost;
      
      location /status {
         vhost_traffic_status_display;
         vhost_traffic_status_display_format html;
      }

    }

# 启动nginx,并指定配置文件为/app/nginx/conf/nginx.conf
cd/usr/local/nginx/sbin/

# 测试nginx配置文件是否正确
./nginx -t
#        启动
./nginx登录验证: http://192.168.2.140/status


2.2 监控字段总结

Server main 主服务器

Server zones服务器区域


三、安装启动nginx-vts-exporter


3.1安装启动nginx-vts-exporter

wget   --no-check-certificate https://github.com/sysulq/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64

tar -zxvf nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz
chmod +x /usr/local/nginx_exporter/bin/nginx-vtx-exporter -R
3.2设置systemctl启动

vim /usr/lib/systemd/system/nginx-exporter.service


Description=nginx_vts_exporter
After=network.target


Type=simple
ExecStart=/opt/nginx-exporter/nginx-prometheus-exporter -nginx.scrape_uri http://192.168.2.140/status/format/json
Restart=on-failure


WantedBy=multi-user.target启动nginx_exporter
systemctl enable nginx_exporter
systemctl start nginx_exporter
systemctl status nginx_exporter

3.3修改prometheus配置文件并重启

- job_name: 'nginx'
static_configs:
- targets: ['192.168.1.1:9913']
    labels:
      instance: nginxhttp://192.168.2.139:30242/targets


3.4grafana导入nginx-vts-exporter监控

模板连接:Nginx VTS Stats | Grafana Labs


nginx常用命令
nginx -s reopen #重启Nginx

nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx

nginx -s stop #强制停止Nginx服务

nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)

nginx -t #检测配置文件是否有语法错误,然后退出

nginx -?,-h #打开帮助信息

nginx -v #显示版本信息并退出

nginx -V #显示版本和配置选项信息,然后退出

nginx -t #检测配置文件是否有语法错误,然后退出

nginx -T #检测配置文件是否有语法错误,转储并退出

nginx -q #在检测配置文件期间屏蔽非错误信息

nginx -p prefix #设置前缀路径(默认是:/usr/share/nginx/)

nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)

nginx -g directives #设置配置文件外的全局指令

killall nginx #杀死所有nginx进程以上就是prometheus监控nginx并实现可视化的操作指南的详细内容,更多关于prometheus监控nginx并可视化的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/server/320957rbd.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: prometheus监控nginx并实现可视化的操作指南