Config for a SPA + Custom Routes

You can simplify this by using handle_path instead, which has the strip_prefix built-in:

handle_path /couch/* {
	reverse_proxy localhost:5984
}

This will run before your route or handle_path blocks, because Caddy sorts directives based on this directive order:

Instead, you can write it like this:

eduquilt.com {
	handle_path /couch/* {
		reverse_proxy localhost:5984
	}

	handle_path /express/* {
		reverse_proxy localhost:3000
	}

	handle {
		root * /home/skuilder/www

		try_files {path} {path}/ /index.html

		file_server
	}
}

Using handle blocks ensures that those parts are handled mutually exclusively (only the first handle or handle_path matched will run) and they are sorted according to the length of their path matcher, so one without a matcher will always run last.

2 Likes