If your website is getting a lot of spam visitors or malicious traffic, then it is important to get a count of unique IPs from which your website is getting traffic. It is also important to count requests per IP for each of these IPs in NGINX. In this article, we will look at how to count unique IPs & requests per IP in NGINX.
How to Count Unique IPs & Requests per IP in NGINX
It is quite easy to count uniqie IPs & requests per IP in NGINX.
Open terminal and run the following command.
$ sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
You will get a list of IP addresses that send most requests along with the number of requests from each ip (to its left).
205 180.114.210.21 207 161.129.10.34 134 66.249.69.11 129 66.249.69.12 ...
Also read : How to Host Multiple Domains on One NGINX Server
You can use the same trick on Apache server log also.
Once you have identified the suspicious IP addresses, you can list the requested URLs from each IP using grep command as shown below.
$ sudo cat /var/log/nginx/access.log | grep 180.114.210.21
That’s it. It is important to track the most frequently visiting IP addresses and keep an eye on the URLs they are requesting to keep your website safe & secure.
Related posts:
NGINX: How to Fix Upstream Sent too big header Error
NGINX Block URL Access
How to Prevent Direct Access to Images in NGINX
NGINX Prevent Host Header Attack
Difference between $host and $http_host in NGINX
How to Redirect IP to Domain URL in NGINX
How to Force Download File in NGINX
NGINX Catch All Location

Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.