Nextcloud redirects to local IP when trying to access from domain

Artichoke

Dabbler
Joined
Jul 12, 2021
Messages
24
I have a TrueNAS SCALE instance installed with Nextcloud 23.0.2_1.3.13 installed through the Apps section in the TrueNAS SCALE UI. After I install Nextcloud, I am able to access the web UI through my local IP and it works well. However, When I try to access Nextcloud from my domain (let's call it mydomain.tld), it keeps trying to redirect me to my local IP. I have found other similar issues like this on these forums and Nextcloud's, but none of the solutions that worked for them worked for me.

Here is my setup:
  • TrueNAS version: TrueNAS-SCALE-22.02.0
  • Nextcloud version: Nextcloud 23.0.2_1.3.13
  • DNS: Domain registered and protected with Cloudflare
  • Port forwarding: Internal port 9001 -> External port 443
  • Certificate: Let's Encrypt
  • Nextcloud app config:
  • Screen Shot 2022-03-08 at 4.35.27 pm.png
  • Nextcloud config.php:
Code:
<?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,
    ),
  ),
  'overwritehost' => 'mydomain.tld',
  'overwriteprotocol' => 'https',
  'trusted_proxies' =>
  array (
    0 => '10.42.0.0/16',
    1 => '103.21.244.0/22',
    2 => '103.22.200.0/22',
    3 => '103.31.4.0/22',
    4 => '104.16.0.0/12',
    5 => '108.162.192.0/18',
    6 => '131.0.72.0/22',
    7 => '141.101.64.0/18',
    8 => '162.158.0.0/15',
    9 => '172.64.0.0/13',
    10 => '173.245.48.0/20',
    11 => '188.114.96.0/20',
    12 => '190.93.240.0/20',
    13 => '197.234.240.0/22',
    14 => '198.41.128.0/17',
    15 => '2400:cb00::/32',
    16 => '2606:4700::/32',
    17 => '2803:f800::/32',
    18 => '2405:b500::/32',
    19 => '2405:8100::/32',
    20 => '2c0f:f248::/32',
    21 => '127.0.0.1',
  ),
  'forwarded_for_headers' =>
  array (
    0 => 'HTTP_CF_CONNECTING_IP',
  ),
  'passwordsalt' => 'REDACTED',
  'secret' => 'REDACTED',
  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'mydomain.tld',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'pgsql',
  'version' => '23.0.2.1',
  'overwrite.cli.url' => 'https://mydomain.tld',
  'dbname' => 'REDACTED',
  'dbhost' => 'REDACTED',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'REDACTED',
  'dbpassword' => 'REDACTED',
  'installed' => true,
  'instanceid' => 'occnx70veem7',
);


Here is a list of things I have changed and/or tried:
  • setting the overwrite_url and overwrite_cli_url in Nextcloud's config.php to mydomain.tld
  • changing the host from my local IP to mydomain.tld (this results in the domain being right, but not the port)
  • Adding Cloudflare's reverse proxies to the trusted_proxies in Nextcloud config.php
  • Turning off Cloudlfare proxy and setting it to dns only
  • Port forwarding and testing another webserver to confirm its TrueNAS or Nextclouds fault
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
What I have found (and what "fixed" it for me) was that specifying too many options actually makes it impossible to work.

I removed all the other items related to the url (like overwrite host) and left only the client url rewrite together with the protocol overwrite for https.
 

Artichoke

Dabbler
Joined
Jul 12, 2021
Messages
24
What I have found (and what "fixed" it for me) was that specifying too many options actually makes it impossible to work.

I removed all the other items related to the url (like overwrite host) and left only the client url rewrite together with the protocol overwrite for https.
I tried removing overwite_host and overwrite_protocol and it does the same thing as before (redirects to the correct domain but not port) .
 
