Home >Backend Development >PHP Tutorial >Use cronolog to cut nginx access logs and clean old logs regularly

Use cronolog to cut nginx access logs and clean old logs regularly

不言
不言Original
2018-07-07 16:38:451950browse

This article mainly introduces the use of cronolog to cut nginx access logs and clean old logs regularly. It has certain reference value. Now I share it with you. Friends in need can refer to it

Preparation work

Install cronolog

brew instal cronolog
If you encounter this error

Use cronolog to cut nginx access logs and clean old logs regularly

##Execute


sudo chown -Rwhoami:admin /usr/local/sbinIf there is no
/usr/local/sbinexecute this folder first
mkdir /usr/local/ sbin

Use cronolog to cut logs

Create log source pipe file

##mkfifo /usr/local/etc/nginx/access.log.pipe

Configure nginx access log

access_log /usr/local/etc/nginx/access.log.pipe main;

Start cronolog, When access.log.pipe generates data, use cronolog to transfer the data in access.log.pipe to access.log.%Y-%m-%d

nohup cat /usr/local/ etc/nginx/logs/access.log.pipe | nohup /usr/local/sbin/cronolog /usr/local/etc/nginx/logs/access.log.%Y-%m-%d &

Start or restart nginx

nginx start or nginx -s raload

Clear old logs regularly

Create a cleanup script

vi delete_nginx_logs.sh

Save content

LOG_PATH="/usr/local/etc/nginx/logs"
save_days=7
find $LOG_PATH -mtime +$save_days -exec rm -rf {} \;

Add scheduled execution task

crontab -e

Execute at 0 o'clock every day

0 0 0 * * sh /usr/local/nginx/delete_nginx_logs.sh
The above is the entire content of this article, I hope it will be useful for everyone's learning Help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Location configuration method in Nginx

##Use nginx to deploy multiple Web Servers on one server

The above is the detailed content of Use cronolog to cut nginx access logs and clean old logs regularly. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn