How to use handle_errors with reverse_proxy? (Noob)

1. Caddy version (caddy version):

2.11

2. How I run Caddy:

a. System environment:

Docker

b. Command:

docker-compose up -d

c. Service/unit/compose file:

    version: "3"
    services:
        reverse-proxy:
            image: caddy
            container_name: reverse-proxy
            volumes:
                - /root/config/caddy/Caddyfile:/etc/caddy/Caddyfile
                - /root/config/caddy:/data
            ports:
                - 80:80
                - 443:443
            restart: unless-stopped
        home-website:
            image: caddy
            container_name: home-website
            volumes:
                - /caddy/home:/usr/share/caddy
            ports:
                - 4000:80
            restart: unless-stopped
        assets-website:
            image: caddy
            container_name: home-website
            volumes:
                - /caddy/home:/usr/share/caddy
            ports:
                - 8050:80
            restart: unless-stopped

d. My complete Caddyfile or JSON config:

:443 {
    tls /data/ssl/example.com.pem /data/ssl/example.com.key
    handle_errors {
        root * /data/error-pages
        rewrite * /{http.error.status_code}.html
        file_server
    }
}

home.example.com {
    reverse_proxy localhost:4000
}

assets.example.com {
    reverse_proxy localhost:8050
}

3. The problem I’m having:

I have a blank page when there’s an error even though there’s an 404.html file at /data/error-pages/404.html

4. Error messages and/or full log output:

{"level":"info","ts":1594127779.476189,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
{"level":"info","ts":1594127779.4783037,"logger":"admin","msg":"admin endpoint started","address":"tcp/localhost:2019","enforce_origin":false,"origins":["localhost:2019","[::1]:2019","127.0.0.1:2019"]}
2020/07/07 13:16:19 [WARNING] Stapling OCSP: no OCSP stapling for [cloudflare origin certificate *.example.com example.com]: no URL to issuing certificate
{"level":"info","ts":1594127779.4790118,"logger":"http","msg":"skipping automatic certificate management because one or more matching certificates are already loaded","domain":"assets.example.com","server_name":"srv0"}
{"level":"info","ts":1594127779.4790308,"logger":"http","msg":"skipping automatic certificate management because one or more matching certificates are already loaded","domain":"home.example.com","server_name":"srv0"}
{"level":"info","ts":1594127779.4790447,"logger":"http","msg":"enabling automatic HTTP->HTTPS redirects","server_name":"srv0"}
{"level":"info","ts":1594127779.4797516,"logger":"tls","msg":"cleaned up storage units"}
2020/07/07 13:16:19 [INFO][cache:0xc0007b4000] Started certificate maintenance routine
{"level":"info","ts":1594127779.4875376,"msg":"autosaved config","file":"/config/caddy/autosave.json"}
{"level":"info","ts":1594127779.4875915,"msg":"serving initial configuration"}

5. What I already tried:

I tried to move handle_errors to :80 and to inside home.example.com, assets.example.com and *.example.com with no luck.

6. Links to relevant resources:

Welcome matan,

Since your handle_errors directive is in a different site than your reverse_proxy directives, it won’t apply to the sites that are reverse-proxying.

Also, just so you know, the reverse proxy won’t return an error if the proxying succeeds (even if the upstream returns a 4xx or 5xx status). For now, you can still intercept those responses (with JSON config currently): Modules - Caddy Documentation

1 Like

Got it! Thanks a lot!

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