Caddy server recipes

Unless indicated otherwise, these recipes are for the Caddy Web server version 2.


Show an error page on errors

This will give your server a human-readable, if not the most user-friendly, error page. It will tell the user an error has occurred and give them something to report to you. The page will contain a custom error message for the most common errors: 403, 404, 500. Otherwise the error message will be the description text of the HTTP status code.


handle_errors {
    rewrite * /error.html



<!DOCTYPE html>
{{ $code := placeholder "http.error.status_code" }}
{{ $text := placeholder "http.error.status_text" }}
    <title>{{ $code }} {{ $text }}</title>
    <h1>Error {{ $code }}</h1>
{{ if eq $code "403" }}
    <p>You don't have permission to access this resource.</p>
{{ else if eq $code "404" }}
    <p>The requested URL was not found on this server.</p>
{{ else if eq $code "500" }}
    <p>An internal server error has occurred.</p>
{{ else }}
    <p>{{ $text }}.</p>
{{ end }}

Redirect a query parameter to a path

This will only work with a single query parameter. The example is something useful for the Fossil SCM wiki. It prevents search engines and users from accessing your pages as both /wiki?name=foo and /wiki/foo. (The latter seems more search engine-friendly.)

@wiki_query {
    path_regexp ^/wiki(?:/*|)$
    query name=*
route @wiki_query {
    uri replace name= /
    redir * /wiki{query}

Reverse proxy everything except some static files

root * /opt/foo/static
@not_static {
    not {
        path /BingSiteAuth.xml /robots.txt  
        path /favicon.ico
reverse_proxy @not_static localhost:8100

WeeChat reverse proxy

See the Glowing Bear wiki page Proxying WeeChat relay with a web server.

Caddyfile.d: a modular Caddyfile

import Caddyfile.d/*.conf

Tags: configuration, how-to, Internet, sysadmin, webdev.