File_server path_regexp not working properly

1. Caddy version (caddy version):

2.5.1

2. How I run Caddy:

systemctl / official caddy installation
I reload caddy with “systemctl reload caddy”
I restart caddy with “systemctl restart caddy”

a. System environment:

Ubuntu 22.04 LTS

b. Command:

Through systemctl restart caddy / systemctl reload caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

systemctl restart caddy

d. My complete Caddyfile or JSON config:

somedomain {
    root * /var/www/somedomain
    @publicPaths {
        path_regexp .*/Public($|/.*)
    }

    file_server @publicPaths
}

3. The problem I’m having:

Any files in a Subfolder named Public should be accessible through the file_server, so I’ve tried multiple combinations of path_regexp But not a single one is working.

4. Error messages and/or full log output:

Can’t access files, e.g.:
somedomain/example/Public/abc.jpg
somedomain/example/Public/somewhere/abc.jpg
somedomain/Public/somewhere/abc.jpg

5. What I already tried:

I’ve tried different regex, but no regex seems to work, but the regular path for a specific Public Folder works, e.g.:

somedomain {
    root * /var/www/somedomain
    @publicPaths {
        path /example/Public/*
    }

    file_server @publicPaths
}

I’ve tested multiple regex (regex101.com), the regex I tested seem to work mostly on php, js, bash and golang, except on caddy. So what am I doing wrong? The documentation about this is a bit sparse, thank you very much.

Caddy uses Go’s regexp (because Caddy is written in Go), so definitely test against the Golang flavor in regex101.

What’s your goal with this matcher exactly? You can just use a path matcher with multiple paths if you want. Try this:

root * /var/www/somedomain

@public path /example/Public/* /Public/*
file_server @public

Keep in mind that you should handle other requests in some way, otherwise Caddy will just respond with an empty 200 status response.

1 Like