Could my Caddyfile be improved?

I’m not sure your webp stuff will work as expected currently, because {path} will contain the file extension, so it will try to look for /foo/bar.jpg.jpg on disk for example, then /foo.bar.jpg.webp. The fix is to use path_regexp to make capture groups. That also allows you to combine them all together:

    @acceptWebp {
        header Accept image/webp*
        path_regexp image ^(.*).(jpe?g|png)$
    }
    handle @acceptWebp {
        try_files {re.image.0}.{re.image.1} {re.image.0}.webp
    }

So if the request is /foo/bar.jpeg, the first capture group will be /foo/bar and the second will be jpeg, so you can use those pieces to try first the file as requested, else the .webp instead.

Edit: I misread what you want when I wrote the above, I think you actually just want this:

    @acceptWebp header Accept image/webp*
    handle @acceptWebp {
        try_files {path}.webp
    }

try_files only rewrites the request if a file is found on disk, and your webp files include the original file extension, so this is probably enough. This will prioritize .webp files if they exist, otherwise it will get served by file_server normally.


    file_server browse
    file_server /grabs/*

I would re-order these two lines to make it clearer how it behaves - Caddy will sort same-directives based on their path matcher, so it will handle them in this order:

    file_server /grabs/*
    file_server browse

First of all, are you sure you need these header_up lines? Have you tried without them? It might work just fine, as long as your upstream service doesn’t explicitly expect localhost, and if it handles X-Forwarded-For already (which is the header more typically used by proxies for revealing the client IP)

    reverse_proxy localhost:8989 {
        header_up Host {http.reverse_proxy.upstream.hostport}
        header_up X-Real-IP {http.request.remote.host}
    }

If you do need them, since this sort of pattern is repeated a lot, so you could use a snippet with arguments to shorten this if you like:

(proxyWithHeaders) {
    reverse_proxy {args.0} {
        header_up Host {http.reverse_proxy.upstream.hostport}
        header_up X-Real-IP {http.request.remote.host}
    }
}

Then use it like this:

import proxyWithHeaders localhost:8989

I’m not sure I understand the question. You’re doing it correctly already, by adding the www. domain to your sites. Are you looking to redirect it? If so you can see the examples here:

You can definitely still do this.

Could you elaborate? I’m not sure I understand.