How to Use awk to Analyze Nginx Log

nginx log field description

127.0.0.1 – – [31/Aug/2018:16:11:16 +0800] “GET /50x.html HTTP/1.1” 200 537 “-” “curl/7.29.0”

Access ip, access time, request method, request url, response status code, response body size, ua

 

Statistics based on access ip

cat access.log | awk ‘{count[$1]++}END{for(ip in count){print ip,count[ip]}}’

cat access.log | awk ‘{count[$1]++}END{for(ip in count){print ip”\t”count[ip]}}’|sort -rnk 2

 

Count the response status codes of nginx

cat access.log|awk ‘{count[$9]++}END{for(ip in count){print ip,count[ip]}}’ #Number of status codes

cat access.log|awk ‘{count[$9]++}END{for(status in count){print status,count[status]/NR*100″%”}}’ #Proportion statistics

cat access.log|awk ‘{count[$9]++}END{for(status in count){print status”\t”int(count[status]/NR*100)”%”}}’ #proportion statistics reserved integer

 

According to ua statistics

cat access.log|awk -F'”‘ ‘{print $(NF-1)}’

cat access.log|awk -F'”‘ ‘{count[$(NF-1)]++}END{for(ua in count){print ua,count[ua]}}’

 

According to time statistics, count the number of visits per minute and the number of visits per second

cat access.log |awk ‘{print $4}’|awk -F’:’ ‘{print $1″:”$2″:”$3}’|awk ‘{count[$1]++}END{for(time in count){print time,count[time]}}’ #Count the number of requests per minute

cat access.log|awk ‘{count[$4]++}END{ for(time in count){print time,count[time]} }’ #Request per second, concurrent

 

nginx log filtering

cat access.log|awk ‘$9~/^2/’ #Status code, normal request

cat access.log|awk ‘$9~/^5/’ #Status code, handle exception

cat access.log |awk -F'”‘ ‘$(NF-1) ~ /iPhone/’ #Filter ua containing iphone

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *