The default nginx will only generate an access log, in the accumulation of daily, log files will become very large, if you need to do a log analysis, whether it is using script analysis, or the log download local analysis, is not very convenient. The daily partition of Nginx access logs facilitates the analysis of logs.
A: Create a new script file
Vim cut_nginx_log.sh
- #!/bin/bash
- #nginx Access Log Segmentation shell script
- #www. webyang.net
- #日志目录
- Log_dir = "/usr/local/nginx/logs/"
- CD $log _dir
- Time = ' Date +%y%m%d--date= '-1 Day '
- #nginx启动目录
- Nginx_dir = "/etc/init.d/nginx"
- #日志保存天数
- save_days = the
- #日志文件名所处 '/' Split position, key ('/' number +1)
- Num = 6 ;
- #此处待优化 (get file name without suffix)
- website = ' ls $log _dir*.log | xargs-n 1 | cut-f 1-d "." | cut-f $num-D "/" '
- #日志目录下所有的日志文件为文件夹, cycle the new time file
- for I inch $website
- Do
- #判断目录是否存在
- if [! - d $log _dir$i ]; Then
- mkdir "$i"
- Fi
- MV $log _dir$i . Log $log _dir$i / $i - $time . Log
- Done
- #删除设置天数前的数据
- Find $log _dir / - Mtime + $save _days - exec RM - RF {} \;
- #平滑启动nginx
- $nginx _dir Reload
Second, set crontab, daily cutting
If it is not installed, run the Yum-y install Crontabs installation.
Enter the CRONTAB-E command (detail reference: http://www.webyang.net/Html/web/article_232.html),
Enter the following content:
XX * * * */bin/bash/usr/local/nginx/cut_nginx_log.sh
1, 00 00 means 00 Points 00 points, that is, 0 o'clock in the morning, the back "* * *" for "Sun and Moon Year" does not need to define
2. "/usr/local/nginx/cut_nginx_log.sh" is the path of your shell.
Ok! When you're done, the log is automatically cut at 0 o'clock every day and the corresponding directory is named after the site log file.
Third, display
Web1.log = Web1/web1-20160322.log web1/web1-20160323.log
Web2.log = Web2/web2-20160322.log web2/web2-20160323.log
Blog: http://www.webyang.net/Html/web/article_255.html
The above describes the shell script to split the Nginx log, and regularly clean up, including the content, I hope that the PHP tutorial interested in a friend helpful.