Caddy equivalent to apache2's 'mod_userdir' module

Hi,

I want to use caddy to allow users of my server to have their own folder, similar to how it was done in apache2

Is there a way to do this?

Thanks in advance!

Looks like it simply sets the web root based on the /~user syntax.

The simplest way to achieve the same effect is to configure a site in Caddy for each user.

example.com {
  root /var/www/html
  browse
}

example.com/~foo {
  root /home/foo
  browse
}

example.com/~bar {
  root /path/to/bar
  browse
}

Alternately, structure your file tree such that your user’s folders are accessible directly under the web root.

/var/www/html
├── /~foo
│   ├── /stuff
│   └── /things
└── /~bar
    ├── /folder
    └── /projects

Don’t forget to secure these somehow.

Looks like that will work, thanks!

You could also probably do some sort of rewrite with a regex to capture the username and translate the URL internally.

Looks like you’re trying to “rewrite” the request to an absolute path on your file system, which isn’t going to work.

Rewrite changes the URI of the request to the rewritten string. For example, with this rewrite:

root /var/www/html
rewrite {
	regexp /~(\w+)
	to /home/{1}/www
}

A request for example.com/~bob will be rewritten to example.com/home/bob/www.

You will then need to ensure that /var/www/html/home/bob/www exists, because the URI is relative to the web root.

/home is common to all three examples, so you could do this:

root /home
rewrite {
  r /~(\w+)
  to /{1}/www
}

Which would work along the lines you’re thinking, but don’t forget about potential security concerns that come with putting your home directories in a public web root. It’s very easy to miss ways in which someone could (un)knowingly abuse a regex rewrite to get access to files they maybe shouldn’t.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.