Custom error pages using expressions not working

1. Output of caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. How I run Caddy:

a. System environment:

OS: Debian 11
Caddy installed through: apt
Running through: systemd

b. Command:

service caddy restart

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddy config:

{
        debug
}

cdn.blazing.works {
        root * /var/cdn
        file_server

        handle_errors {
                @401 expression `{error.status_code} == 401`
                @403 expression `{error.status_code} == 403`
                @404 expression `{error.status_code} == 404`
                @405 expression `{error.status_code} == 405`
                @418 expression `{error.status_code} == 418`
                @429 expression `{error.status_code} == 429`
                @500 expression `{error.status_code} == 500`
                @503 expression `{error.status_code} == 503`
                @504 expression `{error.status_code} == 504`

                root * /var/www/errors

                handle @401 {
                        rewrite * /blazingworks-401.html
                }
                handle @403 {
                        rewrite * /blazingworks-403.html
                }
                handle @404 {
                        rewrite * /blazingworks-404.html
                }
                handle @405 {
                        rewrite * /blazingworks-405.html
                }
                handle @418 {
                        rewrite * /blazingworks-418.html
                }
                handle @429 {
                        rewrite * /blazingworks-429.html
                }
                handle @500 {
                        rewrite * /blazingworks-500.html
                }
                handle @503 {
                        rewrite * /blazingworks-503.html
                }
                handle @504 {
                        rewrite * /blazingworks-504.html
                }

        handle {
            rewrite * /blazingworks-500.html
        }

                file_server
        }
}

3. The problem I’m having:

No matter what I try, I cannot get the individual custom errors to be used. No matter what, it always uses the fallback (so the 500 html file). When I remove that fallback, I just get my browser’s default 404 error presented. But I can’t get my custom 404 html page working.

4. Error messages and/or full log output:

Had to upload the log on pastebin, because Discourse gave me an error telling me I can’t post some specific line from the logs.

You can find them here.

5. What I already tried:

I tried using the expression(s) {err.status_code} in [404] to match what is shown on the documentation, but that didn’t work either.

6. Links to relevant resources:

The placeholders for status codes are either {err.*} (shortcut) or {http.error.*} (long-form), but you used {error.*}. See here in the docs:

Also since v2.6.0, you can use this shorter syntax for expression matchers (i.e. omit the expression word):

@401 `{err.status_code} == 401`

You could probably significantly simplify your config though:

@knownCodes `{err.status_code} in [401, 403, 404, 405, 418, 429, 500, 503, 504]`
handle @knownCodes {
	rewrite * /blazingworks-{err.status_code}.html
}
1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.