Common Caddyfile Patterns

These are not drop-in solutions; you will have to customize your domain name, ports/sockets, directory paths, etc. They are intended to illustrate some of the most common configuration patterns.

Menu

As usual, the first line is the site address. The root directive specifies the path to the root of the site (the * means to match all requests, so as to disambiguate from a )—change the path to your site if it isn’t the current working directory. Finally, we enable the static file server.

Proxy all requests:

  1. example.com
  2. reverse_proxy localhost:5000

Only proxy requests having a path starting with /api/ and serve static files for everything else:

  1. example.com
  2. file_server

Customize the site root and path matcher accordingly; this example assumes PHP is only in the /blog/ subdirectory—all other requests will be served as static files.

The is actually just a shortcut for several pieces of configuration.

To add the www. subdomain with an HTTP redirect:

To remove it:

  1. www.example.com {
  2. redir https://example.com{uri}
  3. }
  4. example.com {
  5. }

However, if you need to, you can still enforce trailing slashes with your config. There are two ways to do it: internally or externally.

This uses the directive. Caddy will rewrite the URI internally to add or remove the trailing slash:

Using a rewrite, requests with and without the trailing slash will be the same.

External enforcement

This uses the directive. Caddy will ask the browser to change the URI to add or remove the trailing slash:

  1. example.com