Introducing a new way to review your PRs.

Read the announcement

basecamp / kamal-proxy

Lightweight proxy server for Kamal

View on GitHub

15 documents · Updated Nov 18, 2026

docs / proxy-error-responses

Proxy error responses

The proxy classifies request, service, and upstream failures before it sends a client-facing response. A shared error middleware then turns the selected status into an embedded or service-specific HTML page while preserving normal upstream responses.

Error response flow

Error classification

The proxy records an error response during request handling instead of writing it at the first failure point. This gives the outer middleware a chance to select the right page and lets nested service pages take priority over the proxy-wide defaults.

When a request moves through the proxy, the response path is:

flowchart LR
    A[Client request] --> B[Router and service]
    B --> C[Request checks]
    C --> D[Target and upstream]
    D --> E{Failure?}
    E -- No --> F[Normal upstream response]
    E -- Yes --> G[Classify status]
    G --> H[Set error response]
    H --> I[Service page or embedded page]
    I --> J[Client response]

SetErrorResponse stores the status and optional template data in the request context. If no error-page middleware is present, it uses the standard plain-text HTTP error instead. A client that disconnects is a special case: the proxy records status 499 for logs but does not try to send a page to a client that is no longer connected.

References: internal/…/error_page_middleware.go• L12-41 internal/…/target.go• L361-399

Error page rendering

ErrorPageMiddleware renders a page after the wrapped handler returns. It sets an HTML content type, writes the selected status, and uses the matching <status>.html template when one exists. A missing or failed template falls back to a simple status heading at the root middleware.

The server-wide middleware uses the embedded default pages. A service can add its own page directory inside that middleware chain. The service page handles a status when it has a matching template; otherwise the outer default page handles it.

References: internal/…/error_page_middleware.go• L43-103 internal/…/server.go• L218-229 internal/…/service.go• L586-609

Failure classification

Request failures

Request failures happen before the upstream can produce a useful response. The proxy maps body limits to 413, body read timeouts to 408, and malformed chunked input to 400. An unexpected request-buffer failure becomes 500.

The same request checks also apply when the target proxy reads the request body. This keeps the client-visible result consistent whether request buffering is enabled at the outer request path or fails during forwarding.

References: internal/…/request_buffer_middleware.go• L23-47 internal/…/target.go• L361-404 internal/…/errors.go• L3-21

Upstream failures

Upstream failures occur while the proxy sends a request or reads the upstream result. A timeout becomes 504, while a general proxy error such as an invalid upstream response becomes 502. The proxy also maps a request that is cancelled during target draining to 504, because the request cannot complete through the retiring target.

An ordinary HTTP response from the upstream is not reclassified. The error handler is used for transport and proxy errors, so valid upstream status codes and bodies continue to the client.

References: internal/…/target.go• L293-304 internal/…/target.go• L361-419

Service availability failures

Service availability failures happen before or instead of target forwarding. The router returns 404 when no service matches the request. The load balancer returns 503 when it cannot claim a target, and a stopped service also returns 503 with an optional message. A pause that exceeds its wait limit returns 504.

The proxy also returns 503 when a request reaches a service over TLS even though that service does not have TLS enabled. These results describe proxy state rather than an upstream response.

References: internal/…/router.go• L136-149 internal/…/load_balancer.go• L174-189 internal/…/service.go• L612-663

Response size failures

Response buffering protects the proxy from sending a response larger than the configured limit. If the buffer exceeds that limit or cannot send its stored data, the middleware selects 500; streaming responses bypass this buffering path.

This failure is distinct from an upstream 502: the upstream may have returned a valid response, but the proxy cannot safely deliver it under its response policy.

References: internal/…/response_buffer_middleware.go• L18-44 internal/…/response_buffer_middleware.go• L55-104 internal/…/target.go• L121-128

Client-facing pages

Embedded default pages

The embedded page set provides the proxy-wide fallback for common error statuses. The pages are static HTML and do not expose upstream response details. The 503 page can also display a service-supplied message when the service is stopped with one.

StatusDefault page purpose
404No service matches the request
413Request content is too large
502The upstream returned an invalid or unusable result
503The service is unavailable
504The upstream or proxy wait exceeded its limit

Statuses without an embedded page use the root middleware's simple status heading. A response that already came from the upstream keeps its own body and content type.

References: internal/…/embed.go• L1-6 internal/…/404.html• L7-11 internal/…/413.html• L7-11 internal/…/502.html• L7-11 internal/…/503.html• L7-11 internal/…/504.html• L7-11

Service-specific pages

A service may point to a directory of HTML pages through its error-page setting. The proxy loads matching status files for that service and places this handler inside the service request path, so a service page can replace the shared page without changing error classification.

The directory must contain parseable HTML templates. A bad directory or invalid template prevents the service middleware from being created. A valid directory that lacks a status file does not hide the outer default page.

References: internal/…/service.go• L101-118 internal/…/service.go• L586-609 internal/…/error_page_middleware.go• L36-47 internal/…/error_page_middleware_test.go• L71-117

Error responses sit at the end of several proxy flows. Read proxy server for the request lifecycle, request routing for service selection, service and target lifecycle for pause and drain behavior, load balancing for target availability, and TLS and certificates for TLS-related service failures.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the status classification in Target.handleProxyError aligned with request-buffer and service-state failures.
  • Keep every embedded status page named with the exact status code that the middleware looks up.
  • When changing nested middleware order, preserve the rule that service pages can override matching proxy-wide pages and that the root handler can still render a fallback.
  • When adding template data to a new status, confirm that both service-specific and embedded-page paths handle the data shape.

Important failure modes

TriggerConsequenceGuard
Error-page templates cannot be parsed at startup or service creationThe affected handler cannot be builtValidate the page directory and template syntax before enabling the service
A status has no matching page in an inner middlewareThe outer page set gets the next chance to render itKeep the root default middleware around the router
A response exceeds the configured buffer limitThe client receives 500 instead of the upstream bodySet a response limit that matches the service contract or disable response buffering for that target
A target is draining when the load balancer claims itThe request receives 503 and no target request startsWait for target health and lifecycle state to settle before routing traffic
The client cancels the requestNo client-facing error page is sent; the proxy records 499Treat the status as a disconnect signal, not as an upstream outage

Key files

FileWhy to read it
internal/…/error_page_middleware.goDefines deferred error selection, template lookup, nesting, and fallback output.
internal/…/target.goDefines upstream error classification and target proxy integration.
internal/…/service.goConnects service state and custom pages to client responses.
internal/…/request_buffer_middleware.goShows request-size, timeout, and malformed-input responses.
internal/…/response_buffer_middleware.goShows response-size protection and its 500 result.
internal/…/server.goShows the proxy-wide placement of the default error middleware.
internal/…/embed.goDefines the embedded default page set.
internal/…/404.html, internal/…/413.html, internal/…/502.html, internal/…/503.html, internal/…/504.htmlShow the client-facing default messages for the main proxy error classes.