← 返回上一頁
shell script

nginx統計access.log裡IP訪問腳本

最後更新:2018-07-12
Nginx
⚠ 本文最後更新於 2018-07-12(7 年前),部分內容可能已過時。

以下檔案下載

count_req

#!/bin/bash
#
# Filename:    count_req.sh
# Revision:    1.0
# Author:      Qicheng
# Website:     http://qicheng0211.blog.51cto.com
# Description: 統計Nginx日誌裡IP訪問頻率
 
NGINXLOG="./access.log"
start_time=$(head -n1 "$NGINXLOG" | grep -o " \[.*\] ")
stop_time=$(tail -n1 "$NGINXLOG" | grep -o " \[.*\] ")
echo -e "start:\t\e[92m$start_time\033[0m"
echo -e "stop:\t\e[92m$stop_time\033[0m"
echo '所有的請求TOP50-->>'
# 所有的請求
cat "$NGINXLOG" | awk '{++S[$1]} END {for(a in S) print S[a],"\t", a}' | sort -rn -k1 | head -n 50
echo '--------------------------------------------------'
echo '成功的請求TOP50-->>'
# 成功的請求
grep ' 200 ' "$NGINXLOG" | awk '{++S[$1]} END {for(a in S) print S[a],"\t", a}' | sort -rn -k1 | head -n 50

把腳本放到和access.log同一目錄下執行即可。

分享這篇
X LinkedIn Facebook Hacker News Reddit

在〈“nginx統計access.log裡IP訪問腳本”〉中有 2 則留言

  1. 這兩句後面要稍微改一下,顯示才正常
    echo -e "start:\t\e[92m$start_time33[0m"
    echo -e "stop:\t\e[92m$stop_time33[0m"

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料