Nested file_server directive in handle block could not work

1. Caddy version (caddy version):

λ  caddy version
v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

2. How I run Caddy:

a. System environment:

macOS 10.15.7

b. Command:

brew services start caddy

c. Service/unit/compose file:

λ  cat /usr/local/etc/Caddyfile
# https://caddyserver.com/docs/caddyfile/directives/import#import
import sites-enabled/*.caddy

d. My complete Caddyfile or JSON config:

:9999 {
    log
    handle /www {
        file_server browse {
            root /usr/local/var/www/
            index none
        }
    }
    handle /aws {
        root * ~/Dropbox/AWS/
        file_server browse
    }
}

3. The problem I’m having:

it seems nesting file_server in handle block not work, no errors in log but returns nothing

4. Error messages and/or full log output:

one log for tail -f /usr/local/var/log/caddy.log after refresh localhost:9999/aws page

{"level":"info","ts":1604636604.255157,"logger":"http.log.access","msg":"handled request","request":{"method":"GET","uri":"/www/","proto":"HTTP/1.1","remote_addr":"[::1]:59993","host":"localhost:9999","headers":{"Cache-Control":["max-age=0"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"],"Sec-Fetch-User":["?1"],"Sec-Fetch-Dest":["document"],"Connection":["keep-alive"],"Upgrade-Insecure-Requests":["1"],"Accept-Encoding":["gzip, deflate, br"],"Sec-Fetch-Site":["none"],"Accept-Language":["en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7"],"Dnt":["1"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"],"Sec-Fetch-Mode":["navigate"],"Cookie":["_ga=GA1.1.1758989155.1595703656; G_AUTHUSER_H=0; sort=name; order=asc; gpadminpassport=cm9vdHw5YWVmNTI1NWFmODY3YzUxMmQ5ZmZhNzJlZmYwNzc0OQ==; __wt1vic=9ead114173abda3; __wt1vpc=dipInstanceId%3Dn0je5w0t-6cd91690b6c8dec8d21a5a0c67496fc2-3e3171f1f60d874390b7d70860bc57bb%26dssLicenseKind%3DCOMMUNITY%26bkdDistrib%3Dosx%26bkdDistribVersion%3D10.15%26isAutomation%3Dfalse%26dssVersion%3D5.0.1%26vdssUser%3D92668751; dss_access_token_00084d7e0213597e4f7da3b0d213a200=P5faqo2k7LoSV4PIeR1b0tULeN733of3ao3zntPTe3Y5YEeFgsTQjElUVsOVIQWK; dss_xsrf_token_00084d7e0213597e4f7da3b0d213a200=861ed91ebbf21c05cf51f8096c37b481dacf89ac8d29e0e1e8c3419b3fb7c627; publish2_publish_scheduled_time=; pubilsh2_publish_draft_content=false; csrf_token=++Nn4pBmnL5TVugg5AYCyW/6/3ZDNPYQzJ8sa8kB3Io=; io=MDv7Rw_487rfulyBAAAA; drawer_state=1; _session=MTYwMzE1MzAxMHxEdi1CQkFFQ180SUFBUkFCRUFBQV9nR29fNElBQWdaemRISnBibWNNRHdBTlgyRjFkR2hmYzJWemMybHZiZ1p6ZEhKcGJtY01fZ0ZWQVA0QlVXVjVTbWhpUjJOcFQybEtTVlY2U1RGT2FVbHpTVzVTTldORFNUWkphM0JZVmtOS09TNWxlVXAzWTIwNU1tRlhVbXhqYVVrMlNXNUNhR016VGpOaU0wcHJTV2wzYVdSWVRteGpiV3hyU1dwdmFVMVRTWE5KYlhob1l6TlNabUpIT1c1aFZ6UnBUMmxKZVUxRVNYZE1WRVYzVEZSRk1sWkVSVFJQYWswMFQycFJNVXhxUlhoTlZFVXlUV2x6ZDA5RWIzZE5RMGx6U1cxNGFHTXpVbVpaVjA0d1lWaGFiRWxxYjJsTmFrRjVUVU13ZUUxRE1IaFBWbEYzVFVSdk1FMXFiekZOZVRSNFQxUnJORTVxYTNKTlJHYzJUVVJCYVV4RFNtdGhXRTR3WTIxR2FtUkhiSFppYkRrd1lWY3hiRWxxYjNwTmFra3pUVVJyZWs1VVJUUk5SRUYzVEVOS2NXUkhhMmxQYVVwb1drY3hjR0pyUW14bFIwWjBZMGQ0YkV4dFRuWmlVMG81TGxoemFXUnlSeko0VURVelZqTk5VME5xZFd0dFQwWk5lSFJCZUdkTVVGbGxieTF1ZUVzMVFWOUVOM01HYzNSeWFXNW5EQXNBQ1hKbGRIVnlibDkwYndaemRISnBibWNNRHdBTkwyRmtiV2x1TDI5eVpHVnljdz09fMxG56dH6D0-Uw6AiZ9OeF5qH37KpbWlbW-gq2c-Uop0; Tilt-Token=3e69da50-5ec3-43b4-9b9a-f7a6382fdcbe"]}},"common_log":"::1 - - [06/Nov/2020:12:23:24 +0800] \"GET /www/ HTTP/1.1\" 0 0","duration":0.000011017,"size":0,"status":0,"resp_headers":{"Server":["Caddy"]}}

5. What I already tried:

6. Links to relevant resources:

Caddy v2.0.0 is quite an old version at this point. Please upgrade, latest is v2.2.1

Path matching in Caddy v2 is exact-match, /www will only match requests to exactly /www and not /www/foo. You need to append a * to the path, like /www* to match everything below it.

Also, the file server appends the current request path to the root defined, so for your /www handle, the file server would be looking in /usr/local/var/www/www. To deal with this, you can instead, you can use handle_path which strips the path prefix from the request before handling the request. (note handle_path was added in I think v2.1.0, so you’ll need to upgrade to use it anyways)

Your config would look something like this:

:9999 {
	log
	handle_path /www* {
		root * /usr/local/var/www
		file_server browse {
			index none
		}
	}

	handle_path /aws* {
		root * ~/Dropbox/AWS
		file_server browse
	}

	handle {
		# Do something with requests that didn't match the other handlers
	}
}
1 Like

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