1.awk对nginx日志文件状态码的查询统计命令:
No.1.
cat /var/log/nginx/access.log-20210128 | awk '{print $9,$10}' | awk 'BEGIN{count[$1]=0}($2!=200){count[$1]+=1}END{for(i in count) print(i,count[i])}'
No.2.
awk '{print $9,$10}' /var/log/nginx/access.log-20210128 | awk 'BEGIN{count[$1]=0}($2!=200){count[$1]+=1}END{for(i in count) print(i,count[i])}'
2.统计nginx访问ip的数量有几个(独立ip访问数量):
cat /var/log/nginx/access.log-20210128 | awk '{print $1}' | sort -nr | uniq | wc -l
3.统计不同ip访问nginx的次数,并由多到少排序:
cat /var/log/nginx/access.log-20210128 | awk '{print $1}' | sort | uniq -c| sort -rn
4.统计在某段时间内(1月27日15点)的访问总次数:
使用正则表达式
cat /var/log/nginx/access.log-20210128 | grep -E '27/Jan/2021:15'| wc -l
统计在某段时间内(1月27日15点-16点)的访问总次数:
cat /var/log/nginx/access.log-20210128 | grep -E '27/Jan/2021:1[5-6]'| wc -l
统计在某段时间内(1月27日15点-24点)的访问总次数:
cat /var/log/nginx/access.log-20210128 | grep -E '27/Jan/2021:1[5-9]|27/Jan/2021:2[0-3]'| wc –l
5.查看访问最频繁的前3个ip,并列出访问ip:
cat /var/log/nginx/access.log-20210128 | awk '{print $1}' | sort -n | uniq -c| sort -rn | head -n 3
6.查看访问次数大于X(这里假设是100)的ip,并列出访问次数:
cat /var/log/nginx/access.log-20210128 | awk '{print $1}' | sort -n | uniq -c| sort -rn | awk '{if($1>100)print $0}'
7.查询出访问最频繁的URL:
cat /var/log/nginx/access.log-20210128 | awk '{print $7}' | awk -F '?' '{print $1}' | sort -n | uniq -c | sort -nr | head -n 1
对url进行统计:
cat /var/log/nginx/access.log-20210128 | awk '{print $7}' | awk -F '?' '{print $1}' | sort -n | uniq -c | sort –nr
8.统计访问成功次数(状态码为2开头的都为成功2xx):
cat /var/log/nginx/access.log-20210128 | awk '{if($9 ~ /2[0-9]{2}/)print $9}' | wc -l