Okay, let's create a simple Caddyfile and then break down what each part does.
This Caddyfile is a very common setup: it serves static files for a domain and automatically gets an SSL certificate for you.
# This is a comment
# Caddyfile for a simple static website
yourdomain.com {
# Sets the root directory where Caddy will look for files
# The * means apply this root to all requests for this domain
root * /var/www/html
# Enables the static file server
# This directive tells Caddy to serve files from the root directory
file_server
# Caddy automatically handles HTTPS for yourdomain.com
# by default when you use a domain name here.
# It will provision and renew certificates from Let's Encrypt or ZeroSSL.
# It also automatically redirects HTTP (port 80) to HTTPS (port 443).
}
# You can add other site blocks below for different domains or subdomains
# AnotherDomain.com {
# ... directives for AnotherDomain.com ...
# }
Explanation of the Caddyfile:
-
# This is a comment:
- Lines starting with
# are comments. Caddy ignores them. They are useful for adding notes to your configuration.
-
yourdomain.com { ... }:
- This is a site block. The first line (
yourdomain.com) specifies the address that this configuration block applies to. Caddy will listen for requests coming to yourdomain.com.
- You must replace
yourdomain.com with your actual domain name (e.g., example.com, www.mysite.net).
- You can also use an IP address (
192.168.1.10), localhost, or even just the port number (:80) here. Using a domain name is special because Caddy will automatically handle getting free SSL/TLS certificates for you (from Let's Encrypt or ZeroSSL) and setting up HTTPS.
- The curly braces
{} contain the directives that tell Caddy what to do for requests matching this address.
-
root * /var/www/html:
- This is the
root directive. It specifies the document root – the directory on the server's filesystem where Caddy should look for files to serve.
- The
* after root is a matcher. In this case, * means this root applies to all request paths (within this site block).
/var/www/html is the path to the directory. You should change this to the actual path where your website's files (like index.html, styles.css, etc.) are located on your server. Common places include /var/www/html, /srv/http, /home/youruser/website.
-
file_server:
- This is the
file_server directive. It enables Caddy's static file server functionality.
- With
root and file_server together, Caddy will:
- Receive a request (e.g., for
yourdomain.com/about.html).
- Look for a file named
about.html inside the directory specified by root (/var/www/html/about.html).
- If found, it serves that file.
- If a directory is requested (e.g.,
yourdomain.com/), it will look for an index file (like index.html or index.htm) within that directory and serve it if found.
-
Automatic HTTPS Explanation (Comments):
- The comments explain Caddy's default behavior when you use a domain name. Caddy is designed to make HTTPS easy.
- When you use a domain name in the site address, Caddy automatically attempts to:
- Agree on a TLS standard (like ACME).
- Prove control of the domain (usually via the HTTP challenge).
- Obtain a free SSL certificate from a Certificate Authority (like Let's Encrypt or ZeroSSL).
- Configure itself to use that certificate for HTTPS (port 443).
- Automatically redirect any incoming HTTP requests (port 80) to their HTTPS equivalent.
- Renew the certificate automatically before it expires.
- This happens without needing explicit
tls or redir directives for the basic case.
How to Use This Caddyfile:
- Save the content above (replacing
yourdomain.com and /var/www/html) into a file named Caddyfile (no extension).
- Place this
Caddyfile in the directory where you will run the caddy command from, or specify its path when running Caddy.
- Make sure your actual website files are in the directory you specified in the
root directive (/var/www/html in the example).
- Run Caddy using a command like
caddy start (to run in the background) or caddy run (to run in the foreground, useful for testing). Note: Binding to ports 80 and 443 often requires elevated privileges, so you might need to use sudo caddy start or set up Caddy as a system service.
This is a fundamental Caddyfile. Caddy can do much more, like reverse proxying, load balancing, request matching based on various criteria, compression, and much more, all configured through this simple-to-read Caddyfile format using different directives.