Find the error behind your server's 500 response

500 Internal Server Error means your server ran into an unexpected condition that stopped it from finishing the request, and it had no more specific status code to send. It's the generic catch-all, so the status alone won't tell you the cause. Your logs will. At the minute the 500 happened, they usually show an unhandled exception, a broken configuration, a process that ran out of memory or a file the server wasn't allowed to read.

What usually causes a 500

  • An unhandled exception

    Your code throws an error that nothing catches, such as a failed database query or a value that turned out empty, and the framework answers with a 500.

  • A broken configuration

    A typo in a server or app config file, or a setting the app expects but can't find after a deploy, fails every request that depends on it.

  • Out of memory

    A process that hits its memory limit partway through a request fails with a fatal error, and the server answers 500. If the operating system kills the process instead, no status gets sent, and the proxy in front usually reports a 502.

  • Wrong file permissions

    The user the web server runs as can't read a script or template it needs, or can't write to a cache or upload folder, so the request stops halfway.

RFC 9110 defines 500 as an unexpected condition that kept the server from fulfilling the request. MDN calls it a generic catch-all and lists improper configuration, out-of-memory issues, unhandled exceptions and improper file permissions among its causes. A proxy that gets an invalid response from the server behind it sends a 502, which RFC 9110 defines separately.

Reproduce the 500 and find it in the access log

# Request the failing page and print the status and how long it took
curl -sS -o /dev/null -w "%{http_code} in %{time_total}s\n" https://yoursite.com/failing-page

# Show the 20 most recent requests that got a 500 (combined log format, status in field 9)
awk '$9 == 500' access.log | tail -n 20

Read the logs at the minute of the 500

  • Take the time from the access log and open your application's error log at that minute. Frameworks log the exception with a stack trace, and the first frames that belong to your own code usually point to the line that failed. If the application logged nothing, check the web server's error log: a 500 can come from the server itself, for example when it can't read a config file or a script.

  • Keep the details in the log and show visitors a plain error page. The OWASP error handling cheat sheet warns that an exception rendered to the user can reveal your technology stack, its versions and the site's installation path. MDN's example 500 page carries a request ID instead, so a visitor's report leads you to the matching log entry.

How to fix a 500 Internal Server Error

  1. Reproduce it and note the timeRequest the failing page yourself and note the exact time, or take the time from a visitor's report. A 500 on every page points to configuration, while a 500 on one page points to that page's code or data.
  2. Find the logged errorOpen the application's error log at that time and find the exception or message. If it's empty, read the web server's error log for the same minute.
  3. Roll back the change that lines upIf the 500s started with a deploy, a config edit or a dependency update, roll it back to get the site working again, then fix the change before you ship it a second time.
  4. Fix the cause and guard against itHandle the failing case in code, correct the configuration or the permissions, or give the process more memory. Then add a test or a startup check for that case, so the next deploy fails before visitors see it.

Frequently asked questions

Is a 500 error my fault or my host's?

Usually it's in your own application or its configuration, since the status means your server hit a condition it didn't expect. If your app logged nothing and the 500s started without any change on your side, check your host's status page and ask them about the server. Either way, MDN notes that a 500 needs the server's owner to investigate, not the visitor.

Do 500 errors affect how Google crawls my site?

Short bursts slow Google's crawling, and pages that keep failing eventually leave the index. Google's HTTP status code guide says it ignores content sent with a 5xx status, lowers the crawl rate in proportion to how many URLs return server errors, and removes URLs that persistently return one.

Why does the 500 happen only for some visitors?

Look at what differs in their requests. A signed-in account with unusual data, a cookie from an older version of the site or a form field your code doesn't expect can each send one request down a path that throws. Search the error log for the request ID or URL from their report rather than trying to reproduce it with your own account.