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

不言
Release: 2023-04-02 20:40:02
Original
1753 people have browsed it

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 {} \;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!