【面试题】分析nginx访问日志,找出访问页面数量前十的ip 您所在的位置:网站首页 军舰数量排名前十 【面试题】分析nginx访问日志,找出访问页面数量前十的ip

【面试题】分析nginx访问日志,找出访问页面数量前十的ip

2024-07-09 17:17| 来源: 网络整理| 查看: 265

nginx访问日志access.log的日志格式:

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main;

第一个字段就是访问的ip。 因此获取访问页面数量前十的ip的方法为:

cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head

思路:每条访问记录占一行,获取第一个字段的ip,排序后去重,再按出现次数降序排序,最后打印前十个。 uniq -c 去重后再行首标记重复次数 sort -nr 逆序排序 head 默认打印前十个

[root@Charramma logs]# cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head 21 119.90.20.254 5 119.90.20.25 4 119.90.21.2 4 119.90.20.253 2 119.90.2.254 2 119.90.22.34 2 119.90.22.3 2 119.90.2.2 2 119.90.20.23 1 119.9.20.254


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有