Handle_path being ignored when paired with handle in same block

1. Caddy version (caddy version):

v2.4.6

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
	file_server
}

*.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 {
		reverse_proxy 127.0.0.1:8756
	}
}

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.)

2 Likes

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:

2 Likes

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