Handle_path being ignored when paired with handle in same block

1. Caddy version (caddy version):


2. How I run Caddy:

a. System environment:

Ubuntu Server 18.04

b. Command:

caddy run --config /etc/caddy/Caddyfile

d. My complete Caddyfile or JSON config:

	email aditya@diwakar.io
	acme_dns cloudflare {env.CLOUDFLARE_AUTH}

aditya.diwakar.io {
	root * /var/www/html

*.diwakar.io *.aditya.diwakar.io {
	@api host api.aditya.diwakar.io api.diwakar.io
	handle @api {
		handle_path /cot* {
			redir "https://cot-reports.americas.diwakar.io{uri}"
		handle {
			respond "api matched {host}{uri}"

*.americas.diwakar.io {
	@cot host cot-reports.americas.diwakar.io
	handle @cot {

3. The problem I’m having:

I am trying to redirect all of the following api.aditya.diwakar.io/cot*, api.diwakar.io/cot*cot-reports.americas.diwakar.io/*. Essentially a simple domain change ignoring the /cot* prefix. The above configuration is what I believe should work, using handle_path that automatically would strip the prefix.

However, none of the URLs are getting matched with the handle_path, and instead are getting matched with the catch-all handle at the bottom of the api block. Running curl -iL https://api.aditya.diwakar.io/cot/disaggregated/067651 leads to:

HTTP/2 200 
server: Caddy
content-length: 58
date: Sun, 12 Dec 2021 07:24:50 GMT

api matched api.aditya.diwakar.io/cot/disaggregated/067651

4. Error messages and/or full log output:

No error messages, just incorrect behavior as mentioned in 3.

5. What I already tried:

Removing the second handle block in the handle @api block seems to fix the behavior and it correctly redirects to cot-reports.americas.diwakar.io/..., but it is not working with what I believe is the proper configuration. Of course, I am new to Caddy and could be making a silly mistake. Thanks! :smiley:

1 Like

Interesting. If you adapt your config to JSON, you’ll see that your handle block gets sorted before the handle_path for some reason. That shouldn’t happen. There’s a bug in there somewhere. I’ll have to investigate later.

As a workaround, you can probably use route @api instead for the wrapping one, if you don’t need mutual exclusivity/fallback for the outer part.

1 Like

Thanks @francislavoie for the response! I do unfortunately need a fallback on the outer subdomain. Do you know of any other workarounds in the meanwhile?

At first glance, I tried:

@cot path /cot*
handle @cot {
    uri strip_prefix /cot
    redir https://cot-reports.americas.diwakar.io{uri}

However, this doesn’t correctly redirect and cot still shows up in the redirected URL. Maybe I’m missing something?

With regard to a potential bug, should I go ahead and open a GitHub issue so this is tracked?

That’s okay, I’ll look into this right now, I have a hunch as to what’s going on. I’ll link back to this issue when I have a potential fix.

I think this might work:

@cot path_regexp cot ^/cot(.*)$
handle @cot {
	redir https://cot-reports.americas.diwakar.io{re.cot.1}?{query}
1 Like

Francis, thank you :pray: (Been a bit busy over here lately.)


Sounds good, thanks. I was leaning towards path_regexp anyways. Glad I /potentially/ uncovered some unintended behavior.

1 Like

Alright, found the problem, made a fix:


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