# Prometheus 监控 NGINX

## Prometheus 监控 NGINX

```bash
yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ -y
mkdir /usr/local/nginx/
wget https://github.com/vozlt/nginx-module-vts/archive/refs/tags/v0.2.1.tar.gz
tar fx v0.2.1.tar.gz -C /usr/local/nginx/
```

```bash
wget -c http://nginx.org/download/nginx-1.21.1.tar.gz
tar fx nginx-1.21.1.tar.gz 
cd nginx-1.21.1
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module  --add-module=/usr/local/nginx/nginx-module-vts-0.2.1
make && make install    # 如果源码添加插件，不用 make install
```

```bash
# vi /usr/local/nginx/conf/nginx.conf

http {

    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;

    server {
          listen 80; 
          vhost_traffic_status off;
          vhost_traffic_status_filter_by_set_key $uri uri::$server_name;                       #每个uri访问量
          #vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;    #不同国家/区域请求量
          vhost_traffic_status_filter_by_set_key $status $server_name;                         #http code统计
          vhost_traffic_status_filter_by_set_key $upstream_addr upstream::backend;             #后端转发统计
          vhost_traffic_status_filter_by_set_key $remote_port client::ports::$server_name;     #请求端口统计
          vhost_traffic_status_filter_by_set_key $remote_addr client::addr::$server_name;      #请求IP统计

          location /status {
                  vhost_traffic_status_display;
                  vhost_traffic_status_display_format html;

          }
    }
}
```

```bash
useradd -s /sbin/nologin www
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx
```

### 下载部署

* 项目地址：https://github.com/hnlq715/nginx-vts-exporter/tree/v0.10.3
* 在需要被监控的Nginx服务器上，下载并上传Nginx exporter

wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz

tar -zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz -C /usr/local/

cd /usr/local/ mv nginx-vts-exporter-0.10.3.linux-amd64 nginx-vts-exporter

### 启动服务

```bash
vim /usr/lib/systemd/system/nginx-vts-exporter.service
[Unit]
Description=prometheus_nginx_vts
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/nginx-vts-exporter/nginx-vts-exporter  -nginx.scrape_uri http://192.168.66.68:30080/status/format/json
Restart=on-failure

[Install]
WantedBy=multi-user.target


systemctl daemon-reload
systemctl enable  nginx-vts-exporter.service
systemctl start  nginx-vts-exporter.service
systemctl status  nginx-vts-exporter.service
```

### 查看

> 输入IP+端口9913后面跟/metrics

### 配置Prometheus

```bash
#添加Nginx-vts-export相关监控配置
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
  ## Nginx监控
  - job_name: 'nginx'
    scrape_interval: 30s
    static_configs:
      - targets: ['xx.xx.xx.xx:9913']
    relabel_configs:
      - source_labels: [__address__]
        regex: '(.*)\:9913'
        target_label: 'hostname'
        replacement: '$1'
      - source_labels: [__address__]
        regex: '(.*)\:9913'
        target_label: 'nodeip'
        replacement: '$1'


# 检查配置是否正确
[root@prometheus ~]# promtool check config /usr/local/prometheus/prometheus.yml

# 重载prometheus配置
[root@prometheus ~]# curl -X POST http://127.0.0.1:9090/-/reload
```

### Grafana 模板

2949
