File_server with handle?

1. Caddy version (caddy version):


2. How I run Caddy:

Using docker-compose, details are below.

a. System environment:

OS: Ubuntu 20.04 focal on WSL2
Docker 20.10.16
docker-compose 1.29.2

b. Command:

docker-compose up

c. Service/unit/compose file:


    image: caddy:2
    - published: 80
      target: 80
    - ./data/static:/static:ro
    - ./proxy/Caddyfile:/etc/caddy/Caddyfile:ro
version: '3.8'

d. My complete Caddyfile or JSON config:

http://localhost {
	handle /static/* {
		root * /static
		file_server browse

(the site will eventually run behind a proxy that handles SSL)

3. The problem I’m having:

I’m trying to serve files out of a volume mounted in the container at /static, and make them available at the request path of /static.

The Caddyfile config above responds with a 404 at the /static/ path:

$ http http://localhost/static/
HTTP/1.1 404 Not Found
Content-Length: 0
Date: Sun, 22 May 2022 14:57:12 GMT
Server: Caddy

4. Error messages and/or full log output:

$ docker-compose up
Creating network "site_default" with the default driver
Creating site_proxy_1 ... done
Attaching to site_proxy_1
proxy_1  | {"level":"info","ts":1653231683.10917,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
proxy_1  | {"level":"info","ts":1653231683.1127582,"logger":"admin","msg":"admin endpoint started","address":"tcp/localhost:2019","enforce_origin":false,"origins":["//","//localhost:2019","//[::1]:2019"]}
proxy_1  | {"level":"info","ts":1653231683.1132877,"logger":"tls.cache.maintenance","msg":"started background certificate maintenance","cache":"0xc000260000"}
proxy_1  | {"level":"warn","ts":1653231683.1140573,"logger":"http","msg":"server is listening only on the HTTP port, so no automatic HTTPS will be applied to this server","server_name":"srv0","http_port":80}
proxy_1  | {"level":"info","ts":1653231683.1159847,"logger":"tls","msg":"cleaning storage unit","description":"FileStorage:/data/caddy"}
proxy_1  | {"level":"info","ts":1653231683.1162305,"logger":"tls","msg":"finished cleaning storage units"}
proxy_1  | {"level":"info","ts":1653231683.1164582,"msg":"autosaved config (load with --resume flag)","file":"/config/caddy/autosave.json"}
proxy_1  | {"level":"info","ts":1653231683.116496,"msg":"serving initial configuration"}

There are no further messages in the caddy logs when the request is processed.

5. What I already tried:

I have tried omitting the handle directive to serve the static files at the site root to verify that the file_server directive is functioning as expected, and that much works (although it doesn’t serve the files at /static as desired):


http://localhost {
	root * /static
	file_server browse


$ http http://localhost --headers
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Date: Sun, 22 May 2022 15:07:49 GMT
Server: Caddy
Transfer-Encoding: chunked

(body omitted for brevity, but it’s the standard caddy browse page showing the static files)

I feel like I’ve read through the documentation for file_server ten times at this point, and I must be missing something because I can’t see anything mentioned there that would imply the directive shouldn’t work in a handle block, but it apparently does not?

Is this a bug, or working as intended, or have I made a silly mistake somewhere here?

I think you’re looking for handle_path, here, not handle.

The way file_server works, is it takes the request path, and appends it to the defined root. So Caddy is looking in /static/static/ for your files, when you use handle.

The handle_path directive instead will strip the matched path from the front of the path before continuing handling, removing that duplicated segment.

See the docs:

1 Like

Aha! Thank you so much, that’s exactly what I needed… and the docs for file_server do say that, but it didn’t click for me, so thank you.

1 Like

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