一般 nginx 日志会使用第三方分析工具或打入的日志系统进行分析

日志分析

日志格式如下:

log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';

访问量前10的IP

# 1.截取第1列,即IP列
# 2.排序(因为uniq默认只对连续行去重)
# 3.去重并统计重复行的次数
# 4.对重复行的次数进行排序 -n:数字类型排序 -r:倒序 -k1:第一列,即重复行的次数
# 5.取前10个访问次数最多的IP
cat access.log | awk '{print $1}' | sort | uniq -c | sort -n -r -k 1 | head -10
cat access.log | cut -f1 -d ' ' | sort | uniq -c | sort -n -r -k 1 | head -10

404请求占比

# 1.统计总行数,即总请求数,赋值给变量total
# 2.统计包含404的行数,先通过 " 截取,再通过 空格 截取并过滤出 404 的行,赋值给变量four_zero_four
# 3.通过expr计算404页面的占比
export total=`wc -l access.log | cut -f1 -d " "` && export four_zero_four=`cat access.log | awk -F "\"" '{print $3}' | awk '{ if($1 == "404") print $1}' | wc -l` && expr $four_zero_four \* 100 / $total

results matching ""

    No results matching ""