# Simple static site
example.com {
root * /var/www/html
file_server
}
# PHP site
example.com {
root * /var/www/html
php_fastcgi unix//run/php/php-fpm.sock
file_server
}
# Basic reverse proxy
example.com {
reverse_proxy localhost:8080
}
# Proxy with headers
example.com {
reverse_proxy localhost:8080 {
header_up Host {host}
header_up X-Real-IP {remote_host}
header_up X-Forwarded-For {remote_host}
header_up X-Forwarded-Proto {scheme}
}
}
# Load balancing
example.com {
reverse_proxy {
to localhost:8080 localhost:8081
lb_policy round_robin
}
}
# Force HTTPS
example.com {
redir https://{host}{uri} permanent
}
# Custom TLS
example.com {
tls your@email.com
}
# Self-signed certificate
example.com {
tls internal
}
# Custom certificate files
example.com {
tls /path/to/cert.pem /path/to/key.pem
}
example.com {
header {
# Security headers
Strict-Transport-Security "max-age=31536000; includeSubDomains"
X-Content-Type-Options "nosniff"
X-Frame-Options "DENY"
Referrer-Policy "no-referrer-when-downgrade"
X-XSS-Protection "1; mode=block"
# CORS
Access-Control-Allow-Origin "*"
}
}
example.com {
encode gzip zstd
file_server {
precompressed br gzip
}
}
# Basic authentication
example.com {
basicauth {
user JDJhJDE0JE91S2l4dFVWckN2RzlwZXZzLnpPNE8vNmY3L2V6MkV1bnZxUjhNWnhVZEtVZC5URXNPMVVX
}
}
# Rate limiting
example.com {
rate_limit {
zone dynamic {
key {remote_host}
events 10
window 10s
}
}
}
# Path-specific rules
example.com {
handle /api/* {
reverse_proxy localhost:3000
}
handle /static/* {
root * /var/www/static
file_server
}
}
example.com {
log {
output file /var/log/caddy/access.log
format json
level INFO
}
}
{
env SITE_ROOT
}
example.com {
root * {$SITE_ROOT}
file_server
}
# Path matching
handle /api/* {
respond "API endpoint"
}
# Method matching
@post method POST
handle @post {
respond "POST request"
}
# Host matching
@host host example.com
handle @host {
respond "Specific host"
}
# File matching
@images path *.jpg *.png *.gif
handle @images {
header Cache-Control "max-age=31536000"
}
-
Test configuration:
caddy validate
-
Reload configuration:
caddy reload
-
Format Caddyfile:
caddy fmt
-
View current config:
caddy adapt