How to add multiple root directives with different routes?

1. Output of caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. How I run Caddy:

a. System environment:

Windows 11 Pro (22H2)

b. Command:

caddy run --watch

c. Service/unit/compose file:

caddy run --watch

d. My complete Caddy config:

        storage file_system .\caddy

(php_filebrowser) {
        # Add trailing slash for directory requests
        @canonicalPath {
                file {path}/index.php
                not path */
        redir @canonicalPath {path}/ 308

        # If the requested file does not exist, try index files
        @indexFiles file {
                try_files {path}/index.php {path}/index.html
        rewrite @indexFiles {http.matchers.file.relative}

        # Proxy PHP files to the FastCGI responder
        @phpFiles path *.php
        reverse_proxy @phpFiles localhost:9000 {
                transport fastcgi {
                        split .php
        file_server browse

localhost, {
        log {
                output file .\log\caddy.log
                format console
        route /www/* {
                root .\www
        route /gh/* {
                root C:\Projects
        import php_filebrowser

3. The problem I’m having:

I’m trying to have two endpoints, each with a different root directory. Both should be routed like the “php_filebrowser” snippet. Localhost returns the php_filebrowser with the root directory of caddy (where the binary is). “localhost/www”, “localhost/www/” and “localhost/www/*” return a 404 error. Same with the “gh” endpoint.

4. Error messages and/or full log output:

$ caddy validate
2022/10/28 23:56:13.552 INFO    using adjacent Caddyfile
2022/10/28 23:56:13.553 WARN    Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies   {"adapter": "caddyfile", "file": "Caddyfile", "line": 2}
2022/10/28 23:56:13.554 INFO    http    server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
2022/10/28 23:56:13.554 INFO    http    enabling automatic HTTP->HTTPS redirects        {"server_name": "srv0"}
2022/10/28 23:56:13.554 INFO    tls.cache.maintenance   started background certificate maintenance      {"cache": "0xc00077f960"}
2022/10/28 23:56:13.555 INFO    tls.cache.maintenance   stopped background certificate maintenance      {"cache": "0xc00077f960"}
Valid configuration

5. What I already tried:

Tried different variations but couldn’t get it running as it should.

6. Links to relevant resources:


Remember that for static files, the URI path is appended to the root path from the config. So if the URI includes /www/ then the root should have a www folder within it (etc).

You can use handle_path instead of route to strip the matched path prefix from the request before continuing handling.

Is there any particular reason you’re using the expanded-form instead of php_fastcgi? I think you’re only doing this to customize the try_files, but you can actually do this directly from php_fastcgi with its try_files subdirective.

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