In this article you are going to learn about placeholders, varibles and environment variables in Caddy.
Placeholders #
You can access placeholders in Caddy using curly braces { }
to access dynamic values inside caddy's configuration. Some of these dynamic values are built in and some are defined by you.
For example, to get current time use time.now
.
:80 { respond {time.now} }
Caddyfile
This will return the current time.
Here is a complete list of some built in placeholders.
Variables #
You can you define variables using the vars
keyword.
The defined variable is to use with vars.*
. For example, to access a variable named root
use vars.root
.
:80 { vars root https://example.com handle { respond "{vars.root}" } }
Caddyfile
To define multiple varibles use the block form of vars
.
:80 { vars { root https://example.com message "Hello, World!" } handle { respond "{vars.root} {vars.message}" } }
Caddyfile
You can define variable values based on route as well. Below is how you can have different value in a variable depending on the path prefix.
:80 { vars base "/app" vars /admin* base "/admin" vars /manage* base "/manage" handle { respond "{vars.base}" } }
Caddyfile
Some examples of evaluation:
- Path:
/123
-> base:/app
- Path:
/admin/settings
-> base:/admin
- Path
/manage/users/1
-> base:/manage