Extract unique IP address from Apache & Nginx log file

Lets say you wanted to count the number of unique IP addresses hitting your Apache server. It’s very easy to do in a Linux (or compatible) shell. In this tutorial, I’m using Ubuntu server.

First, locate the log file that you want to extract. For example, apache2 log file is located at /var/log/apache2 (depending on your distro). For nginx, the log file is located at /var/log/nginx.

Here I give you the first example on how to extract & count unique IP address in Nginx log file.

Nginx Access Log file

Nginx Error Log file

Next, is the step on how to extract & count unique IP address from Apache log file.
Apache access & error log file

Apache Access Log file

Apache Error Log file

If you have any other step, you can share with me in the comment section. Hope it helps! 🙂

2 thoughts on “Extract unique IP address from Apache & Nginx log file

  1. Gio

    Hi, Thank you very much for this it helped me out a lot, I also added for Apache Access Log file an extra | wc for counting how many unique lines.
    cat access.log | awk ‘{print $1}’ | sort -r | uniq -c | sort -nr | wc

    Thank you hopefully this is useful for someone else also. Thanks again

    Reply

Any Comments?