Caddy for serving minified JS app/ staticfiles from folder/attachments from folder

Hi all, trying to migrate from nginx to caddy, and encountering some issue for my usecase, maybe someone can help clarify some things for me

1. The problem I’m having:

I’m having an issue with configuring Caddy for my usecase. It must proxy pass request to api container, do try_files for minified JS builded react app, and serve static files from 2 of folders static and attachments.
All parts except serving static files from static and attachment path working.

2. Error messages and/or full log output:

404 not - found

3. Caddy version: 2.8 docker image tag

4. How I installed and ran Caddy:

Its in a docker container

a. System environment:

Docker

d. My complete Caddy config:

{$SITE_URL}/api/* {
	reverse_proxy /api/* api:8000
}
{$SITE_URL}/admin/* {
	reverse_proxy /admin/* api:8000
}
{$SITE_URL} {
	root * /usr/share/web
	file_server
	try_files {path} /index.html
}
{$SITE_URL}/static/* {
	root * /static
	file_server
}
{$SITE_URL}/attachments/* {
	root * /attachments
	file_server
}

5. Links to relevant resources:

Config file in repo remembrancer/docker/caddy/Caddyfile at dev · HomeLabHQ/remembrancer · GitHub

Don’t use path matchers in the site address. That’s been deprecated for a long time, and is slated for removal.

Use the handle directive to make mutually exclusive routes within one site block.

See here in the docs, an example of a single-page-app serving an API and a frontend:

1 Like

Thanks, but can i define different root for file_server in each handle block?

Yes.

{$SITE_URL} {
	handle /api/* {
		reverse_proxy api:8000
	}
	handle /admin/* {
		reverse_proxy /admin/* api:8000
	}
	handle {
		root * /usr/share/web
		file_server
		try_files {path} /index.html
	}
	handle /static/* {
		root * /static
		file_server
	}
	handle /attachments/* {
		root * /attachments
		file_server
	}
}

So basically this should work?
In my case static and attachment routes still return minified JS from root route ;(

For these, you probably want handle_path instead. When Caddy assembles the path to look on disk, it takes the root then appends the request path. So it would be looking in /static/static unless you strip the request path away. handle_path does that for you. Read the docs for a deeper explanation.

You don’t need the /admin/* in here since the handle matches that already.

Also, you could merge these two API routes together:

	@api path /api/* /admin/*
	handle @api {
		reverse_proxy api:8000
	}

Show an example request. Turn the debug global option and show your logs. Show evidence of the problem.

1 Like

@francislavoie thanks for help, i achieved some progress

{$SITE_URL} {
	@api path /api/* /admin/*
	handle @api {
		reverse_proxy api:8000
	}
	handle {
		root * /usr/share/web
		file_server
		try_files {path} /index.html
	}
	handle_path /static/* {
		root * /static
		file_server
	}
	handle_path /attachments/* {
		root * /attachments
		file_server
	}
}

With this config attachments is working, but static is not, the only differences that static have sub-folders. so the path is like
https://remembrancer.dufran.org/static/admin/css/dark_mode.css
Is there a way to make static directory recursive?

Where is that file stored on disk?

Again, please enable the debug global option and show your logs after making a request that misbehaves. Seeing the logs will explain what’s going on.

Many thanks for all the help, seems to be working. I think it was cache related issue on cloudflare part.

1 Like