Joined
Jan 27, 2020
Messages
577
I have a TrueNAS SCALE instance installed with Nextcloud 23.0.2_1.3.13 installed through the Apps section in the TrueNAS SCALE UI. After I install Nextcloud, I am able to access the web UI through my local IP and it works well. However, When I try to access Nextcloud from my domain (let's call it mydomain.tld), it keeps trying to redirect me to my local IP. I have found other similar issues like this on these forums and Nextcloud's, but none of the solutions that worked for them worked for me.

Here is my setup:
  • TrueNAS version: TrueNAS-SCALE-22.02.0
  • Nextcloud version: Nextcloud 23.0.2_1.3.13
  • DNS: Domain registered and protected with Cloudflare
  • Port forwarding: Internal port 9001 -> External port 443
  • Certificate: Let's Encrypt
  • Nextcloud app config:
  • View attachment 53845
  • Nextcloud config.php:
Code:
<?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,
    ),
  ),
  'overwritehost' => 'mydomain.tld',
  'overwriteprotocol' => 'https',
  'trusted_proxies' =>
  array (
    0 => '10.42.0.0/16',
    1 => '103.21.244.0/22',
    2 => '103.22.200.0/22',
    3 => '103.31.4.0/22',
    4 => '104.16.0.0/12',
    5 => '108.162.192.0/18',
    6 => '131.0.72.0/22',
    7 => '141.101.64.0/18',
    8 => '162.158.0.0/15',
    9 => '172.64.0.0/13',
    10 => '173.245.48.0/20',
    11 => '188.114.96.0/20',
    12 => '190.93.240.0/20',
    13 => '197.234.240.0/22',
    14 => '198.41.128.0/17',
    15 => '2400:cb00::/32',
    16 => '2606:4700::/32',
    17 => '2803:f800::/32',
    18 => '2405:b500::/32',
    19 => '2405:8100::/32',
    20 => '2c0f:f248::/32',
    21 => '127.0.0.1',
  ),
  'forwarded_for_headers' =>
  array (
    0 => 'HTTP_CF_CONNECTING_IP',
  ),
  'passwordsalt' => 'REDACTED',
  'secret' => 'REDACTED',
  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'mydomain.tld',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'pgsql',
  'version' => '23.0.2.1',
  'overwrite.cli.url' => 'https://mydomain.tld',
  'dbname' => 'REDACTED',
  'dbhost' => 'REDACTED',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'REDACTED',
  'dbpassword' => 'REDACTED',
  'installed' => true,
  'instanceid' => 'occnx70veem7',
);


Here is a list of things I have changed and/or tried:
  • setting the overwrite_url and overwrite_cli_url in Nextcloud's config.php to mydomain.tld
  • changing the host from my local IP to mydomain.tld (this results in the domain being right, but not the port)
  • Adding Cloudflare's reverse proxies to the trusted_proxies in Nextcloud config.php
  • Turning off Cloudlfare proxy and setting it to dns only
  • Port forwarding and testing another webserver to confirm its TrueNAS or Nextclouds fault
Seeing your huge array of trusted proxies, I'd try to comment out one after another and see if the redirect works at any point. Maybe the use of a reverse proxy that performs the SSL termination and forwards locally with plain http would be worth a try.
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
I would also consider taking out the 'forwarded_for_headers' and see if that changes anything.
 

rankok

Cadet
Joined
May 3, 2022
Messages
9
Hello,
I have the same issue, did someone find any solution for this?

Best regards!
 

XStylus

Dabbler
Joined
Nov 22, 2017
Messages
20
Likewise, I'm running into the same issue and have yet to find the solution.
 

mgoulet65

Explorer
Joined
Jun 15, 2021
Messages
95
Truecharts nextcloud with traefik just worked for me.
 

kazaw85

Cadet
Joined
Apr 23, 2023
Messages
1
for anyone still trying to make it work with official app. I solve it by adding
Nextcloud environment variables (one of 2 showed fixed it)
instead of editing config.php. I ataching picture with my working conf.
In case if picture wont work:
Add Nextcloud enviroment in app setup or edit existing app.
Name: OVERWRITEHOST and/or OVERWRITECLIURL
Value: cloud.yourdomain.com
nc.jpg
 

