Caddy

Installation

The following installation will work on debian-based distributions

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
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

Reverse proxy

A simple reverse proxy

example.com {
    reverse_proxy localhost:5000
}

Only direct certain traffic, such as an api route

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

Restart the service on failure

This will automatically restart the server after 5 seconds if the exit code when crashing was not 1.

To edit the below file, run

sudo systemctl edit caddy
[Service]
RestartPreventExitStatus=1
Restart=on-failure
RestartSec=5s

Redirect from www to root

Add www

example.com {
    redir https://www.{host}{uri}
}

www.example.com {
}

Remove www

www.example.com {
    redir https://example.com{uri}
}

example.com {
}