How to read files from a folder stored oustide the webroot dir?

1. Caddy version:

caddy v2.6.4

2. How I installed, and run Caddy:

apt install caddy

a. System environment:

ubuntu jammy x64

b. Command:

systemd

c. Service/unit/compose file:

systemd service file not modified

d. My complete Caddy config:

test.domain.foo {
        # Set this path to your site's directory.
        root * /srv/www/test.domain.foo/wwwroot

        import ./caddy.d/processwire.conf

        # what I am trying 👇
        handle_path /securefiles/* {
          file_server /home/ubuntu/Data/SecureFiles
        }

        # Enable the static file server.
        file_server

        php_fastcgi 127.0.0.1:9000

        tls root@domain.foo
}

3. The problem I’m having:

The root dir is /srv/caddy/test.domain.foo/wwwroot and I would like to be able to read files from an absolute path stored outside the host directory, from /home/ubuntu/Data/SecureFiles/

What’s the right config to achieve it please ?

4. Error messages and/or full log output:

The following error come from the cmf, anyway, the dir /home/ubuntu/Data/SecureFiles/ is chmod’d to 777 for testing purpose, with the same user:group as caddy is running.

Secure File: Path '/' does not exist or is not writeable

5. What I already tried:

After reading some posts here, I tried to play with

handle_path /securefiles/* {
  file_server /home/ubuntu/Data/SecureFiles
}

It’s not possible to read outside of a configured root. You need to tell Caddy what the root is. So in that handle_path, you should use the root directive again to redefine the root for that route.

But, you’ll probably get permissions issues from that anyway. The caddy user probably doesn’t have permission to read (or execute directories to traverse them) of the ubuntu user. You should move those files to somewhere that can be accessed by the caddy user, like another directory in /srv for example.

Keep in mind that here, you’re setting a path matcher for the file_server directive. The first token to most directives is an optional matcher token. See the syntax file_server (Caddyfile directive) — Caddy Documentation

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