Skip to content

依赖

一个内置的with-http_stub_status_module模块

配置

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

location /status {
	stub_status on; 
}
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

dockerfile
FROM nginx:alpine

RUN sed -i '/server_name  localhost;/a\    location /status {\n        stub_status on;\n    }' /etc/nginx/conf.d/default.conf
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文件如下

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

开始构建镜像

shell
$ docker compose build
$ docker compose build

启动容器

shell
$ docker compose up -d
$ docker compose up -d

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

shell
$ 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;
    }
....
$ 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: 2 
server accepts handled requests
 2 2 4 
Reading: 0 Writing: 1 Waiting: 1

字段含义

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

配置syslog

nginx支持把日志access_log/error_log通过syslog的方式发送出去

首先需要确保机器有syslog服务

可以通过执行命令查看

shell
$ systemctl status rsyslog
$ systemctl status rsyslog

查看docker0网口的IP,该IP会作为容器内部访问宿主机的IP,在linux系统中,该IP一般会是172.17.0.1

shell
$ ip addr show docker0
15: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:81:36:cb:3c brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:81ff:fe36:cb3c/64 scope link 
       valid_lft forever preferred_lft forever
$ ip addr show docker0
15: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:81:36:cb:3c brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:81ff:fe36:cb3c/64 scope link 
       valid_lft forever preferred_lft forever

通过修改/etc/nginx/nginx.conf文件的方式配置远程syslog服务的地址,由于nginx目前处于容器中,所以填上docker0IP表示把syslog日志发送到宿主机的syslog服务

http {
	...
	access_log  /var/log/nginx/access.log  main;
    access_log syslog:server=172.17.0.1:514,tag=nginx_access_log;
    error_log syslog:server=172.17.0.1:514,tag=nginx_error_log;
    ...
}
http {
	...
	access_log  /var/log/nginx/access.log  main;
    access_log syslog:server=172.17.0.1:514,tag=nginx_access_log;
    error_log syslog:server=172.17.0.1:514,tag=nginx_error_log;
    ...
}

之后在容器启动之后,在终端输入命令查看syslog

shell
$ tail -f /var/log/syslog
$ tail -f /var/log/syslog

打开浏览器访问nginx的服务,就可以在上面终端看到访问日志了

参考阅读

Module ngx_http_stub_status_module

nginx syslog mod

Last updated:

Released under the MIT License.