2 按天输出access日志
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
}
access_log '/usr/local/nginx/logs/access_${year}${month}${day}.log';
如果用awk分析这种按天命名的日志文件,可以这样:
tmonth=`date +%m`
tyear=`date +%y`
tday=`date +%d`
day=`expr $tday + 0`
month=`expr $tmonth + 0`
year=`expr $tyear + '2000'`
now_date=`printf "%04d%02d%02d" $year $month $day`
# 解析的日志样例如:39.100.93.154 - - [12/Aug/2020:16:40:46 +0800] "POST /v1/books/returned HTTP/1.1" 200 190 "-" "-"
# 请求次数最多的url,top 20
awk '{print $7}' access_${now_date}.log |sort | uniq -c |sort -n -k 1 -r|head -n 20
# 每秒并发请求数,top 20
awk '{print $4}' access_${now_date}.log |cut -c 14-21|sort|uniq -c|sort -nr|head -n 20
1 限制文件大小
在server_name那段里加一个:client_max_body_size 5m