Django static assets not getting served in caddy v2

1. Caddy version (caddy version):


2. How I run Caddy:

caddy start

a. System environment:


d. My complete Caddyfile or JSON config:


root * /path/to/static/
encode zstd gzip

3. The problem I’m having:

Not able to serve static files

ERROR	http.log.access	handled request	{"request": {"method": "GET", "uri": "/static/blog/js/materialize.min.js",

I have tried file_server /static/* but that is also not working

The issue is that the reverse_proxy is handling all requests before it reaches file_server. Caddy doesn’t know otherwise which requests should avoid using the proxy and which should be handled via the file server.

When using the Caddyfile, directives are handled in a specific order, described in the following docs page. Specifically, reverse_proxy is ordered higher than file_server, so it’ll get handled first if a request matched by it (most directives match all requests by default).

To solve this, you’ll need to use a named matcher that excludes the reverse_proxy directive from handling requests to /static/*.

It’ll look something like this:

@notStatic {
	not path /static/*
reverse_proxy @notStatic

Thanks for detailed explanation, so I followed the approach and now request for static assets is not giving any errors but assets are still not getting delivered to client.

Here is my full caddy config now,

hostname {
    file_server /path/to/static/*
    encode zstd gzip
    @css {
        path_regexp .css
    @js {
        path_regexp .js
    header @css Content-Type text/css
    header @js Content-Type text/js
    @notStatic {
        not path /static/*
    reverse_proxy @notStatic

In client, returned js/css files are empty but I have checked the path and files are there and non-empty so not sure what I am doing wrong here. Any help would be appreciated.

And I running caddy as root so caddy would have access to all the files too.

You need to use the root directive like you did in your original post. file_server doesn’t take the path as the first argument (it should probably warn though, that’s a minor parsing bug).

Thanks, that was it!

1 Like

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