Proxy Pass Variable Causes Redirect Loop

Support intro
longer
example
here

Docker 18.06.1-ce
Nextcloud version 16.0.5
Ubuntu 18.04.3 LTS
Apache version 2.4.38
PHP version 7.3.10

Attempting to proxy pass from nginx to the Nextcloud container with variables causes a redirect loop.

Is this the first time you’ve seen this error? Y

Steps to replicate it:

  1. Use this config segment in nginx:
resolver 127.0.0.11 valid=30s; #Docker DNS

location /nextcloud/ {
	set $var "http://nextcloud:80/";
	proxy_pass $var;
	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
  1. Navigate to URL domain.com/nextcloud to get redirect loop

The output of your Nextcloud log in Admin > Logging:
Throws no errors.

The output of config.php

<?php
$CONFIG = array (
  'htaccess.RewriteBase' => '/',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'apps_paths' =>
  array (
    0 =>
    array (
      'path' => '/var/www/html/apps',
      'url' => '/apps',
      'writable' => false,
    ),
    1 =>
    array (
      'path' => '/var/www/html/custom_apps',
      'url' => '/custom_apps',
      'writable' => true,
    ),
  ),
  'instanceid' => 'XXX',
  'passwordsalt' => 'XXX',
  'secret' => 'XXX',
  'trusted_domains' =>
  array (
    0 => 'domain.com',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'pgsql',
  'version' => '16.0.5.1',
  'dbname' => 'nextcloud',
  'dbhost' => 'postgres:5432',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'nextcloud',
  'dbpassword' => 'XXX',
  'installed' => true,
  'maintenance' => false,
  'theme' => '',
  'loglevel' => 2,
  'mail_from_address' => 'nextcloud',
  'mail_smtpmode' => 'smtp',
  'mail_sendmailmode' => 'smtp',
  'mail_smtpport' => '25',
  'mail_smtphost' => 'mail',
  'mail_domain' => 'domain.com',
  'overwritewebroot' => '/nextcloud',
  'overwritehost' => 'domain.com',
  'overwriteprotocol' => 'https',
  'overwrite.cli.url' => 'https://domain.com/nextcloud',
);

The output of your Apache/nginx/system log in /var/log/____:
Unable to get apache’s logs from Nextcloud container.

The following config works just fine.

resolver 127.0.0.11 valid=30s; #Docker DNS

location /nextcloud/ {
	proxy_pass http://nextcloud:80/;
	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Using variables in proxy pass works for other services i have, but only Nextcloud causes a redirect loop.