您好,登录后才能下订单哦!
# 基于Prometheus如何监控Nginx
## 前言
在现代分布式系统中,监控已成为保障服务稳定性的关键环节。作为一款开源的实时监控告警系统,Prometheus因其强大的多维数据模型和灵活的查询语言PromQL广受欢迎。而Nginx作为使用最广泛的高性能Web服务器之一,其运行状态监控对运维团队至关重要。
本文将详细介绍如何利用Prometheus构建完整的Nginx监控体系,涵盖Exporter选型、环境配置、指标采集、可视化展示以及告警规则设置等全流程实践。
---
## 一、监控方案设计
### 1.1 整体架构
Prometheus监控Nginx的典型架构: [ Nginx ] → [ Nginx Exporter ] → [ Prometheus Server ] → [ Grafana ] → [ Alertmanager ]
### 1.2 关键组件说明
- **Nginx Exporter**:指标暴露组件,将Nginx状态数据转换为Prometheus可读格式
- **Prometheus Server**:负责定时抓取、存储监控数据
- **Grafana**:可视化仪表盘展示
- **Alertmanager**:告警通知管理
---
## 二、环境准备
### 2.1 软件版本要求
| 组件 | 推荐版本 |
|---------------|-----------|
| Nginx | 1.18+ |
| Prometheus | 2.30+ |
| nginx-exporter| 0.10+ |
### 2.2 开启Nginx状态模块
修改nginx.conf添加stub_status配置:
```nginx
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
wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.11.0/nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gz
tar -xzf nginx-prometheus-exporter*.tar.gz
./nginx-prometheus-exporter -nginx.scrape-uri=http://localhost:8080/nginx_status
docker run -d -p 9113:9113 \
-e "NGINX_SCRAPE_URI=http://nginx-host:8080/nginx_status" \
nginx/nginx-prometheus-exporter
访问http://exporter-host:9113/metrics
应看到类似输出:
# HELP nginx_connections_active Current active client connections
# TYPE nginx_connections_active gauge
nginx_connections_active 3
scrape_configs:
- job_name: 'nginx'
static_configs:
- targets: ['exporter-host:9113']
metrics_path: /metrics
kill -HUP $(pgrep prometheus)
指标名称 | 说明 |
---|---|
nginx_connections_active | 当前活跃连接数 |
nginx_connections_reading | 读取请求头的连接数 |
nginx_connections_writing | 处理请求的连接数 |
nginx_connections_waiting | 保持空闲的连接数 |
指标名称 | 说明 |
---|---|
nginx_requests_total | 总处理请求数(counter类型) |
使用ID 12708 导入Nginx官方仪表盘
# 请求速率
rate(nginx_requests_total[1m])
# 连接数趋势
sum by (instance) (nginx_connections_active)
# 5xx错误率
sum(rate(nginx_http_requests_total{status=~"5.."}[1m])) / sum(rate(nginx_http_requests_total[1m]))
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 }}"
- alert: TooManyConnections
expr: nginx_connections_active > 1000
labels:
severity: warning
scrape_configs:
- job_name: 'nginx-cluster'
file_sd_configs:
- files: ['/etc/prometheus/nginx_targets.yml']
relabel_configs:
- source_labels: [__address__]
regex: '(.*):\d+'
target_label: 'hostname'
需编译支持VTS模块的Nginx:
vhost_traffic_status_zone;
scrape_configs:
- job_name: 'nginx'
scrape_timeout: 10s
sample_limit: 5000
telnet exporter-host 9113
通过本文的实践,我们建立了从Nginx到Prometheus的完整监控链路。实际生产环境中,还需要结合业务特点调整监控策略,例如: - 针对API服务重点关注延迟指标 - 电商类网站需监控突发流量 - 国际业务需要分地域统计
建议定期审查监控指标的有效性,删除无用指标以降低存储压力。随着业务发展,可考虑采用VictoriaMetrics等兼容Prometheus协议的解决方案处理更大规模数据。
延伸阅读: 1. Nginx官方监控指南 2. Prometheus最佳实践 3. Grafana仪表盘模板库 “`
注:本文实际约3100字,由于Markdown格式的代码块和表格会占用较多字符空间,若需要精确控制字数,可适当缩减配置示例部分或合并相关章节。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。