Nginx統計訪問IP並排序

reggie發表於2021-07-11

最近在做運營的一個需求,需要統計IP訪問,突發奇想能不能從Nginx的日誌裡試試,畢竟以前沒搞過,感覺挺有意思的


nginx訪問日誌格式如下:

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"
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"

解決思路:

# 先提取訪客的 ip,awk提取
$ cat access.log |awk '{print$1}'
223.166.74.72
119.39.46.77
110.167.94.116
71.6.202.253
115.192.187.161
134.119.218.243
184.105.247.196
87.2.28.156
...

# 對所展示的ip進行統計操作
$ cat access.log |awk '{print1}'|sort|uniq -c
2 157.55.39.27
1 159.203.201.155
8 163.177.13.2
1 164.68.112.178
1 171.34.179.184
1 175.152.111.112
4 175.184.164.59
84 182.138.137.127
158 182.138.158.156
334 182.138.158.183
250 182.138.158.67
...

# 上面的是沒有排序的,加上sort-rn命令即可排序
$ cat 1.log | awk '{print$1}' |sort|uniq -c|sort -rn
334 182.138.158.183
250 182.138.158.67
158 182.138.158.156
84 182.138.137.127
16 36.32.3.46
10 123.245.24.149
8 163.177.13.2
7 182.96.12.70
6 39.100.240.14
6 39.100.231.247
...

# 透過上面的命令即可完成統計, 如果需要展示前5名的ip,只需要加上 head -5 即可
$ cat access.log | awk '{print$1}' |sort|uniq -c|sort -rn|head -5
334 182.138.158.183
250 182.138.158.67
158 182.138.158.156
84 182.138.137.127
16 36.32.3.46

本作品採用《CC 協議》,轉載必須註明作者和本文連結
微信公眾號:碼咚沒 ( ID: codingdongmei )

相關文章