Nextcloud not working after moving it behind a reverse proxy

I have a nextcloud installation. Recently I moved it to a new server and put it behind a reverse proxy but now it is not working. It responds with an internal server error every time I try to login.

I will attach below my config.php, nextcloud logs, and both configurations for apache2.

Config.php
$CONFIG = array (
  'instanceid' => 'Secret',
  'passwordsalt' => 'Secret',
  'secret' => 'Secret',
  'trusted_domains' =>
  array (
    0 => 'nextcloud.domain.tld',
  ),
  'overwritehost' => 'nextcloud.domain.tld',
  'overwriteprotocol' => 'https',
  'overwritewebroot'  => '',
  'overwrite.cli.url' => 'https://nextcloud.domain.tld/',
  'trusted_proxies' =>
  array (
    0 => '192.168.1.6',
  ),
  'datadirectory' => '/home/dal/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '27.0.0.8',
  'dbname' => 'nextcloud',
  'dbhost' => '192.168.1.6',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextcloud',
  'dbpassword' => 'Secret',
  'installed' => true,
  'defaultapp' => 'dashboard',
  'mail_smtpmode' => 'smtp',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => 'Secret',
  'mail_domain' => 'domain.tld',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtphost' => 'mail.domain.tld',
  'mail_smtpport' => '465',
  'maintenance' => false,
  'theme' => '',
  'loglevel' => 2,
  'app_install_overwrite' =>
  array (
    0 => 'files_videoplayer',
    1 => 'video_converter',
    2 => 'extract',
    3 => 'pdfdraw',
    4 => 'quickaccesssorting',
    5 => 'checksum',
    6 => 'cfg_share_links',
    7 => 'ransomware_protection',
    8 => 'registration',
    9 => 'unsplash',
    10 => 'audioplayer',
  ),
  'simpleSignUpLink.shown' => false,
  'updater.release.channel' => 'stable',
  'mail_smtpauth' => 1,
  'mail_smtpsecure' => 'ssl',
  'mail_smtpname' => 'mail@domain.tld',
  'mail_smtppassword' => 'Secret',
  'default_phone_region' => 'US',
  'htaccess.RewriteBase' => '/',
  'redis' =>
  array (
    'host' => '127.0.0.1',
    'port' => 6379,
  ),
  'updater.secret' => 'Secret',
);
Nextcloud log

This a single entry from /data/nextcloud.log
The rest of the logs are just this but repeating

{
	"reqId": "ItCILv4yllVGKRGfIRtE",
	"level": 2,
	"time": "2022-05-06T06:03:24+00:00",
	"remoteAddr": "IP",
	"user": "--",
	"app": "no app in context",
	"method": "POST",
	"url": "/index.php/apps/index.php",
	"message": "Could not detect any host in http:///data/htaccesstest.txt",
	"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36",
	"version": ""
}
Apache2 nextcloud config
<VirtualHost *:80>

    ServerName 192.168.1.25
    DocumentRoot /var/www/nextcloud

    <Directory /var/www/nextcloud>
        AllowOverride All
        Require all granted
    </Directory>


    CustomLog /var/log/apache2/nextcloud.access.log common
    ErrorLog  /var/log/apache2/nextcloud.error.log

</VirtualHost>
Apache2 reverse proxy config
<IfModule mod_ssl.c>
<VirtualHost *:443>
  ServerName nextcloud.domain.tld
  SSLCertificateFile /etc/letsencrypt/live/nextcloud.domain.tld/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/nextcloud.domain.tld/privkey.pem
  #Include /etc/letsencrypt/options-ssl-apache.conf

  ProxyPreserveHost On
  ProxyRequests On

  # allow for upgrading to websockets
  RewriteEngine On
  RewriteRule ^/\.well-known/carddav https://%{SERVER_NAME}/remote.php/dav/ [R=301,L]
  RewriteRule ^/\.well-known/caldav https://%{SERVER_NAME}/remote.php/dav/ [R=301,L]


  RequestHeader setifempty X-Forwarded-Proto https


  # Proxy to your local mangonet instance
  ProxyPass / http://192.168.1.25:80/
  ProxyPassReverse / http://192.168.1.25:80/

</VirtualHost>
</IfModule>

The server running nextcloud is using Ubuntu 22.04.2 LTS with Apache/2.4.41
The server running the reverse proxy is using Ubuntu 20.04.06 LTS with Apache/2.4.52

Hi.

Two things:

Your trusted domains does not include the IP of which your “local Apache” is accepting as “domain name”.

<VirtualHost *:80>

    ServerName 192.168.1.25
    DocumentRoot /var/www/nextcloud

vs

'trusted_domains' =>
  array (
    0 => 'nextcloud.domain.tld',
  ),

And then you are forcing the nextcloud to accept only https

'overwriteprotocol' => 'https',

However your apache webserver is only listening to http as your reverse proxy is the one that terminates the HTTPS.

1 Like

I added 192.168.1.25 to the trusted domains and removed the overwriteprotocol entry but nextcloud is giving the same error

I also tried running a fresh install of nextcloud but even then it gave the same error. Maybe the problem is with the proxy?

For some reason nextcloud wasn’t adding new entries to nextcloud.log

Anyways, I did occ log:watch and found that something was wrong with the secret in config.php

The error was:
ValueError: hash_hkdf(): Argument #2 ($key) cannot be empty at lib/private/Security/Crypto.php

I fixed this by disabling the default encryption module app.