2020-01-24 12:47:52 -07:00
---
title: Caddyfile Directives
---
2021-08-31 14:50:01 -04:00
< style >
#directive-table table {
margin: 0 auto;
overflow: hidden;
}
#directive-table tr:hover {
2022-09-06 13:57:07 -06:00
background: rgba(109, 226, 255, 0.11);
2021-08-31 14:50:01 -04:00
}
#directive-table tr td:first-child {
position: relative;
}
#directive-table a:before {
content: '';
position: absolute;
left: 0;
top: 0;
bottom: 0;
display: block;
width: 100vw;
}
< / style >
2020-01-24 12:47:52 -07:00
# Caddyfile Directives
2022-09-16 17:11:09 -06:00
Directives are functional keywords that appear within site [blocks ](/docs/caddyfile/concepts#blocks ). Sometimes, they may open blocks of their own which can contain _subdirectives_ , but directives **cannot** be used within other directives unless noted. For example, you can't use `basicauth` inside a `file_server` block, because `file_server` does not know how to do authentication. However, you _may_ use some directives within special directive blocks like `handle` and `route` because they are specifically designed to group HTTP handler directives.
2020-01-24 12:47:52 -07:00
The following directives come standard with Caddy, and can be used in the HTTP Caddyfile:
2021-08-31 14:50:01 -04:00
< div id = "directive-table" >
2020-01-24 12:47:52 -07:00
Directive | Description
----------|------------
2021-02-17 11:34:28 -07:00
**[abort ](/docs/caddyfile/directives/abort )** | Aborts the HTTP request
2020-06-08 12:27:26 -06:00
**[acme_server ](/docs/caddyfile/directives/acme_server )** | An embedded ACME server
2020-01-24 12:47:52 -07:00
**[basicauth ](/docs/caddyfile/directives/basicauth )** | Enforces HTTP Basic Authentication
**[bind ](/docs/caddyfile/directives/bind )** | Customize the server's socket address
**[encode ](/docs/caddyfile/directives/encode )** | Encodes (usually compresses) responses
2021-04-16 13:37:53 -04:00
**[error ](/docs/caddyfile/directives/error )** | Trigger an error
2020-01-24 12:47:52 -07:00
**[file_server ](/docs/caddyfile/directives/file_server )** | Serve files from disk
2022-05-17 14:32:11 -04:00
**[forward_auth ](/docs/caddyfile/directives/forward_auth )** | Delegate authentication to an external service
2020-01-24 12:47:52 -07:00
**[handle ](/docs/caddyfile/directives/handle )** | A mutually-exclusive group of directives
2020-03-24 17:14:09 -06:00
**[handle_errors ](/docs/caddyfile/directives/handle_errors )** | Defines routes for handling errors
2020-06-08 14:56:20 -06:00
**[handle_path ](/docs/caddyfile/directives/handle_path )** | Like handle, but strips path prefix
2020-01-24 12:47:52 -07:00
**[header ](/docs/caddyfile/directives/header )** | Sets or removes response headers
2020-05-15 10:50:57 -06:00
**[import ](/docs/caddyfile/directives/import )** | Include snippets or files
2020-02-28 11:16:45 -07:00
**[log ](/docs/caddyfile/directives/log )** | Enables access/request logging
2020-12-14 18:25:35 -07:00
**[map ](/docs/caddyfile/directives/map )** | Maps an input value to one or more outputs
2022-03-11 16:26:00 -05:00
**[method ](/docs/caddyfile/directives/method )** | Change the HTTP method internally
2020-09-22 22:12:24 -04:00
**[metrics ](/docs/caddyfile/directives/metrics )** | Configures the Prometheus metrics exposition endpoint
2020-01-24 12:47:52 -07:00
**[php_fastcgi ](/docs/caddyfile/directives/php_fastcgi )** | Serve PHP sites over FastCGI
2020-09-25 12:53:11 -06:00
**[push ](/docs/caddyfile/directives/push )** | Push content to the client using HTTP/2 server push
2020-01-24 12:47:52 -07:00
**[redir ](/docs/caddyfile/directives/redir )** | Issues an HTTP redirect to the client
2020-11-19 20:04:39 +01:00
**[request_body ](/docs/caddyfile/directives/request_body )** | Manipulates request body
2020-01-24 12:47:52 -07:00
**[request_header ](/docs/caddyfile/directives/request_header )** | Manipulates request headers
**[respond ](/docs/caddyfile/directives/respond )** | Writes a hard-coded response to the client
**[reverse_proxy ](/docs/caddyfile/directives/reverse_proxy )** | A powerful and extensible reverse proxy
**[rewrite ](/docs/caddyfile/directives/rewrite )** | Rewrites the request internally
**[root ](/docs/caddyfile/directives/root )** | Set the path to the site root
**[route ](/docs/caddyfile/directives/route )** | A group of directives treated literally as single unit
2022-10-17 13:23:48 -04:00
**[skip_log ](/docs/caddyfile/directives/skip_log )** | Skip access logging for matched requests
2020-01-24 12:47:52 -07:00
**[templates ](/docs/caddyfile/directives/templates )** | Execute templates on the response
**[tls ](/docs/caddyfile/directives/tls )** | Customize TLS settings
2022-03-09 18:42:06 +01:00
**[tracing ](/docs/caddyfile/directives/tracing )** | Integration with OpenTelemetry tracing
2020-01-24 12:47:52 -07:00
**[try_files ](/docs/caddyfile/directives/try_files )** | Rewrite that depends on file existence
2020-03-21 21:03:55 -06:00
**[uri ](/docs/caddyfile/directives/uri )** | Manipulate the URI
2022-04-25 12:51:42 -04:00
**[vars ](/docs/caddyfile/directives/vars )** | Set arbitrary variables
2020-01-24 12:47:52 -07:00
2021-08-31 14:50:01 -04:00
< / div >
2020-01-24 12:47:52 -07:00
## Syntax
The syntax of each directive will look something like this:
2020-05-17 16:32:12 -04:00
```caddy-d
2020-01-24 12:47:52 -07:00
directive [< matcher > ] < args... > {
subdirective [< args... > ]
}
```
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
2020-03-22 17:23:50 -06:00
Most---but not all---directives accept [matcher tokens ](/docs/caddyfile/matchers#syntax ), which let you filter requests. Matcher tokens are usually optional. If you see this in a directive's syntax:
2020-01-24 12:47:52 -07:00
2020-05-17 16:32:12 -04:00
```caddy-d
2020-01-24 12:47:52 -07:00
[< matcher > ]
```
2020-02-17 16:25:11 -07:00
then the directive accepts a matcher token, letting you filter which requests the directive applies to.
2020-03-22 17:23:50 -06:00
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 ](/docs/caddyfile/matchers ).
2020-01-24 12:47:52 -07:00
## 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:
2020-05-17 16:32:12 -04:00
```caddy-d
2022-03-09 18:42:06 +01:00
tracing
2020-12-14 18:25:35 -07:00
map
2022-04-25 12:51:42 -04:00
vars
2020-03-30 15:15:23 -06:00
root
2022-10-17 13:23:48 -04:00
skip_log
2020-03-30 15:15:23 -06:00
2020-03-22 16:14:17 -06:00
header
2022-09-16 17:03:41 -06:00
copy_response_headers # only in reverse_proxy's handle_response block
2020-11-19 20:04:39 +01:00
request_body
2020-01-24 12:47:52 -07:00
redir
2022-03-11 16:26:00 -05:00
# incoming request manipulation
method
2021-08-31 13:39:15 -04:00
rewrite
2020-03-21 21:03:55 -06:00
uri
2020-01-24 12:47:52 -07:00
try_files
2021-08-31 13:39:15 -04:00
# middleware handlers; some wrap responses
2020-01-24 12:47:52 -07:00
basicauth
2022-05-17 14:32:11 -04:00
forward_auth
2020-01-24 12:47:52 -07:00
request_header
encode
2021-08-31 13:39:15 -04:00
push
2020-01-24 12:47:52 -07:00
templates
2021-08-31 13:39:15 -04:00
# special routing & dispatching directives
2020-01-24 12:47:52 -07:00
handle
2020-06-08 14:56:20 -06:00
handle_path
2020-01-24 12:47:52 -07:00
route
2021-08-31 13:39:15 -04:00
# handlers that typically respond to requests
abort
error
2022-09-16 17:03:41 -06:00
copy_response # only in reverse_proxy's handle_response block
2020-01-24 12:47:52 -07:00
respond
2020-09-22 22:12:24 -04:00
metrics
2020-01-24 12:47:52 -07:00
reverse_proxy
php_fastcgi
file_server
2020-06-08 14:56:20 -06:00
acme_server
2020-01-24 12:47:52 -07:00
```
2020-09-22 22:12:24 -04:00
You can override/customize this ordering by using the [`order` global option ](/docs/caddyfile/options ) or the [`route` directive ](/docs/caddyfile/directives/route ).