Caddy Web Server#

Config Options#

Simple fileserver#

my.domain {
    root * /var/www/html
    file_server
}

Disable HTTP to HTTPS Redirect#

Note: Global Option

{
    auto_https disable_redirects
}

Block IP, IP Range or UserAgent#

@blocked {
    remote_ip 178.104.0.0/15
    remote_ip 202.46.32.0/19
    remote_ip 43.0.0.0/9
    remote_ip 43.160.0.0/12
    remote_ip 43.152.0.0/13
    remote_ip 43.245.0.0/16
    remote_ip 43.239.0.0/16
    remote_ip 69.12.56.0/21
    header "User-Agent" *AhrefsBot*
    header "User-Agent" *Amazonbot*
    header "User-Agent" *Bytespider*
}

# Immediately close connection for blocked IPs
abort @blocked

Basic Authentication (only the contents of /Testing)#

To generate the argon2id passowrd use: echo -n 'MySecretPassword' | argon2 (tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 64 ; echo) -id -e

basic_auth /Testing/* argon2id {
    # Username Testing, Password MySecretPassword
    Testing $argon2id$v=19$m=4096,t=3,p=1$anRkbWRnVFdjRGpDZEQyMmdXM2t5c3hvZTBPOXgyS2NmMGFhYUtPbVJ2dlY5ajkzQ21OQktPVks5MmtyM25ZRQ$s1jUba2USfeay+KiSXSK5O0/rgfCYS++1Hk4+tq/X9A
}

Simple fileserver with indexes#

my.domain {
    root * /var/www/html
    file_server browse
}

Mediawiki#

wiki.mydomain.com {
    @title {
        not file {
            try_files {path} {path}/
            split_path .php
        }
        path_regexp title ^/(.*)$
    }
    rewrite @title /index.php?title={re.title.1}&{query}
    root * /var/www/mediawiki
    php_fastcgi unix//var/run/php/php-fpm.sock
    file_server
}

Reverse Proxy#

example.com {
    reverse_proxy localhost:5000
}

Reverse Proxy only a path#

example.com {
    root * /var/www
    reverse_proxy /api/* localhost:5000
    file_server
}

PHP FPM with network port#

example.com {
    root * /srv/public
    encode
    php_fastcgi localhost:9000
    file_server
}

PHP FPM with unix socket (WordPress Example)#

example.com {
    root * /var/www/example.com
    php_fastcgi unix//run/php/php7.4-fpm.sock
    file_server
    encode gzip
    @disallowed {
        path /xmlrpc.php
        path *.sql
        path /wp-content/uploads/*.php
    }
    rewrite @disallowed '/index.php'
}

Rewrite URLs#

example.com {
    rewrite /add     /add/
    rewrite /remove/ /remove
}

HTTP Only#

http://mydomain.com {
    root * /var/www/mydomain
    file_server
}

Client-side Caching#

# Cache images, CSS, JavaScript client-side
@cachedFiles {
    path *.jpg *.jpeg *.png *.gif *.webp *.ico *.js *.css *.woff *.woff2
}
header @cachedFiles Cache-Control "public, max-age=604800, must-revalidate"