监控nginx
## 一、prometheus监控nginx
docker安装(略)
docker-compose安装(略)
## 1.1待监控Linux安装nginx
创建nginx目录
mkdir /data/nginx -p
cd /data/nginx
创建docker-compose.yaml
vim docker-compose.yaml
配置如下:
services:
nginx:
image: registry.cn-hangzhou.aliyuncs.com/ldw520/nginx:latest
container_name: nginx
restart: always
volumes:
- /data/nginx/conf.d:/etc/nginx/conf.d
- /data/nginx/html:/usr/share/nginx/html
- /data/nginx/log:/var/log/nginx
ports:
- 80:80
启动 docker-compose.yaml
docker-compose up -d
查看docker的信息
docker images
docker ps -a
创建nginx配置文件
cd /data/nginx/conf.d
cat >>server.conf<< "EOF"
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
EOF
重启nginx
cd /data/nginx
docker-compose down
docker-compose up -d
创建默认index.html
vi /data/nginx/html/index.html

访问地址
http://192.168.100.141/index

<hr>
## 1.2 nginx开启监控
centos2机器上,编辑docker-compose.yaml文件
docker exec -it nginx nginx -V 2>&1 | grep --color -o with-http_stub_status_module
* **如果显示了`with-http_stub_status_module`**:这意味着`http_stub_status_module`已经启用,你可以继续配置 Nginx 以使用此模块。
* **如果没有显示**:这意味着在编译 Nginx 时没有包含`http_stub_status_module`。在这种情况下,你通常需要重新编译 Nginx,或者使用一个已经包含该模块的 Nginx 版本。对于 Docker 容器,你可能需要:
* 查找一个已经包含此模块的官方或第三方 Docker 镜像。
* 从源代码自行编译 Nginx 并创建一个新的 Docker 镜像。
##### 配置 Nginx 使用`stub_status`
一旦确认`http_stub_status_module`已启用,你可以在 Nginx 配置文件中添加`stub_status`配置。通常,你会在`http`块中添加一个`location`块,如下所示:
vi /data/nginx/conf.d/server.conf
server {
listen 80;
server_name localhost;
location /nginx_status {
stub_status on;
access_log off;
allow 0.0.0.0/0; # 允许访问的IP地址,可以根据需要修改
deny all;
}
}
完成配置后,不要忘记重新加载或重启 Nginx 以应用更改。
docker exec -it nginx nginx -s reload
访问 http://192.168.100.141/stub_status

<hr>
## 二、prometheus监控mysql
## 2.1docker安装(略)
## 2.2docker-compose安装(略)
## 2.3 安装nginx_exporter
centos2机器上,编辑docker-compose.yaml文件
vi docker-compose.yaml
如图所示:加入一个nginx_exporter配置

nginx-exporter:
image: nginx/nginx-prometheus-exporter:0.11
container_name: nginx_exporter
hostname: nginx_exporter
command:
- '-nginx.scrape-uri=http://192.168.100.141/stub_status'
restart: always
ports:
- "9113:9113"
运行docker-compose up-d的命令,观察mysql的程序是否成功运行
宿主机上访问mysqld_exporter的metrics
http://192.168.100.141:9113/metrics
<hr>
## 2.4 prometheus服务器添加nginx_exporter的地址
192.168.100.85的centos上,修改prometheus的配置文件
#进入docker-prometheus目录
cd /data/docker-prometheus
#修改prometheus.yml
vi prometheus/prometheus.yml
添加nginx_exporter配置如下图:

- job_name: "nginx-exporter"
static_configs:
- targets: ["192.168.100.141:9113"]
labels:
istance: "centos2服务器nginx监听"
保存后输入命令更新:
curl -XPOST http://localhost:9090/-/reload
成功监听

<hr/>
## 1.4 mysql服务器指标查询
nginx_connections_accepted #接收请求数
nginx_connections_active #活动连接数
nginx_connections_handled #成功处理请求数
nginx _connections_reding #正在进行读操作的请求数
nginx connections_waiting #正在等待的请求数
nginx_connections_writing #正在进行写操作的请求数
nginx connections_requests #总请求数
<hr>
2.6 grafana中对mysql进行监控
copy id to clipboard->grafana的dashboards中lmport dashboard
https://grafana.com/grafana/dashboards/12708-nginx/


- Prometheus教程
- 一. dokcer 安装
- 二. 安装docker-compose
- 三. docker-compose安装Prometheus
- 四. 配置grafana的数据源
- 五. Prometheus的Exporter
- 六. Prometheus的基本术语
- 七. 监控Linux
- 八. 监控redis和mongodb
- 九. 监控mysql数据库
- 十. 监控go程序
- 十一. 监控nginx
- 十二. 监控消息队列
- 十三. 监控docker
- 十四. 监控进程
- 十五. 域名监控
- 十六. SNMP监控
- 十七. 黑盒监控
- 十八. 自定义监控
- 十九. go实现自定义监控
- 二十. 服务发现概述
- 二十一. 基于文件的服务发现
- 二十二. 基于Consul的服务发现
- 二十三. relabeling机制