I can’t replicate your problem. Not sure whether you’re talking about the Basic or Bearer authentication, so here’s the test case for both:
Caddyfile
:80 {
@Matcher1 {
header X-Client-ID 1
}
handle @Matcher1 {
reverse_proxy 127.0.0.1:8081
}
@Matcher2 {
header X-Client-ID 2
}
handle @Matcher2 {
reverse_proxy 127.0.0.1:8082
}
}
## Authentication Basic
:8081 {
basic_auth {
## Username "Bob", password "hiccup"
Bob $2a$14$Zkx19XLiW6VYouLHR5NmfOFU0z2GTNmpkT/5qqR7hx4IjWJPDhjvG
}
respond "Welcome, {http.auth.user.id}" 200
}
## Authentication Bearer
:8082 {
@bearer {
header Authentication "Bearer 123"
}
handle @bearer {
respond "Authenticated" 200
}
respond "Not Authenticated" 403
}
Basic
## Success - Correct credentials
$ curl http://localhost -H 'X-Client-ID: 1' -u 'Bob:hiccup' -v
* Host localhost:80 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:80...
* Connected to localhost (::1) port 80
* using HTTP/1.x
* Server auth using Basic with user 'Bob'
> GET / HTTP/1.1
> Host: localhost
> Authorization: Basic Qm9iOmhpY2N1cA==
> User-Agent: curl/8.14.1
> Accept: */*
> X-Client-ID: 1
>
* Request completely sent off
< HTTP/1.1 200 OK
< Content-Length: 12
< Content-Type: text/plain; charset=utf-8
< Date: Wed, 25 Jun 2025 14:44:46 GMT
< Server: Caddy
< Via: 1.1 Caddy
<
* Connection #0 to host localhost left intact
Welcome, Bob
## Failure - Incorrect credentials
$ curl http://localhost -H 'X-Client-ID: 1' -u 'Random:user' -v
* Host localhost:80 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:80...
* Connected to localhost (::1) port 80
* using HTTP/1.x
* Server auth using Basic with user 'Random'
> GET / HTTP/1.1
> Host: localhost
> Authorization: Basic UmFuZG9tOnVzZXI=
> User-Agent: curl/8.14.1
> Accept: */*
> X-Client-ID: 1
>
* Request completely sent off
< HTTP/1.1 401 Unauthorized
< Content-Length: 0
< Date: Wed, 25 Jun 2025 14:45:12 GMT
< Server: Caddy
< Via: 1.1 Caddy
* Basic authentication problem, ignoring.
< Www-Authenticate: Basic realm="restricted"
<
* Connection #0 to host localhost left intact
Bearer
## Success - Bearer Auth present
$ curl http://localhost -H 'X-Client-ID: 2' -H 'Authentication: Bearer 123' -v
* Host localhost:80 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:80...
* Connected to localhost (::1) port 80
* using HTTP/1.x
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/8.14.1
> Accept: */*
> X-Client-ID: 2
> Authentication: Bearer 123
>
* Request completely sent off
< HTTP/1.1 200 OK
< Content-Length: 13
< Content-Type: text/plain; charset=utf-8
< Date: Wed, 25 Jun 2025 14:47:02 GMT
< Server: Caddy
< Via: 1.1 Caddy
<
* Connection #0 to host localhost left intact
Authenticated
## Failure - Bearer Auth absent
$ curl http://localhost -H 'X-Client-ID: 2' -v
* Host localhost:80 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:80...
* Connected to localhost (::1) port 80
* using HTTP/1.x
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/8.14.1
> Accept: */*
> X-Client-ID: 2
>
* Request completely sent off
< HTTP/1.1 403 Forbidden
< Content-Length: 17
< Content-Type: text/plain; charset=utf-8
< Date: Wed, 25 Jun 2025 14:47:44 GMT
< Server: Caddy
< Via: 1.1 Caddy
<
* Connection #0 to host localhost left intact
Not Authenticated
I think your back-end server might not be properly processing the header.
Try to enable debug
or log
, and you should see in your logs something like this for the Basic Authentication:
..., "headers": {"Authorization": ["REDACTED"], ...
or this for the Bearer Authentication:
..., "Authentication": ["Bearer 123"], ...
If you see that, it means Caddy receives it from the client and passes it to the upstream.