Fix Caddy Reverse Proxy 404 and 502 Errors: Upstream, Path, and Body Limits

What will this help you do?

A step-by-step guide to diagnosing and fixing 404 and 502 errors in Caddy reverse proxy setups by checking logs, upstream addresses, path stripping, and request body limits.

Who is it for?

Maintainers already using Caddy who see a site-wide 502, a subpath 404 or a failed upload.

Before you start

Record the failing URL, method and status. Have the Caddyfile, recent logs and upstream listener address ready; redact secrets before sharing.

Scope

Separate upstream, path and request-limit problems. The isolated case covers only its stated HTTP routes, not your production setup or TLS.

Updated

On this page

Preparation: Verify Environment and Log Access

Before troubleshooting, ensure you have SSH access to the server and can view Caddy logs. Caddy logs are essential for distinguishing between routing errors (404) and upstream failures (502). Use the caddy command or your system's log manager to access recent entries. Confirm that Caddy is running and that your Caddyfile is valid by checking for startup errors. If you cannot access logs, you cannot accurately diagnose whether the issue lies in Caddy's routing logic or the backend service's availability.

Step 1: Distinguish 404 vs 502 via Caddy Logs

Open the Caddy logs and search for the specific error codes. A 502 Bad Gateway typically indicates that Caddy could not connect to the upstream server or received an invalid response. A 404 Not Found usually means the request path did not match any defined handler or the file server could not locate the resource. Look for log entries that mention "upstream" for 502 errors or "no match" or "file not found" for 404 errors. This initial classification prevents you from wasting time on the wrong configuration section. If the logs show connection refused, the issue is likely the upstream service. If they show a path mismatch, focus on your routing directives.

Step 2: Troubleshoot 502 Errors: Upstream Status and Address

For 502 errors, verify that the upstream service is running and listening on the correct address and port. Use ss -tlnp or netstat -tlnp on the server to check which ports are active. Compare this output with the reverse_proxy directive in your Caddyfile. For example, if your Caddyfile specifies reverse_proxy localhost:8080, ensure your application is actually listening on port 8080. If the application is bound to 127.0.0.1 but Caddy is trying to connect via a different interface, or if the port is off by one, you will get a 502. Inspect logs, listening addresses and health checks first; restart only with a diagnosed cause, approved impact window and rollback plan. Also, check if the upstream is returning a valid HTTP response; some applications may crash on specific requests, causing intermittent 502s.

Step 3: Troubleshoot 404 Errors: handle_path and Prefix Stripping

