HTTP, TLS & Web Performance

500 Internal Server Error: How to Investigate

Work from the first report to the root cause of a 500 error using logs, recent changes and a few safe, repeatable checks.

Key takeaways

  • A 500 is a failure on the server side, and the response itself rarely says why.
  • Error logs from the web server and the application hold the actual message.
  • Recent changes to code, configuration, permissions or dependencies are the most common triggers.
  • Show visitors a plain error page and keep stack traces and secrets in private logs.
  • After the fix, add a test, an alert or a safeguard so the same failure is caught earlier.

A 500 Internal Server Error means the server hit a problem it could not handle while producing your response. The code is deliberately vague. The real explanation is almost always in a log file on the server, so that is where an investigation starts.

If you are a visitor, there is little to fix: reload later, and tell the site what you were doing. If you run the site, you can usually find the cause quickly by matching the failing request to an error log entry and asking what changed.

What visitors can do

A few harmless steps are worth trying:

  1. Reload after a minute. Some failures are momentary.
  2. If you had just submitted a form or payment, do not repeatedly resend it. Check whether it went through, for example by looking for a confirmation email or your order history.
  3. Try a private window. Occasionally a damaged cookie sends the application down a broken path.
  4. Check the organization's status page or social accounts.
  5. Report it with the address, the time, what you clicked and any reference ID shown.

Your router, DNS settings and antivirus are very unlikely to be involved, because the site's server wrote the error.

First questions for the site owner

Before touching anything, scope the problem:

  • Every page, one section or one action?
  • Every visitor, or only signed-in users, one browser or one region?
  • Constant, or intermittent?
  • When did it start?
  • What changed around then: a release, an update, a configuration edit, a traffic spike, a certificate or password rotation?

The answers cut the search space sharply. A single failing form suggests code or data. Everything failing suggests configuration, a database or the disk.

Reproduce it and find the log line

Make the failure happen once and note the time:

curl -i https://www.example.com/reports

Use curl.exe in Windows PowerShell 5.1. Then read the error log around that moment. Common locations on Linux, which vary by distribution and setup:

Component Typical place to look
nginx /var/log/nginx/error.log
Apache (Debian, Ubuntu) /var/log/apache2/error.log
Apache (Red Hat family) /var/log/httpd/error_log
Services under systemd journalctl -u servicename
Containers docker logs containername
Windows with IIS IIS logs and Windows Event Viewer

Follow a log live while you reproduce:

sudo tail -f /var/log/nginx/error.log

Application frameworks often write their own logs too, inside the project or to a hosted logging service. Managed hosts usually have a log viewer in the control panel. Reading server logs explains the formats.

From error report to fix
1 Scope Which pages, which users, since when. 2 Reproduce Trigger one failure and note the exact time. 3 Read logs Find the first error for that request. 4 Correlate Compare with releases and other changes. 5 Fix or roll back Restore service, then repair properly. 6 Prevent Add a test, alert or guard. Scope Which pages, which users, since when. 1 Reproduce Trigger one failure and note the exact time. 2 Read logs Find the first error for that request. 3 Correlate Compare with releases and other changes. 4 Fix or roll back Restore service, then repair properly. 5 Prevent Add a test, alert or guard. 6

Read from the first error in a burst. Later messages are often consequences. In a stack trace, look for the earliest line that refers to your own code, and note what it was trying to do.

Common causes

Unhandled exceptions. A value was missing, a type was unexpected or a record did not exist, and the code had no path for it.

Configuration errors. A syntax mistake in .htaccess, a missing environment variable or a wrong setting. A bad .htaccess line produces a 500 at once for everything under that directory.

File permissions. The web server's user cannot read a file or write to a cache or upload folder. Resist the urge to open permissions to everyone; grant the specific account the specific access.

Database trouble. The server is down, the password changed, the connection limit is reached or a migration did not run.

Resource limits. The process ran out of memory or exceeded an execution time limit. A full disk causes strange failures, since sessions, logs and temporary files cannot be written.

Dependencies. A library, extension or runtime version changed, or an external API began answering in an unexpected way.

Quick checks on Linux:

df -h          # free disk space
df -i          # free inodes
free -h        # memory
sudo ss -ltnp  # which programs are listening

To test web server configuration syntax:

sudo nginx -t
sudo apachectl configtest

Content management systems and plugins

