Automatically split nginx log files every day

Source: Internet
Author: User

Source: https://www.centos.bz/2011/03/split-nginx-logfile-eveyday/

Nginx generated by the log is a file, with the longer the website running time, the size of the log file is also growing, which we want to analyze the day log is very inconvenient, so we need to split the log file every day, and named after the time.

Create a log split script

1. Login to SSH, create cut_logs.sh file

vi/root/cut_logs.sh

2. Paste the following code into cut_logs.sh, and save

#!/bin/bash# the Nginx logs pathlogs_path= "/home/wwwlogs/" Mkdir-p ${logs_path}$ (date-d "Yesterday" + "%Y")/$ (date-d "Ye Sterday "+"%m ")/mv ${logs_path}www.juzihc.com.log ${logs_path}$ (date-d" Yesterday "+"%Y ")/$ (date-d" Yesterday "+"%m ") /juzihc_$ (date-d "Yesterday" + "%y%m%d"). LOGKILL-USR1 $ (cat/usr/local/nginx/logs/nginx.pid)

3. Add cut_logs.sh Execute Permissions

chmod +x/root/cut_logs.sh
Set cut_logs.sh start time

Execute command crontab-e enter edit state
Add the following code, which starts every day 0:01.

* * * */root/cut_logs.sh

This makes it successful to split the log files on a daily schedule. Of course, if you're worried about the log file taking up too much space, you can also perform a compressed tar and set the log file to delete many days ago.


For more information about operations and maintenance, refer to:

https://www.centos.bz/

such as: Zabbix monitoring memcached php-fpm Tomcat Nginx MySQL website Log

Automatically split nginx log files every day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.