nginx服务资源监控

依赖

一个内置的with-http_stub_status_module模块

配置

启用的时候只需要配置server块当中加上一小段访问url即可获取到

location /status {
 stub_status on; 
}

docker实现

目前采用nginx:alpine作为镜像,实现访问http://127.0.0.1/status获取到服务状态信息

由于需要修改/etc/nginx/conf.d/default.conf文件,所以我们可以选择挂载一个文件或者重新构建一个镜像

下面我们创建一个Dockerfile,该文件主要是在default.conf文件使用sed查找server_name localhost;并且在后面加入一段location

FROM nginx:alpine
RUN sed -i '/server_name localhost;/a\ location /status {\n stub_status on;\n }' /etc/nginx/conf.d/default.conf

创建compose.yaml文件如下

services:
 nginx:
 container_name: demo-nginx
 image: demo-nginx
 build: .
 restart: always
 ports:
 - "80:80"

开始构建镜像

$ docker compose build

启动容器

$ docker compose up -d

查看容器内部nginx配置确认location /status块存在

$ docker exec demo-nginx cat /etc/nginx/conf.d/default.conf
server {
 listen 80;
 server_name localhost;
 location /status {
 stub_status on;
 }
 #access_log /var/log/nginx/host.access.log main;
 
 location / {
 root /usr/share/nginx/html;
 index index.html index.htm;
 }
....

结果验证

浏览器访问地址http://127.0.0.1/status获取返回数据如下

Active connections: 2 
server accepts handled requests
 2 2 4 
Reading: 0 Writing: 1 Waiting: 1 

字段含义

  • Active connections: 活跃连接数
  • server

    • accepts: 接收请求
    • handled: 已处理请求
    • requests: 客户端请求
  • Reading: 当前所有连接中正在读取请求Header的数量
  • Writing: 当前所有连接中正在返回数据的数量
  • Waiting: 当前等待请求的空闲客户端连接数

参考阅读

Module ngx_http_stub_status_module

作者:龚正阳原文地址:https://segmentfault.com/a/1190000043560155

%s 个评论

要回复文章请先登录注册