requird

Cadet
Joined
Feb 23, 2023
Messages
7
In case if picture wont work:
Add Nextcloud enviroment in app setup or edit existing app.
Name: OVERWRITEHOST and/or OVERWRITECLIURL
Value: cloud.yourdomain.com

omg, thank you.

I have a couple questions for you. Is there a reference to the nextcloud environment name/values? (Or where did you find these?) And second, just setting OVERWRITEHOST worked for me. Do you know what OVERWRITECLIURL does?
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
Is there a reference to the nextcloud environment name/values?


Do you know what OVERWRITECLIURL does?
From the manual:
Use this configuration parameter to specify the base URL for any URLs which are generated within Nextcloud using any kind of command line tools (cron or occ). The value should contain the full base URL: https://www.example.com/nextcloud

Defaults to '' (empty string)
 

requird

Cadet
Joined
Feb 23, 2023
Messages
7
Thanks @sretalla ! This helps. There are a lot of good things in there. I manually made some changes to the config.php file, but I can make all the changes here now.

With the above OVERWRITEHOST, I have https://nextcloud.example.com:9001 running internally with a valid cert signed by an internal CA.

I'm planning on running HAProxy plugin on my OPNsense box with client certificates as authorization. Then proxy to the Nextcloud instance running on TrueNAS. So whether I'm inside my home network or outside in public, the nextcloud client will always be able to connect to my nextcloud instance with https://nextcloud.example.com. I have to change the port next.
 

tpxcer

Cadet
Joined
Jun 7, 2020
Messages
8
for anyone still trying to make it work with official app. I solve it by adding
Nextcloud environment variables (one of 2 showed fixed it)
instead of editing config.php. I ataching picture with my working conf.
In case if picture wont work:
Add Nextcloud enviroment in app setup or edit existing app.
Name: OVERWRITEHOST and/or OVERWRITECLIURL
Value: cloud.yourdomain.com View attachment 66052
That work's ,Thank you!
 

brmvnml

Cadet
Joined
Dec 14, 2023
Messages
1
for anyone still trying to make it work with official app. I solve it by adding
Nextcloud environment variables (one of 2 showed fixed it)
instead of editing config.php. I ataching picture with my working conf.
In case if picture wont work:
Add Nextcloud enviroment in app setup or edit existing app.
Name: OVERWRITEHOST and/or OVERWRITECLIURL
Value: cloud.yourdomain.com View attachment 66052
Still a hero...:cool: ... Yes I made an account just to thank you!
 

MrTacoMan

Cadet
Joined
Dec 22, 2023
Messages
1
for anyone still trying to make it work with official app. I solve it by adding
Nextcloud environment variables (one of 2 showed fixed it)
instead of editing config.php. I ataching picture with my working conf.
In case if picture wont work:
Add Nextcloud enviroment in app setup or edit existing app.
Name: OVERWRITEHOST and/or OVERWRITECLIURL
Value: cloud.yourdomain.com View attachment 66052
THANK YOU SO MUCH!!!! This worked for me! I was trying every Youtube video and ChatGPT nothing was helping this finally let me do it. Note to others make sure you put back the original config file. I forgot to do that before trying this method after ai put back the original it worked using the GUI method above.
 
Joined
Dec 29, 2023
Messages
2
for anyone still trying to make it work with official app. I solve it by adding
Nextcloud environment variables (one of 2 showed fixed it)
instead of editing config.php. I ataching picture with my working conf.
In case if picture wont work:
Add Nextcloud enviroment in app setup or edit existing app.
Name: OVERWRITEHOST and/or OVERWRITECLIURL
Value: cloud.yourdomain.com View attachment 66052
Man, I wish I could send you flowers!

Been racking my brain with this for two days. Changing these parameters in config.php wasn't working for me, but your solution got it working instantly. Thank you so much, I can rest easy again.
 
Top