Sorting 404s in Nginx access Logs

The default Nginx logs can be combined and sorted to gather some useful stats.

  1. Make a working directory and copy current logs over (optional):
    mkdir ~/accesslogs
    sudo cp /var/log/nginx/access.log* ~/accesslogs
    cd ~/accesslogs
  2. Extract logs compressed by log rotate:
    ungzip *.gz
  3. Make a big combined log:
    sudo cat access.log* >> log.combined
  4. Parse the combined log and count the 404’s
    sudo awk '($9 ~ /404/)' log.combined | awk '{print $7}' | sort | uniq -c | sort -rn >> 404sC.txt

Leave a Reply

Your email address will not be published. Required fields are marked *