V2: does `file_server` have to be used with `root`?

I had been trying to just use

home.waynewerner.com {
    root * /var/www/waynewerner.com/site
}

in my Caddyfile, but this question led me to the idea of:

home.waynewerner.com {
    root * /var/www/waynewerner.com/site
    file_server
}

I had previously experimented and found that

home.waynewerner.com {
    file_server {
        root * /var/www/waynewerner.com/site
    }
}

worked, too.

If I want to serve static files do I have to use file_server in my Caddyfile?

Yep.

The root directive just sets a variable that can be used by other parts of the handler chain (for example, try_files or templates). If you use root inside the file_server directive, only that file server handler will use/know that root path.

I’m kinda surprised:

file_server {
    root * /var/www/waynewerner.com/site
}

worked, since I wouldn’t expect it to with the * there. That shouldn’t accept a matcher token…

The docs specify that it does accept a matcher token, at least if I understand correctly?

root [<matcher>] <path>

It would be nice to get a warning in a situation like that, though, that just says something like this:

“Are you sure you meant to do nothing with this server? Maybe you wanted a file_server?”

Here’s the rub, though;

In v2, the root directive supports a matcher, but the root subdirective of the file_server directive does not. i.e. Should work when you set the root for the whole site, shouldn’t work if you set it just for the file server.

file_server

Enables static file server, optionally with directory browsing.

file_server [<matcher>] [browse] {
	hide <files...>
	index <files...>
	browse [<template_file>]
	root <path>
}

https://github.com/caddyserver/caddy/wiki/v2:-Documentation#file_server

Could be cool as an [INFO] output of some kind if there’s no terminating handler (i.e. a handler that generates actual content somehow and returns it).

1 Like

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