Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Requests count per IP per request in Nginx access.log

Tags:

unix

nginx

devops

I have unix server running nginx Debian GNU/Linux 9

I am trying to get requests count per IP per request in Nginx access.log file for analysis, so I have 2 questions:

  1. is there a unix command to fetch this info from the log file (the whole file), per ip per request?
  2. also is it possible to filter it by date?, i mean get the requests count per IP per request in a certain day. so hopefully i get something like this
IP        count
127.0.0.1 4
127.0.0.2 5
127.0.0.3 6

i found this, but it just counts ips

sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
like image 493
Ahmed Gamal Avatar asked Oct 26 '25 16:10

Ahmed Gamal


1 Answers

Yes, you can use grep in the first command :

grep '12/Jan/2021' access.log | awk '{print $1}' | sort | uniq -c | sort -nr

and for certain date using extended regular expression :

grep -E '11/Jan/2021|12/Jan/2021' access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20
like image 148
NeverStopLearning Avatar answered Oct 29 '25 12:10

NeverStopLearning