Example One
/script/cut_log.phps automatically cuts the Nginx Web Access log. The default will save 7 days of log, according to the hard disk space situation adjustment, as much as possible to save more log. This log is very important, such as the site is running abnormally, or the Web site is black, logs are very important detection channels.
/script/del_log.sh automatically deletes other unimportant logs every day.
The Cron scheduling task rules are in this file:
/etc/cron.d/yundaiwei
The contents are as follows:
##### Delete web Logs
0 0 * * * ROOT/SCRIPT/CUT_LOG.PHPS &>/dev/null
##### Delete error logs
0 0 * * * root/script/del_log.sh &>/dev/null
Example Two
Yesterday I did a script that can be automatically deleted on a regular basis.
#!/bin/bash
find/usr/local/nginx/logs/-mtime +15-type f-name \*.log | Xargs rm-f
The above script is to delete the log files from the 15 days before nginxlogs, and you can refer to the above script to delete the log files of other programs, such as PHP and Tomcat. However, the use of the script will need to nginx log files to automatically split, otherwise it will delete the log file is being written, Nginx log segmentation script can see the Linux source mode installation nginx and PHP (FastCGI) or CentOS system initial installation MySQL, PHP and Nginx scripts.
To save the script and use the chmod +x command to specify executable permissions, it is best to join the task list (performed one o'clock in the morning every day):
[Root@localhost ~]# Crontab-e
* * * */bin/bash/root/shell/autodellogs.sh
[root@localhost ~]# Service Crond restart
Example Three
In the introduction of the lack of detail I have a look at a detailed
Description
Operating system: CentOS
Site 1:www.111cn.net
Site 2:m.111cn.net
Nginx Installation path:/usr/local/nginx
Nginx configuration file path:/usr/local/nginx/conf/nginx.conf
Site 1 configuration file path:/usr/local/nginx/conf/vhost/bbs.osyunwei.com.conf
Site 2 configuration file path:/usr/local/nginx/conf/vhost/sns.osyunwei.com.conf
Objective:
1, the site 1 and site 2 of the Nginx access log saved by day, log path is:
Site 1:/usr/local/nginx/logs/nginx_logs/bbs_logs
Site 2:/usr/local/nginx/logs/nginx_logs/sns_logs
2, only 30 days to keep the log records
Specific actions:
1. Create Log Store path
Mkdir-p/usr/local/nginx/logs/nginx_logs/bbs_logs
Mkdir-p/usr/local/nginx/logs/nginx_logs/sns_logs
2, set nginx logging format
Vi/usr/local/nginx/conf/nginx.conf #编辑
Log_format Main ' $remote _addr-$remote _user [$time _local] ' $request '
' $status $body _bytes_sent ' $http _referer '
' $http _user_agent ', ' $http _x_forwarded_for ';
Cancel the comment in front of the logging format and add a row at the end, as follows:
Log_format Main ' $remote _addr-$remote _user [$time _local] ' $request '
' $status $body _bytes_sent ' $http _referer '
"$http _user_agent" "$http _x_forwarded_for"
' $http _host $upstream _status $upstream _addr $request _time $upstream _response_time '; #新添加的一行
: wq! #保存退出
3, set up the virtual host configuration file
Vi/usr/local/nginx/conf/vhost/bbs.osyunwei.com.conf #编辑, in bbs.osyunwei.com; this line is added below
Access_log Logs/bbs_access.log Main; #注意这里的main要和上一步中的main一致
: wq! #保存退出
Vi/usr/local/nginx/conf/vhost/sns.osyunwei.com.conf #编辑, in sns.osyunwei.com; this line is added below
Access_log Logs/sns_access.log Main; #注意这里的main要和上一步中的main一致
: wq! #保存退出
Service Nginx Reload #重新加载nginx配置文件
4. Create Nginx Log Cutting script
Vi/home/crontab/cut_nginx_log.sh #编辑
#!/bin/sh
Logs_path= "/usr/local/nginx/logs/"
Days=30
MV ${logs_path}bbs_access.log ${logs_path}nginx_logs/bbs_logs/bbs_access_$ (date-d "Yesterday" + "%Y%m%d"). Log
MV ${logs_path}sns_access.log ${logs_path}nginx_logs/sns_logs/sns_access_$ (date-d "Yesterday" + "%Y%m%d"). Log
KILL-USR1 ' Cat/usr/local/nginx/logs/nginx.pid '
Find ${logs_path}nginx_logs/bbs_logs/-name "bbs_access_*"-type f-mtime + $DAYS-exec rm {} \;
Find ${logs_path}nginx_logs/sns_logs/-name "sns_access_*"-type f-mtime + $DAYS-exec rm {} \;
: wq! #保存退出
chmod +x/home/crontab/cut_nginx_log.sh #添加脚本执行权限
5, add Task plan, modify/etc/crontab
Vi/etc/crontab #在最后一行添加
0 0 * * * root/home/crontab/cut_nginx_log.sh #表示每天凌晨执行
: wq! #保存退出
6, restart Crond to make the settings effective
/etc/rc.d/init.d/crond Restart #yum install-y Vixie-cron installation Scheduled Tasks, some systems may not have pre-installed
Chkconfig Crond on #设为开机启动
Service Crond Start #启动
Every day in/usr/local/nginx/logs/nginx_logs/bbs_logs and/usr/local/nginx/logs/nginx_logs/sns_logs.
Log files similar to Bbs_access_20140126.log and Bbs_access_20140126.log are generated in the directory
And only keep the log records for the last 30 days
At this point, Linux under the scheduled cutting Nginx access log and delete the specified number of days before the completion of the log record.
Note: If the following error occurred during the execution of the script
Nginx: [ERROR] Open () "/usr/local/nginx/logs/nginx.pid" failed
Solution:
/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf
#使用nginx-C parameter specifies the location of the nginx.conf file
Extended reading:
========================================================
Nginx Access Log parameter description
192.168.21.1--[27/jan/2014:11:28:53 +0800] "get/2.php http/1.1"-"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1707.0 safari/537.36 ""-"192.168.21.128 200 127.0.0.1:9000 0.119 0.119
$remote _addr: Client Address 192.168.21.1
$remote _user: Client User-
$time _local: Times and time zones 27/jan/2014:11:28:53 +0800
$request: Requested URL path and HTTP protocol get/2.php http/1.1
$status: HTTP status 200
$body _bytes_sent: Sent to client page size 133
$http _referer: page Jump Source-
$http _user_agent: User access to Terminal mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1707.0 safari/537.36
$http _x_forwarded_for:http Agent, the request-side real IP-
$http _host: User-entered URL (IP or domain name) address in the browser 192.168.21.128
$upstream _status:upstream State 200
$upstream _addr: Back-end upstream address and Port 127.0.0.1:9000
$request _time: Page access Total time 0.119
$upstream _response_time: Upstream response time in page access 0.119