1. 编写 cut_nginx_logs.sh  自动切割日志脚本 

#!/bin/bash
#set the path to nginx log files
log_files_path="/usr/local/nginx/logs/"
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")

#set nginx log files you want to cut
log_files_name=(access error nginx_erro wanda_access kohler_access)
#set the path to nginx.
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save
save_days=30
############################################
#Please do not modify the following script #
############################################
mkdir -p $log_files_dir
 

log_files_num=${#log_files_name[@]}
 

#cut nginx log files
for((i=0;i<$log_files_num;i++));do
mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").log
done


#delete 30 days ago nginx log files
find $log_files_path -mtime +$save_days -exec rm -rf {} \; 


$nginx_sbin -s reload
 


 

-----------------------------------------------------------------------------------------------------------

crontab -e 编写定时执行的脚本

00 00 * * * /bin/bash /root/cut_nginx_logs.sh

 

---------------------------------------------------------------------------------------------

分析nginx日志

linux下用awk统计日志中各个ip访问页面的次数

 
[root@www 08]# awk '{a[$1]+=1;}END{for(i in a){print a[i]" " i;}}' access_20120815.log
3 58.34.124.67
59 180.158.23.31
1 222.73.185.2
2 112.65.193.13
1 183.201.255.5
4 112.90.85.200
1 61.191.188.85
3 220.181.141.69
3 59.151.106.234
197 221.178.9.41
2 127.0.0.1
39 123.147.245.51
1 74.125.126.98
186 61.170.251.200
2 157.55.17.198
381 58.246.147.6
1 58.57.18.210
3 58.245.5.183
19 180.155.113.190
22 124.108.46.3
690 58.38.252.30
2 123.151.42.53
89 119.6.39.37
92 222.45.32.15
108 124.207.107.122
8720 116.226.72.205
489 218.16.100.196
1 38.100.21.63
2 88.198.25.25
1 222.186.189.160
80 125.83.53.24
423 60.20.17.134
272 123.172.82.148
179 222.67.174.241
137 115.238.93.82