Caddy.Config (Caddy v2.3.1)
View SourceStructured configuration for Caddy reverse proxy server.
Configuration is stored in 3 distinct parts that are assembled into a Caddyfile:
- Global options (
global) - Content for the global options block{ ... } - Additionals (
additionals) - List of snippets, imports, matchers, etc. - Sites (
sites) - List of site configurations with address and content
Example
config = %Caddy.Config{
global: """
debug
admin unix//tmp/caddy.sock
""",
additionals: [
%{name: "common", content: "(common) {\n header X-Frame-Options DENY\n}"},
%{name: "security", content: "(security) {\n header X-Content-Type-Options nosniff\n}"}
],
sites: [
%{address: "example.com", config: "reverse_proxy localhost:3000"},
%{address: "api.example.com", config: "reverse_proxy localhost:4000"}
]
}This assembles into:
{
debug
admin unix//tmp/caddy.sock
}
(common) {
header X-Frame-Options DENY
}
(security) {
header X-Content-Type-Options nosniff
}
example.com {
reverse_proxy localhost:3000
}
api.example.com {
reverse_proxy localhost:4000
}Validation
Configuration is validated by calling the Caddy binary's adapt command,
which converts Caddyfile to JSON and catches syntax errors.
Summary
Types
Additional configuration item (snippet, import, matcher, etc.).
Site configuration map with address and config content.
Functions
Convert Caddyfile text to JSON using Caddy binary.
Get the admin API URL for external mode.
Get backup file path
Get configurable base path for caddy files
Get a specific command for external mode.
Get system commands for external mode operations.
Create a default Caddyfile with admin socket configuration.
Create a default config struct with admin socket configuration.
Get environment file path
Get etc path for configuration files
Check if running in external mode.
Get the health check interval in milliseconds for external mode.
Get init configuration file path
Get the operating mode for Caddy management.
Get PID file path
Get configurable priv path
Get run path for runtime files
Get saved JSON configuration file path
Get share path (base path)
Get socket file path
Get tmp path for temporary files
Assemble the 3-part configuration into a complete Caddyfile text.
Validate Caddy binary path
Validate complete configuration
Get XDG config home path
Get XDG data home path
Types
Additional configuration item (snippet, import, matcher, etc.).
name- A descriptive name for this additional (e.g., "common-headers", "security")content- The actual Caddyfile content
Site configuration map with address and config content.
address- The site address (e.g., "example.com", "localhost:8080")config- The site configuration content (without wrapping braces)
Functions
Convert Caddyfile text to JSON using Caddy binary.
This validates the Caddyfile syntax and returns the JSON configuration that Caddy will use internally.
@spec admin_url() :: binary()
Get the admin API URL for external mode.
Supports both TCP and Unix socket connections:
"http://localhost:2019"- TCP connection"unix:///path/to/caddy.sock"- Unix domain socket
Falls back to the configured socket_file in embedded mode.
Example
config :caddy, admin_url: "http://localhost:2019"
@spec backup_json_file() :: Path.t()
Get backup file path
Get configurable base path for caddy files
Get a specific command for external mode.
Returns nil if the command is not configured.
Get system commands for external mode operations.
Commands are executed via System.cmd when managing an externally-controlled Caddy.
Example
config :caddy, commands: [
start: "systemctl start caddy",
stop: "systemctl stop caddy",
restart: "systemctl restart caddy",
status: "systemctl is-active caddy"
]
@spec default_caddyfile() :: binary()
Create a default Caddyfile with admin socket configuration.
@spec default_config() :: t()
Create a default config struct with admin socket configuration.
Get environment file path
Get etc path for configuration files
@spec external_mode?() :: boolean()
Check if running in external mode.
@spec health_interval() :: pos_integer()
Get the health check interval in milliseconds for external mode.
Defaults to 30 seconds.
Example
config :caddy, health_interval: 60_000
Get init configuration file path
@spec mode() :: :embedded | :external
Get the operating mode for Caddy management.
:external(default) - Caddy is managed externally (e.g., systemd), communicate via Admin API:embedded- Caddy binary is managed by this application
External mode is the default because it's safer - it doesn't spawn processes and works with empty configuration (waiting state).
Example
config :caddy, mode: :embedded
Get PID file path
Get configurable priv path
Get run path for runtime files
Get saved JSON configuration file path
Get socket file path
Get tmp path for temporary files
Assemble the 3-part configuration into a complete Caddyfile text.
Combines global options, additional directives, and site configurations into a single Caddyfile string.
Output Format
{
<global content>
}
<additional content>
<site1_address> {
<site1_content>
}
<site2_address> {
<site2_content>
}Examples
iex> config = %Caddy.Config{
...> global: "debug",
...> additional: "",
...> sites: [%{address: "example.com", config: "reverse_proxy localhost:3000"}]
...> }
iex> Caddy.Config.to_caddyfile(config)
"{\n debug\n}\n\nexample.com {\n reverse_proxy localhost:3000\n}"
See System.user_home/0.
Validate Caddy binary path
Validate complete configuration
Get XDG config home path
Get XDG data home path