Nginx日志分割
使用方法(1)
30 23 * * * cd /home/ccodsupport/nginxLog && sudo ./nginxLog.sh &>/dev/null
#!/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 "