#日志格式:
'$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "
$http_referer" "$http_user_agent" "$http_x_forwarded_for"'
#统计访问 IP 次数:
awk '{a[$1]++}END{for(v in a)print v,a[v]}' access.log
#统计访问访问大于 100 次的 IP:
awk '{a[$1]++}END{for(v in a){if(a[v]>100)print v,a[v]}}' access.log
#统计访问 IP 次数并排序取前 10:
awk '{a[$1]++}END{for(v in a)print v,a[v] |"sort -k2 -nr |head -10"}' access.log
#统计时间段访问最多的 IP:
awk '$4>="[02/Jan/2017:00:02:00" && $4<="[02/Jan/2017:00:03:00"{a[$1]++}END{for(v in a)print v,a[v]}' access.log
#统计上一分钟访问量:
date=$(date -d '-1 minute' +%d/%d/%Y:%H:%M)
awk -vdate=$date '$4~date{c++}END{print c}' access.log
#统计访问最多的 10 个页面:
awk '{a[$7]++}END{for(v in a)print v,a[v] |"sort -k1 -nr|head -n 10"}' access.log
#统计每个 URL 数量和返回内容总大小:
awk '{a[$7]++;size[$7]+=$10}END{for(v in a)print a[v],v,size[v]}' access.log
#统计每个 IP 访问状态码数量:
awk '{a[$1" "$9]++}END{for(v in a)print v,a[v]}' access.log
#统计访问 IP 是 404 状态次数:
awk '{if($9~/404/)a[$1" "$9]++}END{for(i in a)print v,a[v]}' access.log
#统计访问URL统计PV:
awk '{print $7}' /usr/local/Nginx/logs/access.log|wc -l
#根据访问IP统计UV:
awk '{print $1}' /usr/local/nginx/logs/access.log|sort | uniq -c |wc -l
#查询访问最频繁的IP
awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|head -n 50
#查询访问最频繁的URL
awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|head -n 50
#nginx当前并发数
netstat -ant | grep :80 | awk '{print $5}' | awk -F":" '{print $1}'| sort | uniq -c | sort -r -n | awk 'BEGIN{total=0}{total+=$1}END{print total}'
#查询时间段内的访问日志,统计出访问IP及其访问次数
tac access.log | grep "HTTP/1.1" 404" | awk -v st="15/Oct/2018:14:58:47" -v et="15/Oct/2018:14:59:47" '{t=substr($4,RSTART+2,21);if(t>st && t<=et) {print $0}}' | awk '{print $1}' | sort | uniq -c | sort -nr