Nginx by day partition log nginx apache nginx php nginx rewrite

Source: Internet
Author: User
Tags log log
Nginx default log is unified write to the installation directory under the log, access log is Access.log, error log is Error.log
And does not provide a configurable place, for the production environment, the log is concentrated together, not a few days log files can be large to a few grams, can only use the operating system of the scheduled task, their own script timed execution, the log log to be cut.

Refer to the article on the online predecessors, wrote a script themselves out. The code is as follows:

  #定义日志的源路径, which is the default log path for Nginx   source  _log_path=/usr/local/nginx/logs  #定义保存后的路径位置  Dest_log_path=/opt/logs/nginx  #定义log文件的名称, in units per day, defines the log prefix as yesterday's date, because timed tasks are intended to be executed after 0 o'clock in the morning.  yesterday=$ (Date -D   "Yesterday"  +%y%m%d)  #echo ${yesterday}   #rm- RF ${dest_log_path}/*   #指定移动  mv  ${source_log_path} /access.log  ${dest_log_path } /nginx_access_  ${yesterday} . LOGMV  ${source_log_path} /error.log  ${dest_log _path} /nginx_error_  ${yesterday} . Log  #移动后, re-send the message to the Nginx main process, so that the main process of Nginx will reopen the log. Otherwise the log will continue to be written to the path after MV  KILL-USR1 ' PS Axu | grep  "nginx:master process"  | Grep-v grep | awk  ' {print $} '  '  cd   ${dest_log_path}   #进入日志的路径, query the log before the day before, and then delete it.  find. -mtime +  7 -name  "*20[1-9][3-9]*"  |xargs rm-rf  exit   0  

Finally, save the code to an SH file and add the file to the system's scheduled tasks.

#定时任务的文件在 /etc/crontab 下,vi此文件,设置为每天 零点 执行。* * * root /bin/sh /opt/sampleEnvironment/commands/nginxlogperday.sh

The code is not really complicated. But it took too much time to write,
The problem is: After defining the variables, such as Source_log_path execution of the script, will be error command not found
At the end of the day I found out that I wrote this in the definition of Source_log_path:

/usr/local/nginx/log

A space is struck on both sides of the equal sign, which causes the error above. This is true when the date variable is defined.

When you use the VI command at ordinary times, just stay in the stage of entry, hardly a few commands. "DD" is known at most.
This time we need to edit, only to find how laborious. The more commands to use this time are:

Delete one line: DD
Move to end of line: $
Move to the end of the line and edit: a (or SHIFT + a)
Move to the end of the document: Shift + G

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the Nginx split-day log, including nginx content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.