: This article mainly introduces how to split Nginx logs using Shell scripts and regularly clean up Nginx logs. For more information about PHP tutorials, see. By default, nginx generates only one access log. during daily accumulation, the log file will become very large. if you need to analyze the log, whether it is using script analysis, it is not convenient to download logs for local analysis. Splitting Nginx access logs every day facilitates log analysis.
I. create a script file
Vim cut_nginx_log.sh
- #! /Bin/bash
- # Nginx access log segmentation shell script
- # Www.webyang.net
- # Log Directory
- Log_dir = "/usr/local/nginx/logs /"
- Cd $ log_dir
- Time = 'date + % Y % m % d -- date = "-1 day "'
- # Nginx Startup directory
- Nginx_dir = "/etc/init. d/nginx"
- # Log Retention days
- Save_days = 15
- # Location where the log file name is located '/', key (number of '/' + 1)
- Num = 6;
-
- # Here to be optimized (get the file name without a suffix)
- Website = 'ls $ log_dir *. log | xargs-n 1 | cut-f 1-d "." | cut-f $ num-d "/"'
- # All log files in the log directory are folders, and corresponding time files are created cyclically.
- For I in $ website
- Do
- # Determine whether a directory exists
- If [! -D $ log_dir $ I]; then
- Mkdir "$ I"
- Fi
- Mv $ log_dir $ I. log $ log_dir $ I/$ I-$ time. log
- Done
-
- # Delete data before the specified number of days
- Find $ log_dir/-mtime + $ save_days-exec rm-rf {}\;
-
- # Enable nginx smoothly
- $ Nginx_dir reload
2. set crontab and cut regularly every day
If not, run yum-y install crontabs.
Enter the crontab-e command (for details, refer to: http://www.webyang.net/Html/web/article_232.html ),
Enter the following content:
00 00 ***/bin/bash/usr/local/nginx/cut_nginx_log.sh
1. 00 means 00: 00, that is, A.M.. you do not need to define "***" as "day, month, and year ".
2. "/usr/local/nginx/cut_nginx_log.sh" is your shell path.
OK! At every day, the log is automatically cut and stored in a directory named after the website log file.
III. Presentation
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 preceding section describes how to use Shell scripts to split Nginx logs and regularly clean up the logs, including some content. I hope you will be helpful to anyone interested in PHP tutorials.