Multiple Root for single domain

Hi guys I seem to misunderstand something about how to configure caddy (new to it previously apache/nginx only) despite googling a lot. How to properly set up different file server root dirs for different subpaths of same domain? Thanks a lot

1. My Caddy version (caddy version):

v2.0.0-rc.3 h1:z2H/QnaRscip6aZJxwTbghu3zhC88Vo8l/K57WUce4Q=

2. How I run Caddy:

As a service (systemctl enable caddy)

a. System environment:

systemd, raspbian (from the package as per caddy website instructions)

b. Command:

c. Service/unit/compose file:

d. My complete Caddyfile or JSON config:

domain.com www.domain.com {
     root /staging/* /path/to/staging
     root * /path/to/production
     file_server
}

3. The problem I’m having:

domain.com/staging -> 404
domain.com/staging/ -> 404
domain.com/staging/index.html ->404

the domain.com/everthingelse works as expected.

The file /path/to/staging/index.html exists and is readable by webserver (permissions).

4. Error messages and/or full log output:

5. What I already tried:

google, google, google

6. Links to relevant resources:

I was able to get closer with this:

domain.com www.domain.com {
        route /staging* {
                uri strip_prefix /staging
                file_server {
                        root /path/to/staging
                }
        }
        file_server * {
                root /path/to/production
        }
}

However not sure this is correct/best way and still has one issue:
domain.com/staging redirects me to domain.com/
domain.com/staging/ and domain.com/staging/index.html does work as expected serving file /path/to/staging/index.html

The route approach is the ideal way to deal with that. In addition, you’d probably want to add a redirect to deal with /staging to add a trailing slash.

redir /staging /staging/ 308

You’d put this before your route block. It will match on exactly /staging, and do a 308 (permanent) redirect to /staging/. After doing that, you can adjust your route matcher to /staging/* which will prevent paths like /stagingabcd from being handled by the route.

1 Like

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