Accidentally found that the Access.log is 21G large, so cut it.
Nginx is a very lightweight Web server, small size, high performance, fast, and many other advantages. But shortcomings are also shortcomings, such as the resulting access log file has been one, will not be automatically cut, if the traffic is very large, will result in a very large log file size, not easy to manage. Of course, we do not want to see such a large access log file, it is necessary to manually cut this file.
Shell scripting on the Linux platform is rich, the use of Shell script plus crontab command can be very convenient to cut, but on the Windows platform is a bit more trouble, just a long time, it is here to record a tidy.
Log file Cutting requirements
Since Nginx logs are written in one file, we need to save the previous day's log as another file at 0 o ' Access.log, where we save the Nginx file in the logs directory as Access_[yyyy-mm-dd].log. In fact, logs directory also has a error.log error log file, this file also need to cut one every day, here said Access.log, Error.log cutting method similar.
Linux Platform Cutting
To cut on a Linux platform, use the date command to get yesterday's date, send a signal to the Nginx process to reopen the log file using the Kill command, and Crontab set the execution task cycle.
First create a Shell script, as follows:
#!/bin/bash## 0 points to execute the script # # Nginx log files in the directory logs_path=/usr/local/nginx/logs## get yesterday's yyyy-mm-ddyesterday= ' date-d ' Yesterday "+"%y%m%d "' # # Move file mv ${logs_path}/access.log ${logs_path}/access_${yesterday}.log## send USR1 signal to Nginx main process. USR1 signal is re-opened log file Kill-usr1 ' Cat/usr/local/nginx/logs/nginx.pid '
The last line in the above script must send the USR1 signal to the Nginx process to reopen the log file, and if not, Nginx will continue to write the log information to the Access_[yyyy-mm-dd].log file, which is obviously incorrect.
After the script is completed, it is stored in the Nginx installation directory of the Sbin, named Cut-log.sh, and then using CRONTAB-E to add a scheduled task, in which to increase the execution of this script:
0 1 * * * /home/shell/cut_log/cut_nginx_log.sh
/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
View crontab Service Status: Crond status
Manually Start crontab services: Service Crond start
To see if the Crontab service is set to boot, execute command: NTSYSV
Do not join the boot automatically start: so that every time manually start trouble: Chkconfig--level Crond on
Nginx log File Cutting