On a CMS such as WordPress, plugins and themes are frequent culprits, especially after updates. A cautious procedure:

  1. Take a backup before changing anything.
  2. Enable the platform's logging to a private file, not on-screen display.
  3. Check the log for a file path naming a plugin or theme.
  4. Disable that component, often by renaming its folder, and retest.
  5. Update, replace or report it.

If no component is named, disable plugins in halves until the error disappears, then narrow down. Do this on a staging copy where possible.

Intermittent errors

Failures that come and go are harder, and their pattern is the clue.

Reading the pattern of intermittent 500s
Under load Peaks with traffic Suspect memory, workers or database connections Look at resource graphs On a schedule Repeats at set times Suspect backups, batch jobs or log rotation Compare with the task scheduler One in several Roughly a fixed share fails Suspect one bad server behind a balancer Compare versions and config per server Certain data Specific users or records Suspect unexpected values or encoding Test with a copy of that record Under load Peaks with traffic Suspect memory, workers or database connections Look at resource graphs On a schedule Repeats at set times Suspect backups, batch jobs or log rotation Compare with the task scheduler One in several Roughly a fixed share fails Suspect one bad server behind a balancer Compare versions and config per server Certain data Specific users or records Suspect unexpected values or encoding Test with a copy of that record

Adding an identifier to each request, and logging it in every layer, lets you follow one failure across several systems. Logs, metrics and traces describes how these fit together.

Roll back first when it is serious

If a release caused a widespread failure, returning to the previous version is usually the fastest way to help users. Diagnose afterward with the logs you saved. Be careful with database changes: rolling code back does not undo a migration, so check that the old code can work with the new structure.

Avoid changing several things at once under pressure. Make one change, test it and write down what you did.

Handling errors gracefully

Production sites should show a short, friendly page and log the details privately. Detailed error output can reveal file paths, queries, library versions and even credentials.

A useful error response:

  • Sends status 500, not 200.
  • Says something went wrong on the site's side.
  • Gives a reference ID that also appears in the log.
  • Suggests what to do next.
  • Does not depend on the database or other service that may have failed.

APIs should return a consistent, small error object with the same properties.

When the log shows nothing

Sometimes the page fails and the log you are reading stays quiet. Work through these possibilities:

  • Wrong log. Each virtual host or service may write to its own file. Check the configuration for the path.
  • Wrong server. With several machines, the request may have gone elsewhere. Search all of them, or send a test with a unique query string and see which log records it.
  • Another layer answered. A content delivery network or proxy can generate the error before your application is involved. Its own event log is the place to look.
  • Logging cannot write. A full disk or wrong permissions on the log directory silences exactly the messages you need.
  • The level is too high. Warnings may be filtered out. Raise detail temporarily, then restore it.
  • The error is swallowed. Code that catches an exception and returns 500 without recording it leaves no trace. Fix that as part of the repair.

Searching the access log for the status helps you confirm which requests failed and when:

awk '$9 == 500 {print $4, $7}' access.log | tail -20

The field numbers assume the common or combined log format.

A worked example

Say a contact form starts returning 500 on a Tuesday while the rest of the site works. Nothing was deployed. You submit a test message and find a log line saying the application could not connect to the mail service because authentication failed.

The "what changed" question still applies, only the change was outside your code: a mail password had been rotated. Updating the stored secret restores the form. The better repair goes further. The form should save the message before trying to send it, tell the visitor it was received and retry delivery later. An alert on repeated mail failures would have raised the issue before a customer did.

500 and its neighbors

Code Points toward
500 The application or server failed internally
502 A proxy got a bad answer from the server behind it
503 The service says it cannot take requests right now
504 A proxy timed out waiting

If you see 502 or 504, read gateway errors explained, because the search starts at the connection between two servers.

Preventing repeats

After recovery, write down the cause, how it was detected and what would have caught it sooner. Then act on it:

  • Add an automated test for the failing case.
  • Alert on a rise in 5xx rates, and on disk space and memory before they run out.
  • Test releases on a staging system that matches production.
  • Keep a quick rollback method and verified backups.
  • Monitor key pages from outside your own network, as described in uptime monitoring.

Keep the write-up short and focused on the system rather than on whoever made the change. If one mistyped line could take the site down, the useful question is why nothing checked it before it went live.

If you rely on a hosting company for server-level logs, send them the exact time with time zone, the address and your reproduction steps. That turns a vague ticket into one they can search.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in HTTP, TLS & Web Performance →

Latest news on this topic