Store nginx logs by date as the file name

Source: Internet
Author: User
Tags mysql import
Nginx is not stored in the date format as the file name. all logs are stored in the same name. for a long time, the log file will become very large. This is not conducive to analysis. Although nginx does not have this function, we can write a small script and schedule tasks to achieve this effect. Script... nginx is not stored in the date format as the file name. all logs are stored in the same name, and the log file will become very large for a long time. This is not conducive to analysis.
 
Although nginx does not have this function, we can write a small script and schedule tasks to achieve this effect.
 
The script code is as follows (cutn/usr/local/tool/cutnginxlog. sh)
 
#! /Bin/sh
# Program:
# Auto cut nginx log script.
#2012/2/5 b4dboy First release QQ: 137 51 52 53
 
# Nginx log path: www.2cto.com
LOGS_PATH =/var/wwwroot/bbs/logs
TODAY = $ (date-d 'Today' + % Y-% m-% d)
 
# Move the log and rename it
Mv $ {LOGS_PATH}/error. log $ {LOGS_PATH}/error _ $ {TODAY}. log
Mv $ {LOGS_PATH}/access. log $ {LOGS_PATH}/access _ $ {TODAY}. log
 
# Send a signal to the nginx main process to re-open the log file
Kill-USR1 $ (cat/usr/local/nginx/logs/nginx. pid)
 
 
 
Note the two paths in the script and grant the execution permission (chmod + x cutnginxlog. sh ).
 
The next step is to add a scheduled task and run it regularly. run the following command as the root user.
 
Echo '59 23 *** root/usr/local/tool/cutnginxlog. sh>/var/logs/cutnginxlog. log 2> & 1'>/etc/crontab
 
The script is executed at every day.
 
Ps: If you want to analyze logs at a certain time, use the Navicat for MySQL import function to import nginx logs to mysql. Next, you just need to construct an SQL statement.

From B4dboy's Blog
Related Article

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.