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

最佳Nginx日志分割

Nginx日志分割

使用方法(1)

  • 使用定时任务
30 23 * * * cd /home/ccodsupport/nginxLog && sudo ./nginxLog.sh &>/dev/null
  • 修改nginx日志路径
#!/bin/bash

# 设置Nginx日志目录
logdir="/home/nginx/logs"

# 查找所有日志文件
find "$logdir" -type f -name "*.log" | while read logfile; do
  # 检查文件是否存在
  if [ -f "$logfile" ]; then
    # 提取日期
    date=`date +%F`
    if [ -z "$date" ]; then
      echo "Error: cannot extract date from $logfile"
      continue
    fi
    # 创建新日志文件名
    newlogfile="$logdir/$(basename "$logfile" .log)_$date.log"
    # 将日志文件拆分为新文件
    cp  "$logfile" "$newlogfile"
    > $logfile
  fi
done
  • 观察日志文件是否分割正常
[nginx@ucloud_nginx_1 logs]$ ls -htrl
-rw-r--r-- 1 nginx nginx 380M Apr 18 17:40 error_2023-04-18.log
-rw-r--r-- 1 nginx nginx    0 Apr 18 17:40 error.log
-rw-r--r-- 1 nginx nginx  55M Apr 18 17:40 access_2023-04-18.log
-rw-r--r-- 1 nginx nginx 737K Apr 18 17:40 zabbix_2023-04-18.log
-rw-r--r-- 1 nginx nginx  895 Apr 18 17:50 access.log
-rw-r--r-- 1 nginx nginx  210 Apr 18 17:50 zabbix.log

使用方法(2)

  • Nginx内部配置设置日志文件格式
  • 对应在主配置文件server块中修改
#在nginxhttp块中添加如下配置,如果有多个server配置了不同的日志输出,则在添加对应logdata变量即可
[root@my-dev conf]# vim nginx.conf
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" "$request_time" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr" "$upstream_response_time "' ;

    map $time_iso8601 $logdate {
    '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
    default   'date-not-found';
    }
    access_log logs/access-$logdate.log main;

# 需要对应点,该配置项对应添加可访问,错误日志如下报错
[root@my-dev conf]# tail -100f ../logs/error.log 
2025/03/02 21:44:04 [crit] 15349#0: *37 open() "/usr/local/nginx/logs/access-2025-03-02.log" failed (13: Permission denied) while logging request, client: 106.37.124.138, server: zhao138969.com, request: "GET /actuator/health HTTP/2.0", upstream: "http://39.106.51.232:80/actuator/health", host: "zhao138969.com", referrer: "https://zhao138969.com/console/single-pages/editor?name=72b282df-7b92-422e-b5b8-96ca6649f325"

#说明对应log目录没有权限,则需要做以下两种操作:
1、让nginx.conf配置文件配置root用户来启动运行nginx,即可解决。
user root;
2、让其他用户进行运行nginx,然后再其log目录设置属主对应是test用户即可,在reload加载即可。
user test;
  • 看效果
[root@my-dev logs]# ls -htrl
total 84K
-rw-r--r-- 1 root root   0 Mar  2 21:36 zabbix.log
-rw-r--r-- 1 root root 267 Mar  2 21:40 access.log
-rw-r--r-- 1 root root 15K Mar  2 21:51 access-2025-03-02.log
-rw-r--r-- 1 root root 60K Mar  2 21:53 error.log
[root@my-dev logs]# tail -100f access-2025-03-02.log
111.196.247.70 - - [02/Mar/2025:21:54:33 +0800] "GET /apis/content.halo.run/v1alpha1/snapshots/dd78515e-3745-44dc-a255-58f690ed1e38 HTTP/2.0" "0.013" 200 3203 "https://zhao138969.com/console/posts/editor?name=b7ba76fc-6287-46d1-b51e-c22628d84d9f" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "-" "127.0.0.1:8090" "0.001 "
111.196.247.70 - - [02/Mar/2025:21:54:48 +0800] "GET /actuator/health HTTP/2.0" "0.008" 403 635 "https://zhao138969.com/console/posts/editor?name=b7ba76fc-6287-46d1-b51e-c22628d84d9f" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "-" "39.106.51.232:80" "0.008 "

相关文章:

  • InterHand26M(handposeX-json 格式)数据集-release >> DataBall
  • DAV_postgresql_3-schema
  • 初识MySQL · 数据库
  • 大白话Webpack 怎么理解,怎么使用它?
  • c++ 文件及基本读写总结
  • LM studio 加载ollama的模型
  • 【二分答案 C/C++】洛谷P1182 数列分段 Section II
  • 深度学习-15.生成对抗网络
  • 大语言模型学习
  • 图像算法工程师的技术图谱和学习路径
  • 从头开始学SpringBoot—02ssmp整合及案例
  • leetcode 48. 旋转图像
  • 《白帽子讲 Web 安全》之移动 Web 安全
  • SpringCloud系列教程(八):服务网关Gateway
  • 基于JavaWeb开发的Java+SpringBoot+vue+element实现物流管理系统
  • 基于Leaflet和SpringBoot的全球国家综合检索WebGIS可视化
  • python多线程之Event机制笔记
  • Hutool - JWT:轻松玩转 JSON Web Token
  • 基于springboot+vue实现的宠物救助及领养平台(源码+L文+ppt)43-21
  • C++ 标准库容器的常用成员函数
  • 国税总局上海市税务局通报:收到王某对刘某某及相关企业涉税问题举报,正依法依规办理
  • 泽连斯基与埃尔多安会面,称已决定派遣代表团前往伊斯坦布尔
  • 新任国防部新闻发言人蒋斌正式亮相
  • 人民网三评“网络烂梗”:莫让低级趣味围猎青少年
  • 来沪一个月几乎未花住宿钱,女子虚构卫生问题屡薅酒店羊毛被刑拘
  • 广东早熟荔枝“抢滩”上海,向长三角消费者喊话:包甜,管够