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.
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.
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
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.
Visiting example.com will be redirected to https://www.google.com
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: