Serving static assets as proxy

Caddy version: 2.5.1:

How I run Caddy: Using the official (alpine) Docker image.

Hello everyone,
I’m trying to set Caddy as Reverse Proxy in front of a Rails app and I’d like to make sure Caddy caches and serves the static assets (CSS, JS, images).

Based on this post my Caddyfile looks like

staging.inforlife.ch {
  encode zstd gzip
  file_server
  
@notStatic {
    not file
  }
  
  reverse_proxy @notStatic  app:3000
}

Is there a way I can test if my static assets are served by Caddy?

Thanks,
Sig

You can turn on access logs, I guess. Use the log directive, then watch your logs.

I’d recommend writing your config like this btw:

staging.inforlife.ch {
	encode zstd gzip

	@is-file file
	handle @is-file {
		root * /path/to/webroot
		file_server
	}
  
	handle {
		reverse_proxy app:3000
	}
}

Thanks for the reply.

My only doubt is what I should set as root since the Rails application runs on a different container (app).

Have a nice day,
Sig

root is just the path to the files on disk inside your Caddy container. It’s unrelated to your reverse_proxy. It just tells Caddy’s file_server where to read the files from.

That said, I realized I made a mistake – the root should be outside of the handle in that above config, because the file matcher also needs to know what the webroot is, to be able to find if the file does indeed exist.

staging.inforlife.ch {
	root * /path/to/webroot

	encode zstd gzip

	@is-file file
	handle @is-file {
		file_server
	}
  
	handle {
		reverse_proxy app:3000
	}
}
1 Like

Thanks for the reply.

1 Like

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