Nginx统计访问IP并排序 发表于 2020-04-20 | 分类于 Linux | 统计Nginx的访客ip,并排列出访客访问次数,进行排序。 nginx访问日志格式 123xxx.xxx.xxx.xxx - - [17/04/2020:02:58:45 +0000] "GET / HTTP/1.1" 502 166 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0"xxx.xxx.xxx.xxx - - [17/04/2020:02:58:45 +0000] "GET / HTTP/1.1" 502 166 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0"xxx.xxx.xxx.xxx - - [17/04/2020:02:58:45 +0000] "GET / HTTP/1.1" 502 166 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0" 解决思路: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748# 先提取访客的 ip,awk提取$ cat access.log |awk '{print1}'223.166.74.72119.39.46.77110.167.94.11671.6.202.253115.192.187.161134.119.218.243184.105.247.19687.2.28.156...# 对所展示的ip进行统计操作$ cat access.log |awk '{print1}'|sort|uniq -c2 157.55.39.271 159.203.201.1558 163.177.13.21 164.68.112.1781 171.34.179.1841 175.152.111.1124 175.184.164.5984 182.138.137.127158 182.138.158.156334 182.138.158.183250 182.138.158.67...# 上面的是没有排序的,加上sort-rn命令即可排序$ cat 1.log | awk '{print$1}' |sort|uniq -c|sort -rn334 182.138.158.183250 182.138.158.67158 182.138.158.15684 182.138.137.12716 36.32.3.4610 123.245.24.1498 163.177.13.27 182.96.12.706 39.100.240.146 39.100.231.247...# 通过上面的命令即可完成统计, 如果需要展示前5名的ip,只需要加上 head -5 即可$ cat 1.log | awk '{print$1}' |sort|uniq -c|sort -rn|head -5334 182.138.158.183250 182.138.158.67158 182.138.158.15684 182.138.137.12716 36.32.3.46 -------------The End-------------