Troubleshooting guide
500 Internal Server Error: how to find the cause
A 500 is your application saying "something went wrong and I did not handle it". The answer is almost always an exception you can find in seconds with the right tooling.
3 min read9 sectionsUpdated
On this page
Usual causes
- Unhandled exceptions: null references, type errors, unexpected input.
- Database errors: failed migrations, constraint violations, connection limits.
- Configuration: a missing environment variable or secret after a deploy.
- Dependency failures that the code does not handle: a timeout from a payment API bubbling up as an exception.
- Permissions: the app cannot read a file or write to a directory.
- Resource limits: out of memory, too many open files.
Finding the exception
- Get the time, route and request ID. Good error pages show a request ID the user can quote.
- Search logs for that request ID or for ERROR-level lines on that route in that minute.
- Open the trace. The failing span carries the exception type, message and stack trace.
- Check the error group: is this new since the last release, or old and rising?
# Plain logs on a server
journalctl -u myapp --since "15 min ago" | grep -iE 'error|exception|traceback' | tail -n 40
# Docker
docker logs --since 15m myapp 2>&1 | grep -iE 'error|exception' | tail -n 40
# Kubernetes (including the previous container if it crashed)
kubectl logs deploy/myapp --since=15m | grep -iE 'error|exception' | tail -n 40
kubectl logs <pod> --previous | tail -n 40Handle errors deliberately
Not every failure should be a 500. Return the right code so clients and monitoring can react properly:
| Situation | Better response |
|---|---|
| Invalid input | 400 or 422 with a clear message |
| Not signed in / not allowed | 401 / 403 |
| Resource missing | 404 |
| Dependency down, try later | 503 with Retry-After |
| Dependency too slow | 504 from the gateway, or a fast fallback |
| Genuine bug | 500, logged with stack trace and request ID |
Preventing repeats
- Error tracking that groups exceptions into issues with counts, first and last seen, affected endpoints and versions. In VitalRix error tracking, issues can be assigned, resolved, and come back as regressed if they reappear.
- Alert on error rate, not on single errors, per service or endpoint.
- Deploy markers and canary releases, so a bad release is spotted on a small share of traffic. See canary deployments.
- Startup checks that fail fast when required configuration is missing, instead of throwing on the first request.
How do I see the error behind a 500?
Check the application logs or your error tracker for that time and route. If your service is traced, the failing span holds the exception and stack trace.
Is a 500 bad for SEO?
Occasional 500s are not a problem. Persistent 500s on a page can lead search engines to drop it. Monitor important pages so you hear about it first.
Check it now, free
- Website down checker: Is it down for everyone or just me? Check any site now.
Related reading
- 400 Bad Request: what it means and how to fix it: Fix a 400 Bad Request error: oversized cookies, malformed URLs, invalid JSON bodies and proxy rules, with commands to find which layer rejected it.
- 422 Unprocessable Entity: how to fix it: Fix 422 Unprocessable Entity (Unprocessable Content): validation errors in APIs and forms, CSRF tokens in Rails and Laravel, and how to read the error body.
- 501 Not Implemented: how to fix it: Fix 501 Not Implemented: unsupported HTTP methods, servers and proxies that do not support PATCH or WebDAV, and how it differs from 405 and 500.
- Cloudflare error 521: web server is down: Fix Cloudflare error 521 "Web server is down": the origin refused the connection. Check the web server, firewall rules for Cloudflare IPs and DNS.
- 408 Request Timeout: causes and how to fix it: Understand 408 Request Timeout: slow client uploads, idle keep-alive connections and proxy settings, and how it differs from a 504 Gateway Timeout.
- Cloudflare error 520: web server returns an unknown error: Fix Cloudflare error 520 "Web server returns an unknown error": empty or reset origin responses, crashes, oversized headers and keep-alive mismatches.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.