Caddyfile Directives

The following directives come standard with Caddy, and can be used in the HTTP Caddyfile:

DirectiveDescription
acme_serverAn embedded ACME server
basicauthEnforces HTTP Basic Authentication
bindCustomize the server’s socket address
encodeEncodes (usually compresses) responses
file_serverServe files from disk
handleA mutually-exclusive group of directives
handle_errorsDefines routes for handling errors
handle_pathLike handle, but strips path prefix
headerSets or removes response headers
importInclude snippets or files
logEnables access/request logging
php_fastcgiServe PHP sites over FastCGI
redirIssues an HTTP redirect to the client
request_headerManipulates request headers
respondWrites a hard-coded response to the client
reverse_proxyA powerful and extensible reverse proxy
rewriteRewrites the request internally
rootSet the path to the site root
routeA group of directives treated literally as single unit
templatesExecute templates on the response
tlsCustomize TLS settings
try_filesRewrite that depends on file existence
uriManipulate the URI

Syntax

The syntax of each directive will look something like this:

  1. directive [<matcher>] <args...> {
  2. subdirective [<args...>]
  3. }

The <carets> indicate tokens to be substituted by actual values.

The[brackets] indicate optional parameters.

The ellipses ... indicates a continuation, i.e. one or more parameters or lines.

Subdirectives are always optional unless documented otherwise, even though they don’t appear in [brackets].

Matchers

Most—-but not all—-directives accept matcher tokens, which let you filter requests. Matcher tokens are usually optional. If you see this in a directive’s syntax:

  1. [<matcher>]

then the directive accepts a matcher token, letting you filter which requests the directive applies to.

Because matcher tokens all work the same, the various possibilities for the matcher token will not be described on every page, to reduce duplication. Instead, refer to the centralized matcher documentation.

Directive order

Many directives manipulate the HTTP handler chain. The order in which those directives are evaluated matters, so a default ordering is hard-coded into Caddy:

  1. root
  2. header
  3. redir
  4. rewrite
  5. uri
  6. try_files
  7. basicauth
  8. request_header
  9. encode
  10. templates
  11. handle
  12. handle_path
  13. route
  14. respond
  15. reverse_proxy
  16. php_fastcgi
  17. file_server
  18. acme_server

You can override/customize this ordering by using the order global option or the route directive.