If you are using Caddy 2 web server, the handle_errors directive allows creating a new rule for serving a custom page on a 404 error.
In this example I have created a custom 404 template in a file named 404.php and the following directive is set:
pcx3.com {
root * /var/www/html/pcx3.com/public
file_server
handle_errors {
@404 {
expression {http.error.status_code} == 404
}
rewrite @404 /404.php
file_server
}
}