How to configuare subpath in Caddyfile

1. The problem I’m having:

Subpath doesn’t work

2. Error messages and/or full log output:

No error information

3. Caddy version:

v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

4. How I installed and ran Caddy:

I installed and ran caddy step by step according to this article

a. System environment:

Debian 11

b. Command:

systemctl start caddy

c. Service/unit/compose file:

d. My complete Caddy config:

my-domain-name.com {
 tls email@email.com
 handle_path / {
  redir https://www.google.com
 }
 handle_path /subpath {
  reverse_proxy 127.0.0.1:1234
 }
}

What I want to achieve is that when visiting my-domain-name.com, it will be redirected to https://www.google.com, but when visiting my-domain-name.com/subpath , I will visit 127.0.0.1:1234, but now all visits will be redirected to https://www.google.com.

How to fix this? Thank you.

5. Links to relevant resources:

Path matching in Caddy is exact. A /subpath matcher only matches /subpath exactly, and nothing else. You need a * to match paths under that.

You probably want this:

example.com {
	handle_path /subpath* {
		reverse_proxy 127.0.0.1:1234
	}

	handle {
		# whatever default routing
	}
}

Thank you so much.

Actually, the sort is what I need. I should put /subpath handle block before / handle block.

BTW, there’s a typo in your code. You missed / in second handle block.

No, the ordering does not matter, because Caddy sorts directives according to this algorithm:

I still recommend putting it in your config in the order that it will actually run though, because it makes it easier to read the config. But it has no runtime difference in behaviour.

No, that’s correct. You should not use / for a fallback handle, because that would only match exactly / and nothing else. Which is pretty much never useful.

Please review how request matching works: Request matchers (Caddyfile) — Caddy Documentation

That’s weird.

I only updated the ordering, and everything works.

My whole Caddyfile

example.com {
 tls email@email.com
 handle_path /subpath {
  reverse_proxy 127.0.0.1:1234
 }
 handle_path / {
  redir https://www.google.com
 }
}

If I don’t add / in second handle block, I will get an error. Run journalctl -xe

░░ Support: https://www.debian.org/support
░░
░░ A start job for unit caddy.service has begun execution.
░░
░░ The job identifier is 1658.
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: caddy.HomeDir=/var/lib/caddy
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: caddy.AppDataDir=/var/lib/caddy>
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: caddy.AppConfigDir=/var/lib/cad>
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: caddy.ConfigAutosavePath=/var/l>
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: caddy.Version=v2.6.4 h1:2hwYqiR>
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: runtime.GOOS=linux
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: runtime.GOARCH=amd64
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: runtime.Compiler=gc
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: runtime.NumCPU=1
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: runtime.GOMAXPROCS=1
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: runtime.Version=go1.20
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: os.Getwd=/
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: LANG=C.UTF-8
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: PATH=/usr/local/sbin:/usr/local>
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: NOTIFY_SOCKET=/run/systemd/noti>
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: HOME=/var/lib/caddy
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: LOGNAME=caddy
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: USER=caddy
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: INVOCATION_ID=6a9c0489a8a843f39>
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: JOURNAL_STREAM=8:243333
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: {"level":"info","ts":1682383673>
Apr 25 08:47:53 ip-172-26-13-41 caddy[48823]: Error: adapting config using ca>
Apr 25 08:47:53 ip-172-26-13-41 systemd[1]: caddy.service: Main process exite>
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ An ExecStart= process belonging to unit caddy.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
Apr 25 08:47:53 ip-172-26-13-41 systemd[1]: caddy.service: Failed with result>
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ The unit caddy.service has entered the 'failed' state with result 'exit-co>
Apr 25 08:47:53 ip-172-26-13-41 systemd[1]: Failed to start Caddy.

BTW, my needs are

  1. subpath is exactly what I want, and only if it matches exactly, can it be directed to 127.0.0.1:1234。Therefore, there should be no need to change subpath* here.

  2. Visiting example.com will be redirected to https://www.google.com

Thank you.

I guarantee that wasn’t what fixed it.

You can run caddy adapt --pretty --config /etc/caddy/Caddyfile on both versions of your config, you’ll see they produce identical JSON config.

handle and handle_path are not the same. The handle_path directive requires a path matcher. Its job is to strip a path prefix from the request before handling. Stripping / as a prefix doesn’t make sense.

Your logs are truncated, so the error message is hidden. Notice the > at the ends of the lines which indicate truncation. See the docs for the correct command to see your logs:

I see, sorry for my mistake on handle and handle_path.

I will try later, thank you so much.

1 Like

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