Rewrite condition for file not found when file_server's root is not current directory

1. Caddy version (caddy version):

v2.2.1

2. How I run Caddy:

sudo caddy run --watch
and open browser to http://localhost:8456

a. System environment:

Ubuntu 20.04

b. Command:

sudo caddy run --watch

d. My complete Caddyfile or JSON config:

{
	admin off
	http_port 8456
}
localhost:8456 {
	file_server browse {
		root ./dist
	}
	@notFound {
		not file
	}
	rewrite @notFound default.jpg
}

3. The problem I’m having:

This is my folder structure
2020-10-20_20:18:59
I expect the test page to show default.jpg for missing image.
But, all images fall back to default.jpg

5. What I already tried:

I’ve try move Caddyfile to /dist folder, remove root ./dist , execute caddy run from that directory everything works fine.
I just want to know how to propery set rewrite when file not found to something when root is not current working directory.

Thank you.

I think you want to use the root directive rather than the file_server subdirective, because that will set root for everything (including the file matcher which uses root to find files on disk)

{
	admin off
	http_port 8456
}
localhost:8456 {
	root * ./dist

	@notFound not file
	rewrite @notFound default.jpg

	file_server browse
}
2 Likes

Thank you ! It work :smile:

1 Like

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