docs: Add Caddyfile syntax highlighting (#41)

This commit is contained in:
Francis Lavoie 2020-05-17 16:32:12 -04:00 committed by GitHub
parent 75fcfc21bb
commit e0f5ee1bb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 215 additions and 179 deletions

View file

@ -15,7 +15,7 @@ Caddy configuration does not accept plaintext passwords; you MUST hash them befo
## Syntax
```
```caddy-d
basicauth [<matcher>] [<hash_algorithm>] {
<username> <hashed_password_base64> [<salt_base64>]
...
@ -32,7 +32,7 @@ basicauth [<matcher>] [<hash_algorithm>] {
Protect all resources in /secret so only Bob can access them with the password "hiccup":
```
```caddy-d
basicauth /secret/* {
Bob JDJhJDEwJEVCNmdaNEg2Ti5iejRMYkF3MFZhZ3VtV3E1SzBWZEZ5Q3VWc0tzOEJwZE9TaFlZdEVkZDhX
}

View file

@ -11,7 +11,7 @@ Note that binding sites inconsistently may result in unintended consequences. Fo
## Syntax
```
```caddy-d
bind <hosts...>
```
@ -22,12 +22,12 @@ bind <hosts...>
To make a socket accessible only on the current machine, bind to the loopback interface (localhost):
```
```caddy-d
bind 127.0.0.1
```
To include IPv6:
```
```caddy-d
bind 127.0.0.1 ::1
```

View file

@ -8,7 +8,7 @@ Encodes responses using the configured encoding(s). A typical use for encoding i
## Syntax
```
```caddy-d
encode [<matcher>] <formats...> {
gzip [<level>]
zstd
@ -24,13 +24,13 @@ encode [<matcher>] <formats...> {
Enable Gzip compression:
```
```caddy-d
encode gzip
```
Enable Zstandard and Gzip compression:
```
```caddy-d
encode zstd gzip
```

View file

@ -9,7 +9,7 @@ A static file server. It works by appending the request's URI path to the [site'
## Syntax
```
```caddy-d
file_server [<matcher>] [browse] {
root <path>
hide <files...>
@ -29,18 +29,18 @@ file_server [<matcher>] [browse] {
A static file server out of the current directory:
```
```caddy-d
file_server
```
With file listings enabled:
```
```caddy-d
file_server browse
```
Only serve static files out of the `/static` folder:
```
```caddy-d
file_server /static/*
```

View file

@ -10,7 +10,7 @@ The `handle` directive is kind of similar to the `location` directive from nginx
## Syntax
```
```caddy-d
handle [<matcher>] {
<directives...>
}
@ -27,7 +27,7 @@ If you prefer crafting HTTP handler logic in a more inheritence-based way like n
Handle requests in `/foo/` by the static file server, and send all other requests to the reverse proxy:
```
```caddy-d
handle /foo/* {
file_server
}

View file

@ -10,7 +10,7 @@ When the normal HTTP request handlers return an error, normal procesing stops an
## Syntax
```
```caddy-d
handle_errors {
<directives...>
}

View file

@ -11,7 +11,7 @@ By default, header operations are performed immediately unless any of the header
## Syntax
```
```caddy-d
header [<matcher>] [[+|-]<field> [<value>|<find>] [<replace>]] {
<field> <find> <replace>
[+]<field> <value>
@ -33,25 +33,25 @@ For multiple header manipulations, you can open a block and specify one manipula
Set a custom header field on all requests:
```
```caddy-d
header Custom-Header "My value"
```
Strip the "Hidden" header field:
```
```caddy-d
header -Hidden
```
Replace `http://` with `https://` in any Location header:
```
```caddy-d
header Location http:// https://
```
Set security headers on all pages: (**WARNING:** only use if you understand the implications!)
```
```caddy-d
header {
# enable HSTS
Strict-Transport-Security max-age=31536000;
@ -69,7 +69,7 @@ header {
Multiple header directives that are intended to be mutually-exclusive:
```
```caddy-d
route {
header Cache-Control max=age=3600
header /static/* Cache-Control max-age=31536000

View file

@ -10,7 +10,7 @@ This directive is a special case: it is evaluated before the structure is parsed
## Syntax
```
```caddy-d
import <pattern>
```
@ -21,6 +21,6 @@ import <pattern>
Import all files in an adjacent sites-enabled folder:
```
```caddy-d
import sites-enabled/*
```

View file

@ -8,7 +8,7 @@ Enables and configures HTTP request logging (also known as access logs).
## Syntax
```
```caddy-d
log {
output <writer_module> ...
format <encoder_module> ...
@ -28,7 +28,7 @@ The **output** subdirective lets you customize where logs get written. It appear
Standard error (console, default).
```
```caddy-d
output stderr
```
@ -36,7 +36,7 @@ output stderr
Standard output (console).
```
```caddy-d
output stdout
```
@ -44,7 +44,7 @@ output stdout
No output.
```
```caddy-d
output discard
```
@ -52,7 +52,7 @@ output discard
A file. By default, log files are rotated ("rolled") to prevent disk space exhaustion.
```
```caddy-d
output file <filename> {
roll_disabled
roll_size <size>
@ -72,7 +72,7 @@ output file <filename> {
A network socket.
```
```caddy-d
output net <address>
```
@ -86,8 +86,8 @@ The **format** subdirective lets you customize how logs get encoded (formatted).
In addition to the syntax for each individual encoder, these common properties can be set on most encoders:
```
{
```caddy-d
format <encoder_module> {
message_key <key>
level_key <key>
time_key <key>
@ -114,7 +114,7 @@ In addition to the syntax for each individual encoder, these common properties c
The console encoder formats the log entry for human readability while preserving some structure.
```
```caddy-d
format console
```
@ -122,7 +122,7 @@ format console
Formats each log entry as a JSON object.
```
```caddy-d
format json
```
@ -130,7 +130,7 @@ format json
Formats each log entry as [logfmt](https://brandur.org/logfmt).
```
```caddy-d
format logfmt
```
@ -138,7 +138,7 @@ format logfmt
Writes only a single field from the structure log entry. Useful if one of the fields has all the information you need.
```
```caddy-d
format single_field <field_name>
```
@ -154,13 +154,13 @@ format single_field <field_name>
Enable access logging (to the console):
```
```caddy-d
log
```
Write logs to a file (with log rolling, which is enabled by default):
```
```caddy-d
log {
output file /var/log/access.log
}
@ -168,7 +168,7 @@ log {
Customize log rolling:
```
```caddy-d
log {
output file /var/log/access.log {
roll_size 1gb
@ -180,7 +180,7 @@ log {
Use common log format (deprecated, but can be useful for older setups):
```
```caddy-d
log {
format single_field common_log
}

View file

@ -13,7 +13,7 @@ It expects that any `index.php` at the site root acts as a router. If that is no
## Syntax
```
```caddy-d
php_fastcgi [<matcher>] <php-fpm_gateway>
```
@ -26,7 +26,7 @@ Since this directive is an opinionated wrapper over a reverse proxy, you can ope
The `php_fastcgi` directive is the same as the following configuration:
```
```caddy-d
route {
# Add trailing slash for directory requests
@canonicalPath {
@ -64,18 +64,18 @@ Most modern PHP apps work well with this preset. If yours does not, feel free to
Proxy all PHP requests to a FastCGI responder listening at 127.0.0.1:9000:
```
```caddy-d
php_fastcgi 127.0.0.1:9000
```
Same, but only for requests under `/blog/`:
```
```caddy-d
php_fastcgi /blog/* 127.0.0.1:9000
```
When using php-fpm listening via a unix socket:
```
```caddy-d
php_fastcgi unix//run/php/php7.4-fpm.sock
```

View file

@ -11,7 +11,7 @@ This directive implies that a matched request is to be rejected. It is ordered v
## Syntax
```
```caddy-d
redir [<matcher>] <to> [<code>]
```
@ -28,18 +28,18 @@ redir [<matcher>] <to> [<code>]
Redirect all requests to `https://example.com`:
```
```caddy-d
redir https://example.com
```
Same, but preserve the existing URI:
```
```caddy-d
redir https://example.com{uri}
```
Same, but permanent:
```
```caddy-d
redir https://example.com{uri} permanent
```

View file

@ -9,7 +9,7 @@ Manipulates HTTP header fields on the request. It can set, add, and delete heade
## Syntax
```
```caddy-d
request_header [<matcher>] [[+|-]<field> [<value>|<find>] [<replace>]]
```
@ -23,6 +23,6 @@ request_header [<matcher>] [[+|-]<field> [<value>|<find>] [<replace>]]
Remove the Referer header from the request:
```
```caddy-d
request_header -Referer
```

View file

@ -9,7 +9,7 @@ Writes a hard-coded/static response to the client.
## Syntax
```
```caddy-d
respond [<matcher>] <status>|<body> [<status>] {
body <text>
close
@ -32,19 +32,19 @@ To clarify, the first non-matcher argument can be either a 3-digit status code o
Write a 200 status with an empty body to all health checks:
```
```caddy-d
respond /health-check 200
```
Write a simple response body to all requests:
```
```caddy-d
respond "Hello, world!"
```
Write an error response and close the connection:
```
```caddy-d
respond /secret/* "Access denied" 403 {
close
}

View file

@ -9,7 +9,7 @@ Proxies requests to one or more backends with configurable transport, load balan
## Syntax
```
```caddy-d
reverse_proxy [<matcher>] [<upstreams...>] {
# backends
to <upstreams...>
@ -118,7 +118,7 @@ Caddy's proxy **transport** is pluggable:
The `http` transport can look like this:
```
```caddy-d
transport http {
read_buffer <size>
write_buffer <size>
@ -146,7 +146,7 @@ transport http {
The `fastcgi` transport can look like this:
```
```caddy-d
transport fastcgi {
root <path>
split <at>
@ -163,19 +163,19 @@ transport fastcgi {
Reverse proxy all requests to a local backend:
```
```caddy-d
reverse_proxy localhost:9005
```
Load-balance all requests between 3 backends:
```
```caddy-d
reverse_proxy node1:80 node2:80 node3:80
```
Same, but only requests within `/api`, and with header affinity:
```
```caddy-d
reverse_proxy /api/* node1:80 node2:80 node3:80 {
lb_policy header X-My-Header
}
@ -183,7 +183,7 @@ reverse_proxy /api/* node1:80 node2:80 node3:80 {
Set the upstream Host header to the address of the upstream (by default, it will retain its original, incoming value):
```
```caddy-d
reverse_proxy localhost:9000 {
header_up Host {http.reverse_proxy.upstream.hostport}
}
@ -191,13 +191,13 @@ reverse_proxy localhost:9000 {
Reverse proxy to an HTTPS endpoint:
```
```caddy-d
reverse_proxy https://example.com
```
Strip a path prefix then proxy:
```
```caddy-d
route /prefix/* {
uri strip_prefix /prefix
reverse_proxy localhost:9000

View file

@ -13,7 +13,7 @@ Because `rewrite` essentially performs an internal redirect, the Caddyfile adapt
## Syntax
```
```caddy-d
rewrite [<matcher>] <to>
```
@ -24,25 +24,25 @@ rewrite [<matcher>] <to>
Rewrite all requests to `foo.html`, leaving any query string unchanged:
```
```caddy-d
rewrite * /foo.html
```
Replace the query string on API requests with `a=b`, leaving the path unchanged:
```
```caddy-d
rewrite /api/* ?a=b
```
Preserve the existing query string and add a key-value pair:
```
```caddy-d
rewrite /api/* ?{query}&a=b
```
Change both the path and query string, preserving the original query string while adding the original path as the `p` parameter:
```
```caddy-d
rewrite * /index.php?{query}&p={path}
```

View file

@ -11,7 +11,7 @@ Specifically, this directive sets the `{http.vars.root}` placeholder. It is mutu
## Syntax
```
```caddy-d
root [<matcher>] <path>
```
@ -23,7 +23,7 @@ Note that a matcher token is usually required since the first argument is a path
Set the site root to `/home/user/public_html` for all requests:
```
```caddy-d
root * /home/user/public_html
```
@ -31,7 +31,7 @@ root * /home/user/public_html
Set the site root to `public_html` (relative to current working directory) for all requests:
```
```caddy-d
root public_html
```
@ -39,6 +39,6 @@ root public_html
Set the site root only for requests in `/foo`:
```
```caddy-d
root /foo/* /home/user/public_html/foo
```

View file

@ -13,7 +13,7 @@ This directive is a special case in that its subdirectives are also regular dire
## Syntax
```
```caddy-d
route [<matcher>] {
<directives...>
}
@ -37,7 +37,7 @@ However, there may be occasions where the second directive (`redir`) has a more
So you might try a Caddyfile like this (but this will not work as expected!):
```
```caddy
example.com
file_server /specific.html
@ -48,7 +48,7 @@ The problem is that, internally, `redir` comes before `file_server`, but in this
Fortunately, the solution is easy: just wrap those two directives in a `route` block:
```
```caddy
example.com
route {
@ -68,7 +68,7 @@ And now `file_server` will be chained in before `redir` because the order is tak
Strip `/api` prefix from request path just before proxying all API requests to a backend:
```
```caddy-d
route /api/* {
uri strip_prefix /api
reverse_proxy localhost:9000

View file

@ -9,7 +9,7 @@ Executes the response body as a [template](/docs/modules/http.handlers.templates
## Syntax
```
```caddy-d
templates [<matcher>] {
mime <types...>
between <open_delim> <close_delim>
@ -26,7 +26,7 @@ templates [<matcher>] {
Enable templates on all requests:
```
```caddy-d
templates
```

View file

@ -13,7 +13,7 @@ Compatibility note: Due to its sensitive nature as a security protocol, delibera
## Syntax
```
```caddy-d
tls [internal|<email>] | [<cert_file> <key_file>] {
protocols <min> [<max>]
ciphers <cipher_suites...>
@ -69,19 +69,19 @@ tls [internal|<email>] | [<cert_file> <key_file>] {
Use a custom certificate and key:
```
```caddy-d
tls cert.pem key.pem
```
Use locally-trusted certificates for all hosts on the current site block, rather than public certificates via ACME / Let's Encrypt (useful in dev environments):
```
```caddy-d
tls internal
```
Use locally-trusted certificates, but managed on-demand intead of in the background:
```
```caddy-d
tls internal {
on_demand
}
@ -89,13 +89,13 @@ tls internal {
Specify an email address for your ACME account (but if only one email is used for all sites, we recommend the `email` [global option](/docs/caddyfile/options) instead):
```
```caddy-d
tls your@email.com
```
Enable the DNS challenge for a domain managed on Cloudflare with account credentials in an environment variable:
```
```caddy-d
tls {
dns cloudflare {env.CLOUDFLARE_API_TOKEN}
}

View file

@ -9,7 +9,7 @@ Rewrites the request URI path to the first of the listed files which exists in t
## Syntax
```
```caddy-d
try_files <files...>
```
@ -20,7 +20,7 @@ try_files <files...>
The `try_files` directive is basically a shortcut for:
```
```caddy-d
@try_files {
file {
try_files <files...>
@ -36,18 +36,18 @@ Note that this directive does not accept a matcher token. If you need more compl
If the request does not match any static files, rewrite to an index/router file:
```
```caddy-d
try_files {path} /index.php
```
Same, but adding the original path to the query string:
```
```caddy-d
try_files {path} /index.php?{query}&p={path}
```
Same, but also match directories:
```
```caddy-d
try_files {path} {path}/ /index.php?{query}&p={path}
```

View file

@ -11,7 +11,7 @@ This directive is distinct from [`rewrite`](rewrite) in that `uri` _partially_ c
## Syntax
```
```caddy-d
uri [<matcher>] strip_prefix|strip_suffix|replace \
<target> \
[<replacement> [<limit>]]
@ -30,18 +30,18 @@ uri [<matcher>] strip_prefix|strip_suffix|replace \
Strip `/api` from the beginning of all request paths:
```
```caddy-d
uri strip_prefix /api
```
Strip `.php` from the end of all request paths:
```
```caddy-d
uri strip_suffix .php
```
Replace "/docs/" with "/v1/docs/" in any request URI:
```
```caddy-d
uri replace /docs/ /v1/docs/
```