Limit root to specified folders

1. Caddy version (v2.4.6):

2. How I run Caddy: caddy start

a. System environment: Windows 11

b. Command:

caddy start

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

localhost {
	root * a:/images/  a:/docs
	file_server browse
}

3. The problem I’m having:

Sorry if this question is already documented somewhere, I couldn’t find anything from my search.

I’m completely new to caddy and I’m trying to specify only 2 subdirectories in my root to allow access to.

Current directory is structered:

  • A1
  • Custom Templates
  • Docs
  • Images
  • Report 1
  • Report 2
  • Report 25
  • Text Templates

I only want access allowed to the Images and Docs folders. I saw there is a “not path” directive, but I didn’t want to specify all sub-directories to not include, just the ones I want.

Sorry if this is a stupid question. Thanks.

4. Error messages and/or full log output: “This site can’t be reached”

5. What I already tried:

localhost {
root * a:/images/
file_server browse
}

localhost {
root * a:/docs
file_server browse
}

6. Links to relevant resources:

There can only be one root.

What exactly are you trying to do?

Please give example HTTP requests and what you expect them to do.

Sorry, I realized I worded that stupid. I’m just trying to limit the file browse to only show two folders. Instead of listing what shouldn’t be present, I just wanted to list the two that should be present.

localhost {
	root * a:/
	file_server browse {
     		hide "A1" "Custom Templates" "Report 1" (...etc)
	}
}

Instead of listing everything in the hide I don’t need, is there a way to list just the two I need?
Thanks for your help.

Okay, then you can use a path matcher like this:

localhost {
	handle_path /images* {
		root * a:/images
		file_server browse
	}

	handle_path /docs* {
		root * a:/docs
		file_server browse
	}

	handle {
		# Fallback if no other handle matched
	}
}
1 Like

Thanks for the quick response!
That works with limiting the 2 directories, but I have to add the folder to the path:

localhost/images
	- subdirectories
	
localhost/docs
	- subdirectories

Is it possible to have localhost list the 2 directories as well?

localhost
	- images
		- subdirectories
	- docs
		- subdirectories

Nope, currently the only way to do that is with hide. We could probably add the reciprocal, show. You can open a feature request on github for this if you like.

Ok. Thanks again for your help.

Feature requested here:

PR to implement the new feature here:

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