Incorrect redirect when trying to login from desktop and mobile clients

Nextcloud version :’
Operating system and version : Ubuntu server 18.04
Apache or nginx version : Apache/2.4.29 (Ubuntu)
PHP version: 7.2

The issue you are facing: I have a Digital Ocean droplet running the nextcloud server. I have Nginx (port 80 and 443) and Apache at port 8000. To access my nextcloud instance, I redirect the traffic to port 8000. Everything works fine if I only use the web version. If I try to log in from the ubuntu desktop client, the redirection url to grant access is not valid, because my domain is replaced by “http://localhost:8000”. I suffer the same problem in the android mobile app.

Is this the first time you’ve seen this error? Yes.

Steps to replicate it: Quite hard to replicate due to my setup.

The output of your Nextcloud log in Admin > Logging:

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

$CONFIG = array (
  'instanceid' => 'xxx',
  'passwordsalt' => 'xxxx',
  'secret' => 'xxx',
  'trusted_domains' =>
  array (
    0 => 'localhost:8000',
  'datadirectory' => '/mnt/nextcloud-nfs',
  'dbtype' => 'mysql',
  'version' => '',
  'overwrite.cli.url' => 'mydomain',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'xxx',
  'dbpassword' => 'xxx',
  'installed' => true,

Solved in [SOLVED] Nextcloud 15 redirect to local IP