Common commands for nginx log statistics

Keywords: PHP less Nginx

IP related statistics

  • Statistics of IP access (number of independent IP access)
awk '{print $1}' access.log | sort -n | uniq | wc -l
  • View the IP access of a certain period of time (4-5 points)
grep "07/Apr/2017:0[4-5]" access.log | awk '{print $1}' | sort | uniq -c| sort -nr | wc -l  
  • View the top 100 IP S with the most frequent access
awk '{print $1}' access.log | sort -n |uniq -c | sort -rn | head -n 100
  • View IP with more than 100 accesses
awk '{print $1}' access.log | sort -n |uniq -c |awk '{if($1 >100) print $0}'|sort -rn
  • Query the detailed access status of an IP, and sort by access frequency
grep '127.0.01' access.log |awk '{print $7}'|sort |uniq -c |sort -rn |head -n 100

Page access statistics

  • View the most frequently visited pages (top 100)
awk '{print $7}' access.log | sort |uniq -c | sort -rn | head -n 100

View the most frequently visited page ([exclude php page] (TOP100)

grep -v ".php"  access.log | awk '{print $7}' | sort |uniq -c | sort -rn | head -n 100 

View pages visited more than 100 times

cat access.log | cut -d ' ' -f 7 | sort |uniq -c | awk '{if ($1 > 100) print $0}' | less
  • View the last 1000 records and the most visited page
tail -1000 access.log |awk '{print $7}'|sort|uniq -c|sort -nr|less

Requests per second statistics

  • Count the number of requests per second, the time point of top100 (accurate to seconds)
awk '{print $4}' access.log |cut -c 14-21|sort|uniq -c|sort -nr|head -n 100

Statistics of requests per minute

  • Count the number of requests per minute, the time point of top100 (accurate to minutes)
awk '{print $4}' access.log |cut -c 14-18|sort|uniq -c|sort -nr|head -n 100

Hourly request statistics

  • Count the number of requests per hour, the time point of top100 (accurate to hours)
awk '{print $4}' access.log |cut -c 14-15|sort|uniq -c|sort -nr|head -n 100

performance analysis
Add $request? Time to the last field in nginx log

  • List pages with a transfer time of more than 3 seconds, displaying the top 20
cat access.log|awk '($NF > 3){print $7}'|sort -n|uniq -c|sort -nr|head -20
  • List the pages with php page request time more than 3 seconds, and count the number of times they appear, showing the top 100
cat access.log|awk '($NF > 1 &&  $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

Spider grabbing statistics

  • Count the number of spider grabs
grep 'Baiduspider' access.log |wc -l
  • Count the number of times a spider grabs 404
grep 'Baiduspider' access.log |grep '404' | wc -l

TCP connection statistics

  • View the current number of TCP connections
netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l
  • Sniff the access of port 80 with tcpdump to see who is the highest
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr

Posted by TomT on Tue, 31 Mar 2020 12:10:34 -0700