当前位置: 首页 > news >正文

NGINX自动清理180天之前的日志

需求描述

日志每天会以天为单位产生一个日志,不清理的话会越来越多。这里写一个Lua自定定时清理日志目录下的日志文件。

 依赖安装

安装 lfs 模块

yum install luarocks
yum install lua-develluarocks install luafilesystem

创建模拟旧文件

创建了一个1月的旧文件

[root@iZbp1xxxxxxxxxq7ioZ logs]# ll
total 24
-rw-r--r-- 1 app  app  5124 Jan 16 15:30 access_api_.json_2024-01-16
-rw-r--r-- 1 app  app  8540 Aug 16  2024 access_api_.json_2024-08-16
-rw-r--r-- 1 app  app     0 Aug 14  2024 api-error.log
-rw-r--r-- 1 root root  747 Jan 16 16:41 luatest.lua

配置文件及脚本

检查配置文件后开始重启,中间的Lua脚本会24小时执行一次,检查目录中是否有180天的日志文件。

user  root;
worker_processes  6;
events {worker_connections  30000;
}
http {
# lua环境变量
lua_package_cpath "/usr/lib64/lua/5.1/?.so;;";###init_worker_by_lua_block {local lfs = require "lfs"local function clean_old_logs(premature)if premature thenreturnend-- 日志目录路径local log_path = "/data/logs/"-- 当前时间local current_time = os.time()-- 定义保留天数local retain_days = 180local retain_seconds = retain_days * 24 * 60 * 60-- 遍历日志目录for file in lfs.dir(log_path) do-- 检查文件名是否符合日志格式if file:match("access_api.json_%d%d%d%d%-%d%d%-%d%d") then-- 获取文件的完整路径local file_path = log_path .. file-- 获取文件的属性local attr = lfs.attributes(file_path)if attr thenlocal file_age = current_time - attr.modificationngx.log(ngx.NOTICE, "File: ", file_path, " Age: ", file_age, " Retain seconds: ", retain_seconds)-- 如果文件超过30天,则删除if file_age > retain_seconds thenlocal success, err = os.remove(file_path)if success thenngx.log(ngx.NOTICE, "Deleted old log file: ", file_path)elsengx.log(ngx.ERR, "Failed to delete log file: ", file_path, " Error: ", err)endendelsengx.log(ngx.ERR, "Failed to get attributes for file: ", file_path)endendendend-- 设置定时器,每86400秒(即24小时)执行一次local ok, err = ngx.timer.every(86400, clean_old_logs)if not ok thenngx.log(ngx.ERR, "Failed to create timer: ", err)end}###include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;###日志格式log_format json  escape=json '{"timestamp":"$time_iso8601",''"remote_addr": "$remote_addr", ''"referer": "$http_referer", ''"request": "$request", ''"status": $status, ''"bytes": $body_bytes_sent, ''"agent": "$http_user_agent", ''"x_forwarded": "$http_x_forwarded_for", ''"up_addr": "$upstream_addr",''"up_host": "$upstream_http_host",''"up_resp_time": "$upstream_response_time",''"request_time": "$request_time",''"request_GlobalId": "$http_GlobalId",''"response_GlobalId": "$sent_http_GlobalId"''"response_body": "$resp_body",''"request_body": "$request_body"'' }';###日志按天分割map $time_iso8601 $logdate{'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;default 'date-not-found';}###charset  utf-8 ;gzip  on;# 后端IP地址upstream api-prod {server 10.66.66.86:8501 max_fails=5 fail_timeout=30s;server 10.66.66.88:8501 max_fails=5 fail_timeout=30s;}server {listen       80 ;listen       7309 ;server_name  api.xxxxxx.cn api-test.xxxxxx.cn;charset  utf-8 ;#日志配置lua_need_request_body on;set $resp_body "";body_filter_by_lua 'local resp_body = string.sub(ngx.arg[1], 1, 1000)ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_bodyif ngx.arg[2] thenngx.var.resp_body = ngx.ctx.bufferedend';location / {proxy_pass http://api-prod;# 屏蔽 SLBHealthCheck 和 Blackbox Exporter/0.21.1 的 404 请求日志if ($http_user_agent ~* "(SLBHealthCheck|Blackbox Exporter/0.21.1)") {access_log off;return 200;}}access_log /data/logs/access_api.json_$logdate json;error_log /data/logs/api-error.log error;
}
}

验证

180天之前的日志文件没有了。

[root@iZbpxxxxxxxxxxxoZ logs]# ll /data/logs
total 16
-rw-r--r-- 1 root root 8540 Aug 16 16:37 access_api.json_2024-08-16
-rw-r--r-- 1 root root    0 Aug 14 16:36 api-error.log
-rw-r--r-- 1 root root  747 Jan 16  2024 luatest.lua

http://www.lryc.cn/news/424613.html

相关文章:

  • jackson 轻松搞定接口数据脱敏
  • Nginx 正则表达式与rewrite
  • tekton什么情况下在Dockerfile中需要用copy
  • 第九届世界渲染大赛在哪里提交作品呢?
  • fastjson(autoType)反序列化漏洞
  • Java入门基础16:集合框架1(Collection集合体系、List、Set)
  • Qt如何调用接口
  • Android14之解决编译libaaudio.so报错问题(二百二十七)
  • 【专题】2024年7月人工智能AI行业报告合集汇总PDF分享(附原数据表)
  • 干货分享|如何使用Stable Diffusion打造会说话的数字人?
  • OrangePi AIpro学习4 —— 昇腾AI模型推理 C++版
  • vue js 多组件异步请求解决方案
  • 【Android】不同系统版本获取设备MAC地址
  • 残差网络--NLP上的应用
  • 1章4节:数据可视化, R 语言的静态绘图和 Shiny 的交互可视化演示(更新2024/08/14)
  • 浅谈个人用户如何玩转HTTP代理
  • 动手研发实时口译系统
  • C#(asp.net)电商后台管理系统-计算机毕业设计源码70015
  • Unity 中创建动画的教程
  • 2024年最全渗透测试学习指南,小白也能轻松hold住!零基础到精通,看完这篇就够了!
  • 有道云docx转换markdown,导入hugo发布到github page,多平台发布适配
  • 如何理解:进程控制
  • 工业互联网边缘计算实训室解决方案
  • Android全面解析之Context机制(一) :初识Android context
  • 气象百科——气象监测站的介绍
  • 学懂C++(三十):高级教程——深入解析 C++ Windows API 的多线程支持
  • 苹果笔记本电脑可以玩steam游戏吗 MacBook支持玩steam游戏吗 在Steam上玩黑神话悟空3A大作 苹果Mac怎么下载steam
  • 海康摄像头(测温型)桌面客户端开发分享
  • 骑行耳机哪个品牌性价比高?精选五大畅销骑行耳机推荐!
  • libcurl8.9.1 上传json