All traffic is routed to sub route despite fallback file_server

1. Caddy version (caddy version):

v2.2.1 h1:Q62GWHMtztnvyRU+KPOpw6fNfeCD3SkwH7SfT1Tgt2c=

2. How I run Caddy:

a. System environment:

Ubuntu 20.04.1 LTS

b. Command:

I run caddy with systemctl and reload the config with systemctl reload caddy

d. My complete Caddyfile or JSON config:

cdn1.hanomado.com

root * /mnt/volume-hel1-1/audiofiles

route /api/*{
 uri strip_prefix /api
 reverse_proxy localhost:3000
}
file_server

3. The problem I’m having:

All request are forwarded to the reverse proxy and the static files are not served. Without the route /api/* block the files are served as expected.

5. What I already tried:

Reordered the config in several ways & tried with reverse_proxy directive

You’re missing a space between * and {. Spaces are important in the Caddyfile for it to be correctly parsed.

That said, I recommend using handle_path instead of route here, because it includes strip_prefix logic.

Also FYI, re-ordering things in the Caddyfile will have no effect because during the Caddyfile adapt step, the directives are sorted according to this predetermined order:

I would write your config like this:

cdn1.hanomado.com {

	handle_path /api/* {
		reverse_proxy localhost:3000
	}

	handle {
		root * /mnt/volume-hel1-1/audiofiles
		file_server
	}
}

Thank you so much for your answer and fast help!
Did not know about the spaces being important.

1 Like

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