# Prometheus+Grafana如何監控Nginx
## 前言
在現代IT基礎設施中,監控已成為確保服務可靠性的關鍵環節。Nginx作為廣泛使用的Web服務器和反向代理,其性能指標監控尤為重要。本文將詳細介紹如何利用Prometheus和Grafana搭建完整的Nginx監控方案,涵蓋從數據采集、存儲到可視化的全流程。
---
## 一、技術棧簡介
### 1.1 Prometheus
Prometheus是一款開源的系統監控和告警工具,具有以下核心特性:
- 多維數據模型(時間序列由metric名稱和鍵值對標識)
- 靈活的查詢語言PromQL
- 不依賴分布式存儲,單個服務器節點可直接工作
- 通過HTTP拉?。╬ull)方式收集時間序列數據
- 支持推送(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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。