# 基于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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。