手动7 - nginx 日志切割

本文介绍如何配置Nginx的日志格式并实现自动切割功能,通过编写Shell脚本配合crontab定时任务来定期切割日志文件,确保服务器资源的有效利用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1

设置日志格式及路径:

全局格式设置 :nginx.conf

log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';

 

access_log /www/web_logs/access.log wwwlogs;

error_log  /www/web_logs/error.log  notice;


2

新建文件 /cron/nginx_spilit.sh

#!/bin/bash
log_files_path="/www/web_logs/"
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
log_files_name=(access access error error)
nginx_sbin="$/usr.local/nginx/sbin/nginx "
save_days=60
mkdir -p $log_files_dir
log_files_num=${#log_files_name[@]}
for((i=0;i<$log_files_num;i++));do
mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").log
done
find $log_files_path -mtime +$save_days -exec rm -rf {} \;
$nginx_sbin -s reload

3

crontab -e

00 00 * * * /bin/bash /cron/nginx_spilit.sh





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值