---
title: "Caddy serves index.html instead of 404"
category: "code"
id: 439478d4-8867-42af-9f62-829a7b475a3a
canonical: https://solvedlist.com/solutions/caddy-serves-index-html-instead-of-404
---

# Caddy serves index.html instead of 404

> Fix for Caddy misrouting API and missing assets to index.html by proper handle block ordering and matchers.

## Question

Caddy serves index.html for API routes and missing assets instead of 404. How to order handle blocks?

## Solution

Symptom: behind Caddy the SPA works, but `/api/...` returns the HTML shell, `/sitemap.xml` and `/llms.txt` arrive as HTML, and a missing build file answers 200.

Cause: a catch-all `try_files {path} /index.html` rewrites every unmatched request to the shell and `file_server` serves it with status 200. Do not reason about source order: the Caddyfile adapter sorts `handle` routes by matcher specificity, so a block without a matcher always becomes the fallback, wherever it is written.

Fix, one backend matcher, guarded assets, fallback last:
```
example.com {
root * /srv/app/dist
@backend path /api/* /sitemap.xml /llms.txt /robots.txt
handle @backend {
reverse_proxy localhost:8080
}
@brokenAsset {
path *.js *.css *.map *.ico *.woff2
not file {path}
}
handle @brokenAsset {
respond 404
}
handle {
try_files {path} /index.html
file_server
}
}
```
Client-side routes still return the SPA with 200, which is intended for a single-page app. The fix makes missing build files and unknown backend paths return a real 404 instead of the shell.

Verify: `caddy validate --config /etc/caddy/Caddyfile`, then `curl -sI https://example.com/api/nope | head -1` (404 from the backend), `/assets/app.js` (200), `/typo.js` (404), and `curl -s https://example.com/sitemap.xml | head -2` (XML).

## Query directly via MCP

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}
```

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
