A comprehensive guide to configuring security features in Caddy for enhanced protection.
Before configuring security on Caddy, ensure you have:
example.com {
# TLS configuration
tls {
protocols tls1.2 tls1.3
curves x25519 p256 p384 p521
alpn h2 http/1.1
}
# Basic security headers
header {
# Remove server header
-Server
# Security headers
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
X-Frame-Options "DENY"
Referrer-Policy "strict-origin-when-cross-origin"
Permissions-Policy "geolocation=(), microphone=(), camera=()"
}
}
Key directives explained:
example.com {
header {
# Content Security Policy
Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';"
# Cross-Origin settings
Cross-Origin-Embedder-Policy "require-corp"
Cross-Origin-Opener-Policy "same-origin"
Cross-Origin-Resource-Policy "same-origin"
# Additional security headers
X-XSS-Protection "1; mode=block"
X-Download-Options "noopen"
X-Permitted-Cross-Domain-Policies "none"
}
}
example.com {
# Rate limiting for API endpoints
@api {
path /api/*
}
rate_limit @api {
zone api_limit
rate 10r/10s
burst 20
}
# Rate limiting for login attempts
@login {
path /login
method POST
}
rate_limit @login {
zone login_limit
rate 5r/1m
burst 10
}
}
Rate limiting explained:
example.com {
# WAF configuration using Caddy's security middleware
security {
# Basic WAF rules
rule {
match {
path "*.php"
method "POST"
}
block {
status 403
message "Access denied"
}
}
# SQL Injection protection
rule {
match {
query ".*(SELECT|INSERT|UPDATE|DELETE|DROP|UNION).*"
}
block {
status 403
message "SQL Injection attempt detected"
}
}
}
}
# Caddy configuration will appear here