【Prometheus+Grafana】--3 监控Nginx

本文介绍如何在服务器上部署Nginx监控模块,包括nginx-module-vts的安装配置及与Prometheus集成的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

被监控机器环境搭建&配置

  • nginx-module-vts下载地址:
[root@nc-zhangfr1]# https://github.com/vozlt/nginx-module-vts
  • nginx-module-vts安装
[root@nc-zhangfr1]# mv nginx-module-vts-master /usr/local/
  • 编译环境
[root@nc-zhangfr1]# yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  • 安装nginx
[root@nc-zhangfr1]# wget nginx.org/download/nginx-1.15.7.tar.gz


--2021-03-17 14:25:15--  http://nginx.org/download/nginx-1.15.7.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1026732 (1003K) [application/octet-stream]
Saving to: ‘nginx-1.15.7.tar.gz’

100%[==================================================================================================================================================>] 1,026,732    325KB/s   in 3.1s   

2021-03-17 14:25:19 (325 KB/s) - ‘nginx-1.15.7.tar.gz’ saved [1026732/1026732]
  • 解压
[root@nc-zhangfr1]# tar -zxvf nginx-1.15.7.tar.gz
  •  编译安装nginx
[root@nc-zhangfr1 nginx-1.15.7]./configure --prefix=/home/nginx-1.15.7 
[root@nc-zhangfr1 nginx-1.15.7] make && make install
  • 检查与排错

出现下述错误,可以在nginx的路径下创建logs文件夹即可。mkdir logs

[root@nc-zhangfr1 nginx-1.15.7]# sbin/nginx -t

nginx: [alert] could not open error log file: open() "/home/nginx-1.15.7/logs/error.log" failed (2: No such file or directory)
nginx: the configuration file /home/nginx-1.15.7/conf/nginx.conf syntax is ok
2021/03/17 15:26:31 [emerg] 23660#0: open() "/home/nginx-1.15.7/logs/nginx.pid" failed (2: No such file or directory)
nginx: configuration file /home/nginx-1.15.7/conf/nginx.conf test failed
  • 编译nginx-module-vts模块
[root@nc-zhangfr1 nginx-1.15.7]# ./configure --prefix=/home/nginx-1.15.7 --add-module=/usr/local/nginx-module-vts/ 
[root@nc-zhangfr1 nginx-1.15.7]# make && make install
  • 修改nginx配置文件

增加如下内容

http {
    ...  #默认配置
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;  #根据host分别进行流量统计
    ...  #默认配置    

    server {
        ...  #默认配置
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
        ...  #默认配置
    }
}
  • 启动nginx
[root@nc-zhangfr1]# /home/nginx-1.15.7/sbin/nginx

  • 查看nginx监控模块

  • 安装nginx-vts-exporter

[root@nc-zhangfr1]# wget -c https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.9.1/nginx-vts-exporter-0.9.1.linux-amd64.tar.gz

[root@nc-zhangfr1]# tar -zxvf nginx-vts-exporter-0.9.1.linux-amd64.tar.g
  • 启动nginx-vts-exporter
[root@nc-zhangfr1 nginx-vts-exporter-0.9.1.linux-amd64]# nohup ./nginx-vts-exporter -nginx.scrape_uri http://IP/status/format/json &

  • 修改prometheus配置文件
  - job_name: 'nginx'
    static_configs:
      - targets: ['IP:9913']
        labels:
          instance: web1

重启prometheus。

  • Grafana导入模版

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值