Using multiple root to handle multiple path

Hi, I’m trying to set multiple roots to be handling multiple paths, but the /about handler does not work. it gives a localhost page not found.
My Caddyfile below

:2016 { 
 
        handle /about { 
                root * /path/1 
        } 
        handle { 
                root * /path/2 
        }        
        log { 
                output stdout 
        } 
        file_server 
}

This is my error log

ERROR	http.log.access.log0	handled request	{"request": {"method": "GET", "uri": "/about", "proto": "HTTP/1.1", "remote_addr": "[::1]:41572", "host": "localhost:2016", "headers": {"Sec-Fetch-Site": ["none"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-User": ["?1"], "Sec-Fetch-Dest": ["document"], "Connection": ["keep-alive"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"], "Accept-Language": ["en-US,en;q=0.9"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Accept-Encoding": ["gzip, deflate, br"]}}, "common_log": "::1 - - [01/Jul/2020:08:34:19 +0800] \"GET /about HTTP/1.1\" 404 0", "duration": 0.00006654, "size": 0, "status": 404, "resp_headers": {"Server": ["Caddy"]}}

Thanks for the help

What is the file on disk that you expect to get served? Caddy will be looking for a file on disk at the path /path/1/about. Caddy’s file server appends the path to the root.

If you don’t want /about to be part of the path it looks on the filesystem, then you should use the handle_path directive (available since Caddy v2.1) which essentially is the same as handle plus a uri strip_prefix directive which rewrites the path.

Also note that in Caddy v2, path matching is exact-match, so if you need to serve any pages like /about/me then /about will not match that, you would need to use /about* or /about/*.

2 Likes

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