Infinite redirect loop in Caddy v2 server

Nextcloud version: 23.0.0
Operating system and version: Arch Linux
Apache or nginx version: Apache 2.4.25
Caddy version: 2.4.6
PHP version: 8.1.2


Hi, I know that Caddy isn’t officially supported but I am hoping the community can help me figure out what’s wrong with my configuration.

I managed to piece together a mostly working web server configuration and I was able to go through the web installer.

The issue started happening after that was done, the server falls into an infinite redirect loop to /apps/dashboard/ or /login:

$ curl -v 'http://home.server/cloud/login'
*   Trying 192.168.1.168:80...
* Connected to home.server (192.168.1.168) port 80 (#0)
> GET /cloud/login HTTP/1.1
> Host: home.server
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< Cache-Control: no-store, no-cache, must-revalidate
< Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-...'; style-src 'self' 'unsafe-inline'; frame-src *; img-src * data: blob:; font-src 'self' data:; media-src *; connect-src *; object-src 'none'; base-uri 'self';
< Content-Type: text/html; charset=UTF-8
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Location: http://home.server/cloud/login
< Pragma: no-cache
< Referrer-Policy: no-referrer
< Server: Caddy
< Set-Cookie: ....
< Status: 302 Found
< X-Content-Type-Options: nosniff
< X-Download-Options: noopen
< X-Frame-Options: SAMEORIGIN
< X-Permitted-Cross-Domain-Policies: none
< X-Powered-By: PHP/8.1.2
< X-Robots-Tag: none
< X-Xss-Protection: 1; mode=block
< Date: Tue, 25 Jan 2022 00:30:05 GMT
< Content-Length: 0
< 

Here is my configuration:

config.php:

Code
<?php
$CONFIG = array (
  'datadirectory' => '/var/lib/nextcloud/data',
  'logfile' => '/var/log/nextcloud/nextcloud.log',
  'apps_paths' => 
  array (
    0 => 
    array (
      'path' => '/usr/share/webapps/nextcloud/apps',
      'url' => '/apps',
      'writable' => false,
    ),
    1 => 
    array (
      'path' => '/var/lib/nextcloud/apps',
      'url' => '/wapps',
      'writable' => true,
    ),
  ),
  'instanceid' => '',
  'overwritewebroot' => '/cloud',
  'passwordsalt' => '',
  'secret' => '',
  'trusted_domains' => 
  array (
    0 => 'home.server',
  ),
  'dbtype' => 'mysql',
  'version' => '23.0.0.10',
  'overwrite.cli.url' => 'http://home.server/cloud',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextcloud',
  'dbpassword' => '',
  'installed' => true,
);

Caddyfile (web server config:

Code
@cloud {
	path /cloud /cloud/*
}

handle @cloud {
	uri strip_prefix /cloud
	redir /cloud /cloud/ # Dirty fix for a bug

	# Source: https://help.nextcloud.com/t/why-is-nextcloud-causing-a-redirect-loop-caddy-webserver/10498/4
	#rewrite /cloud/* /cloud/?{query}

	root * /usr/share/webapps/nextcloud

	php_fastcgi unix//run/nextcloud/nextcloud.sock {
		# Source: https://help.nextcloud.com/t/anyone-is-kind-enough-to-share-his-caddy-2-config
		env front_controller_active true
	}

	file_server

	# Source: https://caddy.community/t/setting-up-nextcloud-behind-caddy/14787
	redir /cloud/.well-known/carddav /remote.php/dav 301
	redir /cloud/.well-known/caldav /remote.php/dav 301

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

	respond @cloud_forbidden 404
}

As you may have noticed, I am using a sub-path as the web root for NextCloud as I have other services running on the same server.

I can’t figure out what’s causing the infinite loop :confused:

Can anyone help me figure it out?