Nextcloud in a subdirectory behind Caddy

  1. Caddy version (caddy version):

  2. How I run Caddy:
    As a front end for Nextcloud, currently just trying to make my internal ip work with a subdirectory /nextcloud

a. System environment:

FreeNAS 11.3 U3.

b. Command:
service caddy start

d. My complete Caddyfile or JSON config: {
root * /usr/local/www/nextcloud

    log {
            output file /var/log/caddyTest.log
            format single_field common_log

    php_fastcgi {
            env front_controller_active true

   # rewrite /index.php/* /index.php?{query}

    redir /.well-known/carddav /remote.php/dav 301
    redir /.well-known/caldav /remote.php/dav 301

    # .htaccess / data / config / ... shouldn't be accessible from outside
    @forbidden {
            path /.htaccess
            path /data/*
            path /config/*
            path /db_structure
            path /.xml
            path /README
            path /3rdparty/*
            path /lib/*
            path /templates/*
            path /occ
            path /console.php

    respond @forbidden 404


  1. The problem I’m having:
    I cannot get Nextcloud to work using the url base /nextcloud. It works perfect if I change my caddy file to just ip. With ip/nextcloud I get a white page on a browser.

  2. Error messages and/or full log output: - - [26/Oct/2020:13:06:27 -0400] “GET /nextcloud/status.php HTTP/1.1” 302 0 - - [26/Oct/2020:13:06:27 -0400] “GET /apps/files/ HTTP/1.1” 200 39662 - - [26/Oct/2020:13:06:29 -0400] “GET /nextcloud/status.php HTTP/1.1” 302 0 - - [26/Oct/2020:13:06:29 -0400] “GET /apps/files/ HTTP/1.1” 200 38881 - - [26/Oct/2020:13:06:34 -0400] “GET /nextcloud/index.php HTTP/1.1” 0 0 - - [26/Oct/2020:13:06:35 -0400] “GET /favicon.ico HTTP/1.1” 0 0 - - [26/Oct/2020:13:06:59 -0400] “GET /nextcloud/status.php HTTP/1.1” 302 0 - - [26/Oct/2020:13:06:59 -0400] “GET /apps/files/ HTTP/1.1” 200 39668 - - [26/Oct/2020:13:07:01 -0400] “GET /nextcloud/status.php HTTP/1.1” 302 0 - - [26/Oct/2020:13:07:01 -0400] “GET /apps/files/ HTTP/1.1” 200 38879

  3. What I already tried:
    In nextcloud config I have tried setting overwritebaseurl to /nextcloud and removed the /nextcloud in caddyfile, this gives me too many reddirect error.

I have tried using ip/nextcloud in caddy file with overwriteurl in nextcloud config.php base as /nextcloud, same thing.

I have tried using ip/nextcloud in caddy file without overwriteurlbase in nextcloud config, this sorta works, using the ip/nextcloud gets the url query to be ip/nextcloud/login but fails with a 404 error.

I have also tried {
root /nextcloud* /usr/local/www/nextcloud
but this redeirects back to ip/login with a 404 error. Adding urlbase in nextcloud config gives me the too many redirects error.

Please help me figure this out, I need it to be in a subdirectoy for use with my reverse proxy.

It should look something like this: {
	handle_path /nextcloud* {
		# the rest of the nextcloud config goes in here

Note that when using path matchers, the * is important, because path matching is exact-match in Caddy v2, so your /nextcloud path matcher only matches exactly /nextcloud and nothing else.

The handle_path directive strips the given prefix from the path before continuing, which is important for root to work correctly, because things that use root, like the file_server, take root and append the path to it when looking for files on disk, so if the path wasn’t stripped, it would be looking at /usr/local/www/nextcloud/nextcloud.

Thanks for the reply. I tried that change and just like you said it removes prefix so nextcloud url is still ip and not ip/nextcloud. Is there any directive to make caddy rewrite the url as /nextcloud ? I did it in a janky way using apache.
I used /usr/local/www/ as root. So when you browse ip/nextcloud php takes over but if you used just ip:80, you would see a simple html file with the nextcloud subdir which you could click to go the ip/nextcloud. Can I replicate this on caddy ?
On caddy if I use /usr/local/www/ as root php never takes over so I get a unformated xml file when going into /nextcloud.

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