# HTTP 500 Internal Server Error — Meaning, Causes & Fixes

> The server hit an unexpected condition and couldn't complete the request.

500 is the catch-all server failure: something went wrong that the server didn't anticipate, usually an unhandled exception. The client did nothing wrong — or nothing the server could name.

The details belong in the server's logs, not in the response: a stack trace on a 500 page shows attackers your internals.

- Class: 5xx Server Error
- Retry: With backoff (idempotent requests only)
- Cacheable by default: no
- Defined in: [RFC 9110 §15.6.1 — 500 Internal Server Error](https://www.rfc-editor.org/rfc/rfc9110.html#section-15.6.1)

## Example response

```http
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
Content-Length: 33

{"error":"Internal Server Error"}
```

## Common causes

- An unhandled exception or crash in application code.
- Misconfiguration: a missing environment variable or secret, a syntax error in `.htaccess`.
- Database or dependency errors the code didn't handle.
- Running out of memory, or a function crashing on startup.

## How to fix it

1. Check the server logs first — the stack trace points at the line.
2. Reproduce locally with the same input and environment variables.
3. Catch expected failures and map them to specific statuses (400, 404, 503), so 500 means 'genuinely unexpected'.
4. Show clients a generic message plus a request id, and log the details against that id.

## Send a 500 from your server

### Express

```
res.status(500).json({ error: "Internal Server Error" });
```

### Next.js route handler

```
return Response.json({ error: "Internal Server Error" }, { status: 500 });
```

### Flask

```
return {"error": "Internal Server Error"}, 500
```

### Go net/http

```
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
```

### nginx

```
return 500;
```

## FAQ

### Is a 500 my fault as a visitor?

Usually not — the server failed. Retrying later may work; if it keeps happening, tell the site what you were doing and when.

### 500 vs 502 vs 503?

500: the application itself failed. 502: a proxy got a bad response from the application. 503: the server is temporarily unable to serve — overloaded or in maintenance.

## Related status codes

- [502 Bad Gateway](https://www.devkult.com/http/502.md)
- [503 Service Unavailable](https://www.devkult.com/http/503.md)
- [504 Gateway Timeout](https://www.devkult.com/http/504.md)

Open the interactive page: https://www.devkult.com/http/500