您好,登录后才能下订单哦!
# Prometheus+Grafana如何监控Nginx
## 前言
在现代IT基础设施中,监控已成为确保服务可靠性的关键环节。Nginx作为广泛使用的Web服务器和反向代理,其性能指标监控尤为重要。本文将详细介绍如何利用Prometheus和Grafana搭建完整的Nginx监控方案,涵盖从数据采集、存储到可视化的全流程。
---
## 一、技术栈简介
### 1.1 Prometheus
Prometheus是一款开源的系统监控和告警工具,具有以下核心特性:
- 多维数据模型(时间序列由metric名称和键值对标识)
- 灵活的查询语言PromQL
- 不依赖分布式存储,单个服务器节点可直接工作
- 通过HTTP拉取(pull)方式收集时间序列数据
- 支持推送(push)时间序列数据通过中间网关
- 通过服务发现或静态配置发现目标
- 多种图形和仪表板支持模式
### 1.2 Grafana
Grafana是一个开源的度量分析与可视化套件,主要特性包括:
- 丰富的数据源支持(Prometheus、InfluxDB、Graphite等)
- 强大的仪表板编辑功能
- 灵活的通知和告警配置
- 精美的可视化效果
### 1.3 Nginx监控需求
典型的Nginx监控指标包括:
- 请求处理数(requests)
- 连接数(connections)
- 请求处理时间(request_time)
- 各状态码数量(status codes)
- 流量统计(traffic)
---
## 二、环境准备
### 2.1 基础环境
- Linux服务器(本文以Ubuntu 20.04为例)
- Nginx已安装并运行
- Docker环境(可选,用于容器化部署)
### 2.2 组件版本
- Prometheus v2.30+
- Grafana v8.0+
- nginx-exporter v0.10+
- Nginx with stub_status模块
---
## 三、Nginx指标暴露配置
### 3.1 启用Nginx stub_status模块
1. 检查Nginx是否已编译with-http_stub_status_module:
```bash
nginx -V 2>&1 | grep -o with-http_stub_status_module
若未启用,需重新编译Nginx或安装包含该模块的版本
配置Nginx.conf添加监控端点:
server {
listen 8080;
server_name localhost;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
curl http://localhost:8080/nginx_status
应返回类似:
Active connections: 3
server accepts handled requests
10 10 20
Reading: 0 Writing: 1 Waiting: 2
对于更详细的指标,可使用第三方模块: 1. VTS模块(nginx-module-vts):
http {
vhost_traffic_status_zone;
server {
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}
wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*
docker run -d -p 9090:9090 --name prometheus prom/prometheus
docker run -d -p 9113:9113 --name nginx-exporter nginx/nginx-prometheus-exporter -nginx.scrape-uri=http://<nginx-host>:8080/nginx_status
scrape_configs:
- job_name: 'nginx'
static_configs:
- targets: ['nginx-exporter:9113']
metrics_path: /metrics
访问Prometheus UI(http://localhost:9090),执行查询:
nginx_connections_active
wget https://dl.grafana.com/oss/release/grafana-8.2.1.linux-amd64.tar.gz
tar -zxvf grafana-8.2.1.linux-amd64.tar.gz
cd grafana-8.2.1
./bin/grafana-server
docker run -d -p 3000:3000 --name grafana grafana/grafana
推荐使用官方仪表板: 1. 仪表板ID:12708(基础版)或7362(VTS版) 2. 通过”+” → Import → 输入ID自动加载
创建包含关键指标的仪表板:
sum(rate(nginx_http_requests_total[1m])) by (host)
可视化类型:Time series 单位:requests/second
nginx_connections_active
可视化类型:Gauge
sum(rate(nginx_http_requests_total{status=~"2.."}[1m])) by (status)
可视化类型:Pie chart
在Prometheus中添加alert.rules:
groups:
- name: nginx-alerts
rules:
- alert: HighErrorRate
expr: rate(nginx_http_requests_total{status=~"5.."}[1m]) / rate(nginx_http_requests_total[1m]) > 0.05
for: 5m
labels:
severity: critical
annotations:
summary: "High error rate on {{ $labels.instance }}"
remote_write:
- url: "http://influxdb:8086/api/v1/prom/write"
# grafana.ini
[server]
protocol = https
cert_file = /path/to/cert.pem
cert_key = /path/to/key.pem
docker run -d \
-e GF_SECURITY_ADMIN_PASSWORD=secret \
grafana/grafana
docker logs nginx-exporter
curl http://exporter:9113/metrics
通过本文介绍的Prometheus+Grafana监控方案,您可以获得: - 实时可视化的Nginx性能指标 - 历史趋势分析能力 - 及时的异常告警通知 - 可扩展的监控架构
建议定期审查监控指标,根据业务需求调整仪表板和告警阈值,使监控系统持续发挥最大价值。
注:本文所有配置示例已在GitHub存档,访问 github.com/example/nginx-monitoring 获取完整代码。 “`
这篇文章共计约2850字,采用Markdown格式编写,包含: 1. 8个主要章节 2. 15+个配置代码块 3. 技术原理说明+实操步骤 4. 故障排查指南 5. 扩展阅读建议
可根据实际环境调整具体参数,建议在测试环境验证后再应用于生产环境。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。