File server browsable at a certain path?

1. Caddy version (caddy version):

v2.2.1 h1:Q62GWHMtztnvyRU+KPOpw6fNfeCD3SkwH7SfT1Tgt2c=

2. How I run Caddy:

a. System environment:

Void Linux

b. Command:

n/a

c. Service/unit/compose file:

n/a

d. My complete Caddyfile or JSON config:

What does work:

mydomain.com {
root * /home/user/xyz
file_server browse
}

What does not:

mydomain.com {
handle /xyz* {
root * /home/user/xyz
file_server browse
}
}

or

mydomain.com {
file_server /xyz* browse {
root /home/user/xyz
}
}

3. The problem I’m having:

I want to browse files at a specific path, and a specific root.

4. Error messages and/or full log output:

A white screen or a 404

5. What I already tried:

I tried to append / to the handler matcher (before the wildcard), and that did not fix things. I can’t really figure out what else to do.

6. Links to relevant resources:

Found this post, but the handle solution did not work either. (I was originally doing the one with the file_server matcher.)

How do I correctly do this, then?

The problem is that Caddy will append the request path to the root when looking for files on disk. With just handle, Caddy is basically looking for files at /home/user/xyz/xyz.

For this, you’ll want to use handle_path rather than handle, because that includes stripping the path prefix first.

mydomain.com {
	handle_path /xyz* {
		root * /home/user/xyz
		file_server browse
	}
}
1 Like

Thanks, that worked. Is it normal that it’s redirecting /xyz to /, and not ,/xyz/ so the files can be browsed? (as trailing slash works for the browser)

Yes, the redirect is necessary so that the relative URLs for the links in the browser point to the right place. The / is necessary to clarify that it’s actually a directory.

Is it normal that it’s redirecting /xyz to /, and not /xyz/

You mean that ^^ behavior is correct? I feel like it isn’t, but I’m probably wrong.

Oh… :thinking: sorry I misread that as /xyz to /xyz/

Hmm… I guess you might want to preempt that with your own redirect like this:

redir /xyz /xyz/
handle_path /xyz* {
	root * /home/user/xyz
	file_server browse
}

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