Simple Script to Monitor Nginx HTTP Codes

Nginx[1] is a popular reverse proxy and a load balancer tool used in the web industry. There are multiple UI based tools to monitor the performance of the Nginx server and the health of the underline servers to which nginx is serving traffic. Following is a simple shell script that I wrote for monitoring the status of nginx upstreams by looking at the nginx access logs.


#!/bin/bashtail_Command="tail "
array=()
while IFS= read -r -d $'\0'; do
array+=("$REPLY")
done < <(find /path/to/nginx/access/log/root -type f -name "*access.log" -print0)
for i in "${array[@]}"
do
echo "===============================================================================================\n"
echo "LOG FILE: $i"
echo "===============================================================================================\n"
cat "$i" | cut -d '"' -f3 | cut -d ' ' -f2 | sort | uniq -c | sort -rn
echo "-----------------------------------------------------------------------------------------------\n"
tail_Command+=" -f $i"
done
#echo "$tail_Command"
tail_Command+=' | grep --color -e "HTTP/1\.1\" 5.." -e "HTTP/1\.1\" 4.."'
eval "$tail_Command"

You have to execute this inside the nginx log root directory and it will first find all the access log files, print the count of different http status codes found in each log and multi-tail all the logs for 4XX and 5XX errors.

Cheers..!

[1] https://www.nginx.com/resources/glossary/nginx/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store