Handle_errors help

1. My Caddy version (caddy version):

v2.0.0-rc.3 h1:z2H/QnaRscip6aZJxwTbghu3zhC88Vo8l/K57WUce4Q=

2. How I run Caddy:

caddy act as https reverse proxy for a spring api server

a. System environment:

running on docker 19.03.8 on ubuntu 18.04

b. Command:

caddy run --config /etc/caddy/Caddyfile --adapter caddyfile

d. My complete Caddyfile or JSON config:

#admin localhost:2019
localhost:443 {
reverse_proxy backend:8080
  tls internal
  log {
    output file /var/log/access.log {
      roll_size 1gb
      roll_keep 5
  handle_errors {
    @404 {
      expression int({http.error.status_code}) == 404
      # expression {http.error.status_code} == '404'
      respond @404 "Oops 404 Try again"

3. The problem I’m having:

I was expeting to see Oops Try again when getting non existing address

$ curl -k https://localhost/nonexists
{"timestamp":"2020-04-25T15:15:44.247+0000","status":404,"error":"Not Found","message":"No message available","path":"/nonexists"}

(this message is exactly the same when I curl http without passing trough caddy proxy)

4. Error messages and/or full log output:

and the log from caddy and /var/log/access.log
2020/04/25 15:15:44.249 ERROR http.log.access.log0 handled request {“request”: {“method”: “GET”, “uri”: “/nonexists”, “proto”: “HTTP/2.0”, “remote_addr”: “”, “host”: “localhost”, “headers”: {“User-Agent”: [“curl/7.58.0”], “Accept”: ["/"]}, “tls”: {“resumed”: false, “version”: 772, “ciphersuite”: 4865, “proto”: “h2”, “proto_mutual”: true, “server_name”: “localhost”}}, “common_log”: “ - - [25/Apr/2020:15:15:44 +0000] “GET /nonexists HTTP/2.0” 404 130”, “latency”: 0.006254959, “size”: 130, “status”: 404, “resp_headers”: {“Vary”: [“Origin”, “Access-Control-Request-Method”, “Access-Control-Request-Headers”], “Content-Type”: [“application/json”], “Date”: [“Sat, 25 Apr 2020 15:15:44 GMT”], “Server”: [“Caddy”]}}

5. What I already tried:

expression {http.error.status_code} == ‘404’
I wish caddy serve my 404.html page, but I am tring to do handle the error first (and then try to with file-server/rewrite if needed…)
I tried already this (with no luck)

can you help me or point to the right documentation please?

Welcome, @Pierluigi_Di_Lorenzo!

The handle_errors directive currently only handles the case when a handler has an error, not when a client has an error. So, 404 error is not an error on the server, so it is returned to the client as-is. If a handler returns an error value, then handle_errors will be invoked.

We’re improving error handling after the 2.0 release to accommodate more use cases.

Hi matt!
thanks for super-fast reply!

404 error is not an error on the server, so it is returned to the client as-is.

This was what I suspected… I asked just to be sure.
I will wait next releases, in the meantime we will handle the error from the server caddy is proxing to.
In fede, Pierluigi

@matt would this be possible to hack it by having a route block that matches on 404 after the proxy then handling it with respond?

Request matchers can’t match on responses. Response matching is a special case that some handlers implement if they’re wrapping the response.

1 Like

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