HTTP handlers are the core components that process requests in Caddy. They form middleware chains that can modify requests, generate responses, or perform side effects.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/caddyserver/caddy/llms.txt
Use this file to discover all available pages before exploring further.
Handler Interface
All HTTP handlers in Caddy implement theHandler interface:
http.Handler, Caddy’s handler can return an error. This allows proper error propagation through the middleware chain.
Middleware Handler
Most handlers are middleware, implementing theMiddlewareHandler interface:
next.ServeHTTP(w, r) to propagate the request.
Handler Types
Middleware Handlers
These handlers modify the request/response but typically call the next handler:- headers - Manipulates HTTP headers
- rewrite - Rewrites request URI and method
- encode - Compresses response bodies
- templates - Executes response as Go template
Terminal Handlers
These handlers generate responses and typically don’t call the next handler:- file_server - Serves static files
- reverse_proxy - Proxies requests to upstream servers
- respond - Writes static responses
Handler Execution
Request Flow
Requests flow down the handler list (top to bottom):Response Flow
Responses flow up the handler list (bottom to top):file_serverwrites the filetemplatesexecutes the templateencodecompresses the output
Handler order matters! Place response transformers (encode, templates) before content originators (file_server, reverse_proxy).
Common Handlers
authentication
Module ID:http.handlers.authentication
Provides HTTP authentication (Basic Auth, etc.)
encode
Module ID:http.handlers.encode
Compresses response bodies with gzip, zstd, or brotli.
error
Module ID:http.handlers.error
Triggers an error with a specified status code.
file_server
Module ID:http.handlers.file_server
Serves static files from the filesystem. See File Server for details.
headers
Module ID:http.handlers.headers
Manipulates request and response headers. See Headers for details.
map
Module ID:http.handlers.map
Maps input values to output values, storing the result in a placeholder.
push
Module ID:http.handlers.push
Implements HTTP/2 server push.
request_body
Module ID:http.handlers.request_body
Manipulates the request body (size limits, etc.).
respond
Module ID:http.handlers.respond
Writes a hard-coded/static response.
reverse_proxy
Module ID:http.handlers.reverse_proxy
Proxies requests to upstream servers with load balancing and health checks. See Reverse Proxy for details.
rewrite
Module ID:http.handlers.rewrite
Rewrites the request URI, method, and query string. See Rewrite for details.
subroute
Module ID:http.handlers.subroute
Groups routes together under common configuration.
templates
Module ID:http.handlers.templates
Executes response bodies as Go templates. See Templates for details.
tracing
Module ID:http.handlers.tracing
Integrates with OpenTelemetry tracing.
vars
Module ID:http.handlers.vars
Sets arbitrary variables on the request.
Handler Configuration
Handlers are configured in routes with thehandle directive:
Error Handling
Handlers can return errors to trigger the error handling chain:HandlerError values for proper status code handling:
Response Handlers
Thereverse_proxy and some other handlers support handle_response routes that execute based on upstream response properties:
Custom Handlers
To create a custom handler, implement the interfaces and register the module:Handlers which act as responders (content origins) need not invoke the next handler, since they write the final response.