A 500 error is never one error. It is a symptom, and the cause could be a crashed PHP worker, a dead database connection, a full disk, or a bad deploy. The Nginx error log records the cause, but only if you are watching it. This guide shows how to monitor nginx error logs and get alerts when 5xx errors spike, so you find out from your own monitoring instead of from a visitor.
Most small servers already collect the data. Nginx writes an error log, usually at /var/log/nginx/error.log, and an access log that records the status code of every response. The gap is not data. It is attention. Nobody reads the error log until the site is down, and by then the log has the answer and the visitors have the complaint. Monitoring closes that gap with a small amount of automation.
Monitor Nginx Error Logs: Know Where They Live
Before you monitor anything, find the logs. The error log path is set in the Nginx configuration, usually in the main nginx.conf or in a server block. The default is /var/log/nginx/error.log, and the default level is error, which records errors but not warnings or notices. If you are debugging a specific problem, raise the level to warn temporarily, then put it back. A warn-level log on a busy site grows fast and buries the real errors in noise.
The access log is the other half. Its default path is /var/log/nginx/access.log, and each line records the request, the status code, and the response time. The access log is where you count 5xx responses, because it sees every request including the ones that succeeded. The error log is where you find the reason: the PHP error, the upstream timeout, the missing file. You need both, and the guide to Nginx access and error logs explains the format of each line so you can read them without guessing.
Check that log rotation works. Logrotate should compress and delete old Nginx logs on a schedule, or the logs will fill the disk on a busy site. A monitoring setup that dies because the disk filled with the logs it was supposed to watch is a failure worth preventing in advance.
Counting 5xx Errors from the Access Log
A 5xx spike is the signal that matters. A single 500 error can be a visitor hitting a broken link at the wrong moment. Fifty in a minute means the site is broken. Count them from the access log with a simple command: grep the log for status codes starting with 5, count the lines, and compare with the previous minute. That count is the metric your alerts will use, whether you compute it with a shell script, a log shipper, or a monitoring tool.
Separate the 5xx codes, because they mean different things. A 500 means the application failed. A 502 means Nginx could not reach the upstream, usually PHP-FPM or the app server. A 503 means the upstream is unavailable or overloaded. A 504 means the upstream timed out. Counting them together tells you the site is broken. Counting them separately tells you where to look first. The guide to fixing 502 Bad Gateway errors covers the most common 5xx of all, and it is the first page to open when the 502 count spikes.
Establish a baseline before you set thresholds. Watch the 5xx count for a week and note what normal looks like. A quiet blog might see zero 5xx responses on a normal day, so any spike is an alert. A busy app might see a handful an hour from bots hitting broken endpoints, so the threshold needs to sit above that noise. An alert threshold copied from someone else’s server is a guess. Your own baseline is a measurement.
Setting Up Alerts on 5xx Spikes
A nginx log alerts setup for nginx 5xx alerts needs three pieces: something that counts, something that decides, and something that notifies. The counter can be a shell script run by cron every minute, a Promtail query feeding Loki, or a metrics exporter feeding Prometheus. The decision is a threshold with a duration, such as more than 20 5xx responses in five minutes. The notification is an email or a webhook to a channel you actually read.
The duration matters as much as the threshold. Alerting on a single minute of errors catches every brief blip, including a deploy restart that produces ten seconds of 502s. Requiring the spike to last five minutes filters out the noise and still catches a real outage quickly. Five minutes of a broken site is a fast alert by any standard, and it is slow enough to ignore a restart.
Test the alert before you trust it. Trigger a 500 on purpose, for example by stopping PHP-FPM for a minute on a staging site, and confirm the alert arrives. An alert you have never seen fire is an alert you cannot trust, and the time to discover that is not during a real outage. Test it monthly, or at least after every change to the monitoring setup.
Reading the Error Log When an Alert Fires
An alert tells you something is wrong. The error log tells you what. When a 5xx alert fires, open the error log for the same minute and look for the pattern. A line about a connection refused to the PHP socket means PHP-FPM is down. A line about an upstream timeout means the application is slow, not dead. A line about no space left on the device means the disk is full, and the fix is deleting files, not restarting Nginx.
Correlate the error log with the access log. Find the request that returned the 500, then find the error line with the same timestamp. That pair, one access line and one error line, usually names the cause directly. The Fail2ban setup guide for SSH and Nginx is worth reading alongside this, because a flood of bad requests can look like an application failure until you see the source addresses in the access log.
Keep the investigation short. The goal in the first five minutes is to name the broken piece, not to understand the whole system. Once the site is back, read the logs at leisure and fix the root cause. A restart that clears the symptom without fixing the cause guarantees a repeat, usually at a worse hour.
Tools That Watch Nginx Logs for You
You do not need a heavy platform for this. A cron script that counts 5xx codes and sends an email is enough for one VPS, and it has no dependencies to maintain. Loki and Grafana give you graphs and history for the same data, and the Prometheus and Grafana setup guide for a 2 GB VPS covers the metrics side if you want dashboards alongside the logs. Uptime monitoring from outside, such as the setup in the Uptime Kuma setup guide, tells you the site is down from a visitor’s point of view, which is the alert your log monitoring cannot give you when the whole server is unreachable.
Pick the lightest tool that does the job and stick with it. A monitoring stack that needs more care than the site it watches is a liability. For one VPS, a cron counter plus an uptime check covers the failures that matter. Add the heavier tools when you have more servers, not before.
Common Mistakes in Nginx Log Monitoring
The most common mistake is alerting on the error log alone. The error log records causes, not counts, and a single recurring error can fill it while the site works fine. Count 5xx responses in the access log for the alert, and use the error log for the diagnosis. The second mistake is a threshold with no duration, which pages you for every deploy restart. The third is an alert that goes to an inbox nobody reads. Send it to the channel you check every day, and keep the alert count low enough that you read every one.
The fourth mistake is never testing the alert. The fifth is monitoring the logs but not the disk: a full disk stops Nginx from writing logs at all, and a monitoring setup that watches logs cannot see a disk that is already full. The guide to monitoring disk, RAM, and CPU usage covers that half of the job, and it belongs next to log monitoring in your routine.
Set it up today: find the log paths, count the 5xx responses for a week, set a threshold with a five-minute duration, and test the alert on purpose. A nginx error log monitoring setup that pages you five minutes into an outage is worth more than a dashboard you open after a visitor complains, and it takes an afternoon to build.