Cloudfront reverse proxy with rewrite rules for files/directories

I have Caddy acting as a reverse-proxy to Cloudfront. If the user requests a path with no file extension or trailing slash:

/slug

I would like Caddy to attempt to get /slug.html from Cloudfront, and if that is not available, try to get /slug/index.html. If that is not available, then finally respond with the 404. Is this possible?

You’re looking for try_files. Next time, please fill out the template instead of deleting it.

1 Like

Take a look at handle_response facility provided by reverse_proxy directive. It should be enough to achieve the desired behavior: reverse_proxy (Caddyfile directive) — Caddy Documentation

Just a quick example:

reverse_proxy <cloudfront_host> {
	@error status 404
	handle_response @error {
		root    * /path/to/files
		rewrite * {path}/index.html
		file_server
	}
}

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