I cannot get Error handling to work properly

1. The problem I’m having:

I’m trying out and playing with Caddy, but I cannot get error handling to work…

TLDR; for my setup:

  • nikasere.xyz should proxy to localhost:3000 with the /shared path being specifically handled by another proxy to localhost:3001 (works :white_check_mark:)
  • test.nikasere.xyz - statically serves some files (works :white_check_mark:)
  • I have a /srv/50x.html page I want to show if any error happens in any of the defined locations, if it’s not handled by them - e.g. by stuff running as proxy (doesn’t work :x:)

Currently, if I stop the container responsible for /nikasere.xyz (it’s stopped now)

2. Error messages and/or full log output:

{
  "level": "error",
  "ts": 1712846861.2044957,
  "logger": "http.log.error",
  "msg": "dial tcp: lookup server on 127.0.0.11:53: server misbehaving",
  "request": {
    "remote_ip": "188.252.198.224",
    "remote_port": "13750",
    "client_ip": "188.252.198.224",
    "proto": "HTTP/3.0",
    "method": "GET",
    "host": "nikasere.xyz",
    "uri": "/",
    "headers": {
      "Pragma": [
        "no-cache"
      ],
      "Upgrade-Insecure-Requests": [
        "1"
      ],
      "Accept-Encoding": [
        "gzip, deflate, br, zstd"
      ],
      "Sec-Ch-Ua-Mobile": [
        "?1"
      ],
      "User-Agent": [
        "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36"
      ],
      "Sec-Fetch-Site": [
        "none"
      ],
      "Sec-Ch-Ua": [
        "\"Google Chrome\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\""
      ],
      "Sec-Fetch-Mode": [
        "navigate"
      ],
      "Sec-Fetch-User": [
        "?1"
      ],
      "Cache-Control": [
        "no-cache"
      ],
      "Sec-Ch-Ua-Platform": [
        "\"Android\""
      ],
      "Accept": [
        "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
      ],
      "Sec-Fetch-Dest": [
        "document"
      ],
      "Accept-Language": [
        "en-US,en;q=0.9,bs;q=0.8,hr;q=0.7,de;q=0.6"
      ]
    },
    "tls": {
      "resumed": true,
      "version": 772,
      "cipher_suite": 4865,
      "proto": "h3",
      "server_name": "nikasere.xyz"
    }
  },
  "duration": 0.012622262,
  "status": 502,
  "err_id": "1u17ptnp2",
  "err_trace": "reverseproxy.statusError (reverseproxy.go:1267)"
}

3. Caddy version:

caddy v2.7.6

4. How I installed and ran Caddy:

a. System environment:

Ubuntu 22.04 on VPS.

b. Command:

Using docker compose.

sudo docker compose up -d

c. Service/unit/compose file:

version: "3.9"
name: my-app

services:
  caddy:
    image: caddy:latest
    restart: unless-stopped
    container_name: caddy
    ports:
      - '80:80'
      - '443:443'
      - '443:443/udp'
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./site:/srv
      - caddy_data:/data
      - caddy_config:/config
      - /home/dev/stuff/projects/caddy_test/caddy/docs.nikasere.xyz:/data/docs.nikasere.xyz

  server:
    image: example/server
    build:
      context: ../simple-server-test
    restart: unless-stopped
    ports:
      - :3000

  server2:
    image: example/server2
    build:
      context: ../simple-server-test-copy
    restart: unless-stopped
    ports:
      - :3000

volumes:
  caddy_data:
  caddy_config:

d. My complete Caddy config:

nikasere.xyz {
        root * /srv

        reverse_proxy server:3000

        handle_path /shared* {
                uri strip_prefix /shared
                rewrite * /{path}
                reverse_proxy server2:3000
        }
 
        # Doesnt work like this
        handle_errors {
                rewrite * /50x.html
                file_server
        }
}

test.nikasere.xyz {
        root * /data/docs.nikasere.xyz
        file_server
}

# Doesnt work like this either
handle_errors {
        root * /srv
        rewrite * /50x.html
        file_server
}

5. Links to relevant resources:

Ok I fixed the problem… It was my bad… I presume something was saved inside of the Caddy volume which persisted even if I changed my Caddyfile, after deleting the volume then restarting everything works as expected.

handle_path does path stripping, so you don’t need uri strip_prefix here.

Also the rewrite doesn’t do anything at all, you can remove that too.

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