BASH

Analyze Nginx Access Logs for Errors

Quickly parse Nginx access logs using bash, grep, and awk. Identify 4xx/5xx errors or specific request patterns, crucial for website debugging and performance monitoring.

#!/bin/bash

LOG_FILE="/var/log/nginx/access.log"

echo "Top 10 IP addresses with 4xx/5xx errors:"
grep '" (4|5)[0-9]{2}' "$LOG_FILE" | awk '{print $1}' | sort | uniq -c | sort -nr | head -n 10

echo "
Top 10 most requested URLs (excluding images/css/js):"
grep -v -E "(\.jpg|\.png|\.gif|\.css|\.js|\.ico|\.woff)" "$LOG_FILE" | awk '{print $7}' | sort | uniq -c | sort -nr | head -n 10
How it works: This script analyzes a specified Nginx access log file. It uses `grep` to filter lines containing 4xx or 5xx HTTP status codes and then `awk`, `sort`, `uniq -c`, and `head` to find the top 10 IP addresses with errors. It also demonstrates how to find the top 10 most requested URLs, excluding common static assets.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs