动态accesslog

1. 功能说明

通过动态access_log,无需对配置文件进行编辑及重载,就可以实现对access log开关状态的动态控制功能,现提供了动态查询接口和动态修改接口。

动态access_log开关accessLogOn缺省值为true。该动态开关配置在http,server,location的上下文配置中修改没有继承关系。

静态access log开关指令为access_log off,再把动态acesss log开关同时配置情况下,以动态acceess log开关配置为优先。

2.依赖模块

本功能依赖模块:

load_module modules/njt_agent_dynlog_module.so;  
load_module modules/njt_http_sendmsg_module.so;
load_module modules/njt_ctrl_config_api_module.so; 

3.配置说明

njet.conf配置文件中务必有此模块

load_module modules/njt_agent_dynlog_module.so;  

njet_ctrl.conf配置文件中务必包含这些模块

load_module modules/njt_http_sendmsg_module.so;
load_module modules/njt_ctrl_config_api_module.so; 

http {
     server {
        listen       8081;
       
        location /api {
            dyn_module_api;
           }
        }
     }
}

4.调用样例

4.1 API说明

查询接口:

GET http://IP+port/api/v1/config/http_log

修改接口:

PUT http://IP+port/api/v1/config/http_log

4.2动态查询接口

可以查看HTTP 下的所有server及location,以及各个location 块内access log 当前的开关状态;

请求

 curl -X GET http://127.0.0.1:8081/api/v1/config/http_log

结果

< HTTP/1.1 200 OK
< Server: njet/1.23.1
< Date: Mon, 22 May 2023 09:38:22 GMT
< Content-Type: application/json
< Content-Length: 490
< Connection: keep-alive

{
  "servers": [
    {
      "listens": [
        "0.0.0.0:90"
      ],
      "serverNames": [
        "localhost"
      ],
      "locations": [
        {
          "location": "/",
          "accessLogOn": true,
          "accessLogs": [
            {
              "path": "logs/access.log",
              "formatName": "combined"
            }
          ]
        },
        {
          "location": "/test_accesslog",
          "accessLogOn": true,
          "accessLogs": [
            {
              "path": "logs/access.log",
              "formatName": "combined"
            }
          ]
        }
      ]
    }
  ],
  "accessLogFormats": [
    {
      "name": "combined",
      "escape": "default",
      "format": "$remote_addr - $remote_user [$time_local] \"$request\" $status$body_bytes_sent \"$http_referer\" \"$http_user_agent\""
    }
  ]
}

4.3 动态修改接口

可以对查询结果中的access log的开关状态进行动态修改。

请求

curl -X PUT http://127.0.0.1:8081/api/v1/config/http_log
Content-Type: application/json

{
  "servers": [
    {
      "listens": [
        "0.0.0.0:90"
      ],
      "serverNames": [
        "localhost"
      ],
      "locations": [
        {
          "location": "/",
          "accessLogOn": true,
          "accessLogs": [
            {
              "path": "logs/access.log",
              "formatName": "combined"
            }
          ]
        },
        {
          "location": "/test_accesslog",
          "accessLogOn": true,
          "accessLogs": [
            {
              "path": "logs/access.log",
              "formatName": "combined"
            }
          ]
        }
      ]
    }
  ],
  "accessLogFormats": [
    {
      "name": "combined",
      "escape": "default",
      "format": "$remote_addr - $remote_user [$time_local] \"$request\" $status$body_bytes_sent \"$http_referer\" \"$http_user_agent\""
    }
  ]
}

结果

< HTTP/1.1 200 OK
< Server: njet/1.23.1
< Date: Mon, 22 May 202309:49:20 GMT
< Content-Type: application/json
< Content-Length: 27
< Connection: keep-alive
{
  "code": 0,
  "msg": "success."
}