Nginx Log Parser and 5xx Error Alerting via Discord Webhook: Setup Guide


Quick answer: An Nginx log parser reads the access log, pulls the response status from each request line, and matches the 5xx codes (500, 502, 503, 504). A short script formats those matches and posts them as JSON to a Discord webhook URL. The reliable sequence is: confirm the log format, prove you can isolate 5xx lines, build a small payload, post it, and only then schedule it.

This topic came from a public technical discussion signal about implementing an Nginx log parser with 5xx error alerting through a Discord webhook. I have rewritten it as an original, evidence-first walkthrough: the point is to explain the moving parts and the places they break, not to copy the original thread.

Applies To

Nginx access and error logs, log parsing scripts, 5xx monitoring, Discord webhooks, and the automation layers around them: n8n, ClickUp, Lovable, webhook pipelines, internal tools, API integrations, and AI-assisted operations.

Symptoms

  • The parser runs without error but produces no matches, even while the site is clearly returning 5xx responses.
  • Alerts cover the wrong time window, the wrong virtual host, or a single upstream instead of the whole service.
  • The webhook call returns an HTTP error, or reports success while nothing appears in the Discord channel.
  • The same failing request keeps firing the alert, so a new incident is hard to spot among the repeats.

Most Common Causes

  • The parser assumes the wrong log format. The default combined format puts the status code in a fixed field, but a custom log_format directive can move it.
  • Nginx writes to a different file than the script reads, because of a custom access_log path, a per-server block, or a container filesystem.
  • The JSON payload is malformed, or it exceeds Discord’s 2000-character content limit.
  • The webhook URL was revoked or rotated, or the script reads it from the wrong environment.
  • A timer runs more often than the alert window and re-sends the same lines, which looks like a flood but is only duplicate reporting.

Step-by-Step Fix

1. Confirm the access log format

Match the parser to the format Nginx actually writes. The default combined format places the status immediately after the request line, in the ninth space-separated field. If log_format has been customized, count the fields before you trust a hard-coded position.

log_format combined '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';

2. Isolate 5xx responses

Prove the filter works on the command line before you wrap it in a script. Matching the status field directly avoids false hits on a 5xx number that appears inside a URL.

awk '$9 ~ /^5[0-9][0-9]$/ {print $0}' /var/log/nginx/access.log
grep -E '" 5[0-9][0-9] ' /var/log/nginx/access.log

If you need per-site or per-upstream detail, keep the fields that identify them, such as the virtual host or upstream address, so an alert points at the right target.

3. Build a small alert payload

Keep each message short and specific: host, time window, count, and a few example statuses. A concise payload is easier to read in a channel and stays well under Discord’s 2000-character content limit.

{"content":"[web-01] 5xx spike: 12 responses in 5 min (500 x8, 502 x3, 504 x1)"}

Discord accepts a JSON body with a content field, and optionally an embeds array for structured detail. Keep the webhook URL in an environment variable or secret store rather than hard-coding it in the script.

4. Post to the Discord webhook

Send a test message first, so you can separate webhook problems from parsing problems.

export DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..."
curl -sS -X POST "$DISCORD_WEBHOOK_URL" \
  -H "Content-Type: application/json" \
  -d '{"content":"test alert from nginx parser"}'

A successful post returns 204 No Content. A 400 usually means the JSON is malformed, 401 or 404 usually means the webhook URL is wrong or revoked, and 429 means the script is posting faster than the rate limit allows.

5. Schedule it and control noise

Run the parser on a timer, remember the last offset you processed, and alert on a threshold such as “N 5xx responses in five minutes,” rather than one message per line. If a change touches Nginx configuration, validate and reload instead of restarting:

nginx -t
systemctl reload nginx

Verification

  • Run the parser against a log slice that you know contains 5xx responses, and confirm the count matches.
  • Send one test payload and confirm it lands in the intended Discord channel.
  • Trigger a controlled 5xx response and confirm the alert reproduces it with the right host and window.
  • Watch one full alert interval to confirm the same lines are not reported twice.

Production Notes

On a production system, keep the webhook URL out of version control and out of the script body. Save the previous parser and configuration, change one thing at a time, and keep a rollback path. Because an alert is only as good as the window it covers, decide up front how often the parser runs and what threshold counts as an incident; otherwise a legitimate 5xx burst and a duplicate re-send look identical in the channel.

FAQ

Should I parse the access log or the error log?

Both have a role. The access log records the status code returned for each request, which is what you count for 5xx alerting. The error log holds the reason behind many of those responses, so use it to explain an alert rather than to detect one.

Why does the webhook return success but nothing shows up?

Check that the URL points at the channel you expect, that the JSON uses the field names Discord expects, and that the message is not empty. A revoked or regenerated webhook usually fails outright, so silent delivery more often points to the wrong channel or a malformed body.

How do I stop the same 5xx from alerting every minute?

Track the last processed position in the log and count only new lines, then alert on a threshold over a rolling window. That turns a repeating incident into a single message instead of one per run.

References