Creates Caddy web server configurations with automatic HTTPS, reverse proxies, static files, PHP, and custom directives using Caddyfile syntax.
Generates production-ready Caddyfile configurations for the Caddy web server with automatic HTTPS, reverse proxying, static file serving, PHP support, and advanced directives.
Creates complete Caddyfile configurations based on user requirements, following Caddy's conventions for site blocks, directives, matchers, and global options. Handles common patterns like reverse proxies, static sites, PHP applications, custom headers, redirects, and TLS configuration.
When a user requests a Caddy configuration:
1. **Gather Requirements**
- Ask about the domain(s) or addresses to serve
- Determine the primary use case: static files, reverse proxy, PHP application, API gateway, etc.
- Identify any special requirements: custom headers, redirects, authentication, compression, logging, rate limiting
- Confirm if automatic HTTPS should be enabled (default: yes)
2. **Generate the Caddyfile**
- Start with the site address block (domain or `:port`)
- Add directives in logical order (typically: root, encode, handle blocks, reverse_proxy, file_server, etc.)
- Use proper Caddyfile syntax:
- Site blocks start with address, followed by `{` on the same or next line
- Directives are indented inside blocks
- Matchers use `@name` syntax before directives
- Subdirectives are indented under parent directives
- Include comments to explain non-obvious configuration choices
3. **Apply Best Practices**
- Enable gzip/zstd compression with `encode gzip zstd` for most sites
- Use `root * /path/to/files` for static file serving
- Configure `php_fastcgi` for PHP applications (e.g., WordPress, Laravel)
- Set up `reverse_proxy` with appropriate headers for backend services
- Add `file_server` directive when serving static assets
- Use `try_files` for SPA routing (e.g., React, Vue apps)
- Configure CORS headers if needed for APIs
- Add security headers (HSTS, CSP, X-Frame-Options) when appropriate
4. **Common Directive Patterns**
- **Static site:** `root * /var/www/html`, `file_server`
- **Reverse proxy:** `reverse_proxy localhost:3000` or `reverse_proxy unix//run/app.sock`
- **PHP application:** `php_fastcgi unix//run/php/php8.2-fpm.sock` or `php_fastcgi localhost:9000`
- **SPA routing:** `try_files {path} /index.html`
- **Redirects:** `redir https://newdomain.com{uri} permanent`
- **Custom headers:** `header { X-Custom-Header "value" }`
- **Basic auth:** `basicauth { user $2a$14$hashedpassword }`
- **Rate limiting:** `request_body { max_size 10MB }`
5. **Handle Multiple Sites**
- Create separate site blocks for each domain
- Use snippets (with `(snippet-name)` syntax) for shared configuration
- Import snippets with `import snippet-name`
6. **Global Options (if needed)**
- Add global options block at the top of the Caddyfile (outside site blocks)
- Common options: `email`, `admin`, `acme_ca`, `log`, `storage`
- Example:
```
{
email [email protected]
admin off
}
```
7. **Validation & Testing**
- Remind the user to validate the Caddyfile with: `caddy validate --config Caddyfile`
- Suggest testing with: `caddy run --config Caddyfile` (foreground mode)
- Provide reload command for production: `caddy reload --config Caddyfile`
8. **Provide Installation Context**
- If the user doesn't have Caddy installed, provide the installation command:
- macOS: `brew install caddy`
- Ubuntu/Debian: `sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https && curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg && curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list && sudo apt update && sudo apt install caddy`
- Docker: `docker run -d -p 80:80 -p 443:443 -v caddy_data:/data -v $PWD/Caddyfile:/etc/caddy/Caddyfile caddy:latest`
9. **Document the Configuration**
- Add inline comments explaining each section
- Include a header comment with the site purpose and last updated date
- Provide usage instructions (how to start/reload Caddy with this config)
**User:** "I need a Caddyfile for a Node.js API running on port 3000 at api.example.com with CORS enabled"
**Assistant generates:**
```caddyfile
api.example.com {
# Enable compression
encode gzip zstd
# CORS headers for cross-origin requests
header {
Access-Control-Allow-Origin "*"
Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Access-Control-Allow-Headers "Content-Type, Authorization"
}
# Handle preflight requests
@options method OPTIONS
handle @options {
respond 204
}
# Proxy to Node.js backend
reverse_proxy localhost:3000 {
# Preserve original host and IP
header_up Host {host}
header_up X-Real-IP {remote_host}
header_up X-Forwarded-For {remote_host}
header_up X-Forwarded-Proto {scheme}
}
}
```
**User:** "Create a Caddyfile for serving a React app from /var/www/app with SPA routing"
**Assistant generates:**
```caddyfile
example.com {
# Serve files from this directory
root * /var/www/app
# Enable compression
encode gzip zstd
# Security headers
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
X-Frame-Options "DENY"
}
# SPA routing: serve index.html for all non-file paths
try_files {path} /index.html
# Serve static files
file_server
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/generate-caddy-caddyfile-configuration/raw