If you are proxying a sub-path (e.g., /api), ensure that the path prefix is correctly handled. The handle_path directive implicitly strips the matched path prefix from the request URI before passing it to the next handler. For instance, if you use handle_path /api/*, a request to /api/users will be forwarded to the upstream as /users. If your upstream application expects the full path /api/users, you should use handle instead of handle_path, or adjust your application's routing. If you are using handle_path but your app returns 404, it is likely because the app does not recognize the stripped path. Test this by checking the upstream logs to see what path it received.

Step 4: Check File Server and Root Directory Configuration

For static files, ensure the file_server directive is paired with the correct root directive. The file_server appends the request's URI path to the site's root path. If your root is set to /var/www/html and you request /images/logo.png, Caddy will look for /var/www/html/images/logo.png. If the file does not exist at that exact location, you will get a 404. Verify the file permissions and ownership. Also, note that file_server enforces canonical URIs; requests to directories without a trailing slash will be redirected. If you are seeing 404s for static assets, double-check the root path and the actual file location on the disk.

Step 5: Separate 413 from 404 and 502

The request_body max_size directive configures an explicit body limit; do not infer the claimed default Caddy upload restriction. Exceeding a configured limit produces 413. An upload failure alone does not establish the cause of a 404 or 502. Record the actual response status and correlate Caddy, other proxy and application logs before changing the relevant limit. Do not increase limits blindly. The experimental set subdirective requires v2.10.0+; do not copy it into older installations. This guide offers no unverified capacity-change instruction. See the official request_body reference.

Verification and Rollback: Test Access and Hot Reload

Back up the configuration and run caddy validate against the complete intended configuration before an authorized reload. Test the actual failing URL and request method, and compare the expected response body as well as status. A HEAD request may behave differently from GET; one 200 does not establish that every route is repaired. Recheck an unaffected route and recent logs. If the change causes a regression, restore the known-good configuration, validate it and reload through the same reviewed process.

Verifying Upstream and Transport Options

When diagnosing persistent 502 or 404 errors, reviewing transport behaviors and upstream configurations defined in the official Caddyfile reverse_proxy documentation ensures that load balancing, active health checks, and header rewrites operate correctly. For deeper multi-tier architectures, you can also consult Design a Single-Server Setup: Reverse Proxy, App, and Database / 单服务器生产架构怎么画:反向代理、应用、数据库与端口边界 to map out strict port boundaries, or check Troubleshoot 502, 504, and HTTPS Errors Layer by Layer / 502、504 与 HTTPS 故障怎么查:从 DNS 到应用的分层排错 for comprehensive layer-by-layer diagnostics.

Recorded lab case: tell a path 404 from an upstream 502

On 27 October 2026, we tested this distinction in an isolated local Docker Desktop environment: Engine 29.2.1 (Linux arm64) with a cached Caddy 2.11.7 amd64 image. This was a cross-architecture, HTTP-only lab, not a production deployment test. Two independent backends, alpha and beta, served different home pages; beta also served a known /health file. A third container ran the reverse proxy, with only a random host loopback port published. No live AIOOS service was stopped.

The beta site used this routing block. It is a reference fragment from the lab, not a complete production configuration: beta was a name on the lab's private Docker network, and .test was selected through the request Host header rather than public DNS.

caddyfile
http://beta.test:8080 {
    handle_path /api/* {
        reverse_proxy beta:8080
    }
    handle /keep/* {
        reverse_proxy beta:8080
    }
    handle {
        reverse_proxy beta:8080
    }
}
CheckObserved resultWhat it establishes
Request /api/health on beta200, body beta-healthThe /api prefix was removed and the known resource was reached.
Request /keep/health on beta404The preserved path did not exist on this backend; the proxy could still reach the backend.
Stop only the alpha test backendAlpha returned 502; beta still returned 200In this topology, an unavailable upstream affected its own route while the other site remained usable.
Start the same alpha backend againAlpha returned 200Connectivity recovered in this lab without deleting application data.

The important comparison is the same host, path, method and expected response body. A working home page does not prove that /api/health works. If your real application expects /api/health, stripping /api would instead be wrong. Choose handle or handle_path according to the upstream contract, not according to the status code you hope to obtain. A 404 can also originate in the application itself, and a 502 has other possible causes; this experiment does not replace correlating the real request with proxy and application logs.

We also tested configuration handling separately. An unknown directive inside a site block failed validation and was not loaded; the running site remained available. A valid reload added a test response header, and restoring the original configuration removed it while both sites remained usable. This validates the specific lab sequence, not a guarantee of zero downtime for every reload.

For your own test, first record the current configuration and identify a disposable backend. Verify the normal response, change one condition at a time, then verify the exact failed route and an unaffected route after recovery. Do not stop a production backend just to reproduce this case. Back up and validate configuration before an authorized reload; do not delete volumes or recreate containers to diagnose a path mismatch.

Limits: this case did not test public DNS, certificate issuance, authenticated sessions, streaming, real application load or database restoration. The local image architecture differed from the Docker host architecture. Our observations are bounded to the recorded environment. The routing behavior is described in the official handle_path reference; upstream and transport options are covered by reverse_proxy.

Sources

Back up configuration and run caddy validate before reloading. Test the actual failing URL and request method; HEAD returning 405 is not an outage, and a single 200 does not establish that every route is repaired.

Did this guide solve your problem?

Tell us which step blocked you, your version and what happened. Your feedback goes to the editorial queue.

Send feedback or share your experience →

Next steps

Continue with the next useful task; you do not need to read everything at once.

  1. How Caddy Automatic HTTPS Works and How to Troubleshoot It →
  2. Deploy Your First Static Website to a Linux Server with Caddy →
  3. Secure a Linux Server with SSH Keys, UFW and Automatic Updates →
RESOURCES I USE · REFERRAL

Two services to compare when you are ready to launch

This is not an automated ranking, and neither service is necessary for everyone. These are services I use, with the use case and limitations kept visible.

Cloud server · Used for early projects

RainYun

A practical candidate for a website or small service. Choose by user region, configuration and measured workload rather than the lowest headline price.

Referral disclosure: these links contain my referral information. I may receive a platform benefit if you sign up or order, at no additional charge from AIOOS. Check the order page for current pricing, availability, regions and terms. Read the full affiliate disclosure