# shell腳本-Nginx訪問日誌分析 ## 1.原理 > 可以通過/usr/local/nginx/logs/access.log 文件-查看nginx的日誌 > > ``` > [root@localhost scripts]# tail -f /usr/local/nginx/logs/a ...
shell腳本-Nginx訪問日誌分析
1.原理
可以通過/usr/local/nginx/logs/access.log 文件-查看nginx的日誌
[root@localhost scripts]# tail -f /usr/local/nginx/logs/access.log 192.168.70.1 - [17/Jul/2023:17:35:21 +0800] "GET / HTTP/1.1" 200 173833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" "-" 192.168.70.1 - [17/Jul/2023:20:53:37 +0800] "GET / HTTP/1.1" 200 173833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" "-"
/usr/local/nginx/conf/nginx.conf 文件-定義了日誌輸出的格式
可以通過awk命令來取出所需要的數據
[root@localhost scripts]# awk '{print $0}' /usr/local/nginx/logs/access.log 192.168.70.1 - [17/Jul/2023:17:35:21 +0800] "GET / HTTP/1.1" 200 173833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" "-" 192.168.70.1 - [17/Jul/2023:20:53:37 +0800] "GET / HTTP/1.1" 200 173833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82" "-" [root@localhost scripts]# awk '{print $1}' /usr/local/nginx/logs/access.log 192.168.70.1 192.168.70.1
2.shell腳本
Nginx訪問日誌分析腳本
#!/bin/bash # 1.訪問最多的IP # 2.根據時間段來訪問最多的IP # 3.訪問量超過2次的頁面 # 4.訪問頁面狀態碼數量 LOG_FILE=$1 echo "統計訪問最多的10個IP" awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print a[v] v}' $LOG_FILE | sort -k1 -nr | head -10 echo "----------------------------------------" echo "統計一個時間段訪問最多的10個IP" awk '$3>="[16/Jul/2023:17:35:21" && $3<="[17/Jul/2023:18:00:00"{a[$1]++}END{print "UV:",length(a);for(v in a)print a[v] v}' $LOG_FILE | sort -k1 -nr | head -10 echo "----------------------------------------" echo "統計訪問量超過2次的頁面" awk '{a[$7]++}END{print "PV:",length(a);for(v in a){if(a[v]>2)print a[v],v}}' $LOG_FILE | sort -k1 -nr echo "----------------------------------------" echo "統計訪問頁面狀態碼數量" awk '{a[$7" "$8]++}END{for(v in a)print a[v],v}' $LOG_FILE | sort -k1 -nr
[root@localhost scripts]# bash 13.sh /usr/local/nginx/logs/access.log 統計訪問最多的10個IP 2192.168.70.1 UV: 1 ---------------------------------------- 統計一個時間段訪問最多的10個IP 1192.168.70.1 UV: 1 ---------------------------------------- 統計訪問量超過2次的頁面 PV: 1 ---------------------------------------- 統計訪問頁面狀態碼數量 2 HTTP/1.1" 200