A simple shell script to analyze an Nginx access log file from the command line.
This project extracts and displays:
- Top 5 IP addresses with the most requests
- Top 5 most requested paths
- Top 5 response status codes
- Top 5 user agents
Project URL: https://roadmap.sh/projects/nginx-log-analyser
nginx-log-analyser.sh— main shell scriptnginx-access.log— sample Nginx access log file
-
Bash shell
-
Standard Unix command-line tools:
awksortuniqheadsed
-
Clone the repository:
git clone <your-repository-url> cd nginx-log-analyser
-
Make the script executable:
chmod +x nginx-log-analyser.sh
-
Run the script:
./nginx-log-analyser.sh nginx-access.log
Top 5 IP addresses with the most requests:
45.76.135.253 - 1000 requests
142.93.143.8 - 600 requests
Top 5 most requested paths:
/api/v1/users - 1000 requests
/api/v1/products - 600 requests
Top 5 response status codes:
200 - 1000 requests
404 - 600 requests
Top 5 user agents:
Mozilla/5.0 (...) - 400 requests
curl/7.68.0 - 200 requestsThe script uses shell utilities to parse the Nginx access log:
awkto extract specific fields such as IP addresses, request paths, status codes, and user agentssortanduniq -cto count repeated valuessort -nrto rank values by frequencyhead -5to display the top 5 results
- The script expects the log file path as a command-line argument.
- If the file does not exist, the script exits with an error message.