SOLVED Nextcloud (official) + Nginx PM (official) - reverse proxy issues

slacks425

Dabbler
Joined
Aug 6, 2023
Messages
13
I try to install a fully working Nextcloud on my TrueNAS Scale machine which run already several apps, including Nginx Proxy Manager which is used for many apps on the same machine and external ones without any issues. However with Nextcloud I always have problem with the reverse proxy config.

Nextcloud official package is installed with a trusted domain like cloud.domain.tld in addition to local IP. It's on port 9001 locally (default). I can access the it unencrypted on NAS_IP:9001. Then I configured Nginx to point to http NAS_IP 9001 with a *.domain.tld Let's Encrypt Certificate (as for any others app). Now I can access my Nextcloud via https://cloud.domain.tld and login without apparent issues, I can upload files, share file with external link, everything in WebUI look good except when I log out I get a page in http than is not reachable.
In admin panel I have a warning that my reverse proxy is not properly configured and link to this page to solve it: https://docs.nextcloud.com/server/l...ation_server/reverse_proxy_configuration.html

When I try to connect DAVx5 on Android, it says it cannot find CardDAV and CalDAV.
I added this to the custom config tab in Nginx:
Code:
location /.well-known/carddav {
    return 301 $scheme://$host/remote.php/dav;
}

location /.well-known/caldav {
    return 301 $scheme://$host/remote.php/dav;
}


I still have the error in DAVx5. I tried to change in the above $host with $host:$port. Still not working.
I tried to enter the same but in the custom location tab, thus I don't think I'm doing it properly as it ends up with Nextcloud being completely unreachable, when I remove it's working again in WebUI but still all other errors.

Then I tried to login to the Android app, but it fails, I end up logged on the WebUI in a webview within the Nextcloud app but the apps is not connected to the account properly and I cannot sync files or anything.
I didn't try the desktop client yet but I except similar issues as well.

I found someone config online with these more 2 lines before mine in the advanced tab (also on Nextcloud doc):
Code:
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

No sure what it does but it's not solving the issues and not make it worst either.

I modified the Nextcloud config.php file and added my TrueNAS IP as trusted_proxies, not sure if it's necessary if it's on the same machine but it was mention in Nextcloud help.
It tried to add forwarded_for_headers too, both with x_forwarded_for and without x, as I don't find the forward header in the console (F12) when I connect to Nextcloud though the Nginx reverse proxy address.

I modified the overwrite.cli.url that was pointing to localhost to point to https://cloud.domain.tld (strangely it was working before this change already) but it's not helping either.

I tried to add overwritehost and overwriteprotocol to the config.php but then Nextcloud give an internal server error. When I comment, it's working again. Maybe that's the missing part? I tried IP_TrueNAS:Nginx_port_http, IP_TrueNAS:Nginx_port_https, IP_TrueNAS:Nginx_webUI_port (you never know), IP_TrueNAS:Nextcloud_port. I don't know what else I can try. Even if I set only the overwriteprototcol to https it's then giving the internal server error...

What is super strange is that I still have an old Nextcloud install on an older machine (SynologyNAS), for which I setup the redirect from Nginx on this current TrueNAS, pointing to the old machine IP, port 443, and it's all working fine without any special advance configuration nor otuching the config.php file at all.

Any idea what is missing or wrong in my Nginx and Nextcloud config? I search many online posts and topics, both here, on Nextcloud communities and documentations, nothing helped so far.

EDIT:
I solve already one issue, the redirect for CalDAV and CardDAV was correct, but DAVx5 give me an error because I have 2FA enabled and I need to login with a one time use app password.
Remain the issue with the HTTP forbidden error that prevent to login on the Android Nextcloud app. I guess I'm super close to the solution, something in the config.php must be missing or wrong.

EDIT2:
OK everything is solved.
The Nginx extra parameters for CalDAV and CardDAV was correct but you must use an app password to login with DAVx5 or other app if you have 2FA enabled, otherwise it says that it cannot reach CalDAV and CardDAV services, which is a bit misleading, even if it also suggest that the password might be wrong, which it was not suggesting if my Nginx redirect was not correctly done, that was made me guess the problem.

For the http error, I was missing some parameters in the config.php of Nextcloud, you can check my anonymized config code below, I think the most important I had to add was:
  • Check that both the local TrueNAS IP+Nextcloud port as well as the Nextcloud domain name was in the trused_domains list.
  • 'overwrite.cli.url' => 'http://TRUENAS_LOCAL_IP:9001', #and not to the domain name, one of my mistake
  • add: 'overwriteprotocol' => 'https',
  • add: 'trusted_proxies' => array (
    0 => 'TRUENAS_LOCAL_IP',
    ),
  • add forward for headers (not sure if necessary, try without first):
  • 'forwarded_for_headers' => array (
    0 => 'HTTP_X_FORWARDED',
    1 => 'HTTP_FORWARDED_FOR',
    ),
Check below if something else is missing or wrong from your 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,
    ),
  ),
  'passwordsalt' => 'xxx',
  'secret' => 'xxx',
  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'TRUENAS_LOCAL_IP:9001',
    2 => 'cloud.domain.tld',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'pgsql',
  'version' => '27.0.2.1',
  'overwrite.cli.url' => 'http://TRUENAS_LOCAL_IP:9001',
  'overwriteprotocol' => 'https',
  'dbname' => 'nextcloud',
  'dbhost' => 'nextcloud-postgres:5432',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'oc_youradminuser,
  'dbpassword' => 'xxx',
  'installed' => true,
  'instanceid' => 'xxx',
  'memories.exiftool' => '/var/www/html/custom_apps/memories/exiftool-bin/exift>
  'memories.vod.path' => '/var/www/html/custom_apps/memories/exiftool-bin/go-vo>
  'memories.vod.ffmpeg' => '/usr/bin/ffmpeg',
  'memories.vod.ffprobe' => '/usr/bin/ffprobe',
  'twofactor_enforced' => 'true',
  'twofactor_enforced_groups' =>
  array (
    0 => 'admin',
  ),
  'twofactor_enforced_excluded_groups' =>
  array (
  ),
  'enabledPreviewProviders' =>
  array (
    0 => 'OC\\Preview\\Image',
    1 => 'OC\\Preview\\HEIC',
    2 => 'OC\\Preview\\TIFF',
    3 => 'OC\\Preview\\Movie',
  ),
  'memories.gis_type' => 2,
  'trusted_proxies' =>
  array (
    0 => 'TRUENAS_LOCAL_IP',
  ),
  'forwarded_for_headers' =>
  array (
    0 => 'HTTP_X_FORWARDED',
    1 => 'HTTP_FORWARDED_FOR',
  ),
  'default_phone_region' => 'XX',
  'filelocking.enabled' => true,
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'redis' =>
  array (
    'host' => 'TRUENAS_IP_OR_REDIS_SERVER_IP_IF_NOT_ON_TRUENAS',
    'port' => REDIS_PORT_HERE,
    'timeout' => 0.0,
    'password' => 'REDIS_PASSWORD_HERE',
  ),

  'maintenance' => false,
);


After that my Nextcloud with Nginx was fully working in every situation without error. I also added Redis on my TrueNAS with some line in the config to remove the memcache warning and the default_phone_region (change XX by your country as in the link from help) and then I was all green from Nextcloud admin settings.
 
Last edited:

sos_nz

Explorer
Joined
Mar 17, 2023
Messages
58
I run nextcloud in an arch linux VM, so my set up is quite different from running the app (either official or TrueCharts). In brief:

  1. Set up an Arch VM
  2. Install nextcloud: https://www.atlantic.net/dedicated-server-hosting/how-to-install-nextcloud-on-arch-linux/
  3. In arch, install redis php-redis php-igbinary php-imagick php-apcu and the php-legacy versions (you may or may not need / want to use php-legacy. I used, to, but aren't as of the update to 27.1.0. Enable the php modules in /etc/php/conf.d/ AND /etc/php.ini
  4. Set permissions: chown -R http:http /srv/http/nextcloud/ and chmod -R 775 /srv/http/nextcloud/
  5. Edit .htaccess (in /srv/http/nextcloud), under <IfModule mod_rewrite.c> (where "mynextcloud.com" is your desired web address):
    Code:
     RewriteRule ^\.well-known/carddav https://mynextcloud.com/remote.php/dav/ [R=301,L] RewriteRule ^\.well-known/caldav https://mynextcloud.com/remote.php/dav/ [R=301,L] 
  6. Enable the appropriate php extensions in /etc/php/php.ini (bcmath, curl, exif, gd, gmp, intl, mysqli, zend_extension=opcache, pdo_mysql, pdo_sqlite, sysvsem, zip, igbinary, redis, apcu) and edit other php paramters e.g. memory_limit = 1G
  7. Set up your cronjob: crontab -u http -e: */5 * * * * /usr/bin/php --define apc.enable_cli=1 -c /etc/php/php.ini -f /srv/http/nextcloud/cron.php
  8. Good luck!
 

slacks425

Dabbler
Joined
Aug 6, 2023
Messages
13
I run nextcloud in an arch linux VM, so my set up is quite different from running the app (either official or TrueCharts). In brief:

  1. Set up an Arch VM
  2. Install nextcloud: https://www.atlantic.net/dedicated-server-hosting/how-to-install-nextcloud-on-arch-linux/
  3. In arch, install redis php-redis php-igbinary php-imagick php-apcu and the php-legacy versions (you may or may not need / want to use php-legacy. I used, to, but aren't as of the update to 27.1.0. Enable the php modules in /etc/php/conf.d/ AND /etc/php.ini
  4. Set permissions: chown -R http:http /srv/http/nextcloud/ and chmod -R 775 /srv/http/nextcloud/
  5. Edit .htaccess (in /srv/http/nextcloud), under <IfModule mod_rewrite.c> (where "mynextcloud.com" is your desired web address):
    Code:
     RewriteRule ^\.well-known/carddav https://mynextcloud.com/remote.php/dav/ [R=301,L] RewriteRule ^\.well-known/caldav https://mynextcloud.com/remote.php/dav/ [R=301,L] 
  6. Enable the appropriate php extensions in /etc/php/php.ini (bcmath, curl, exif, gd, gmp, intl, mysqli, zend_extension=opcache, pdo_mysql, pdo_sqlite, sysvsem, zip, igbinary, redis, apcu) and edit other php paramters e.g. memory_limit = 1G
  7. Set up your cronjob: crontab -u http -e: */5 * * * * /usr/bin/php --define apc.enable_cli=1 -c /etc/php/php.ini -f /srv/http/nextcloud/cron.php
  8. Good luck!
Thanks for the help. I was running Nextcloud on a Synology NAS for several years and it was basically manually installed, so you proceedure looks more or less familiar and I had to repeat it after each update. I will try to avoid that in the future. I solve my issue with some additional parameter in the config file, I will edit my original post to help future user with same problem.
 

sos_nz

Explorer
Joined
Mar 17, 2023
Messages
58
I will try to avoid that in the future.
Yeah - that's why I've gone the VM route...more control (can edit config.php and .htaccess etc), and can backup the whole VM volume prior to update attempts etc.
 

cdog89

Explorer
Joined
Jan 19, 2024
Messages
75
I try to install a fully working Nextcloud on my TrueNAS Scale machine which run already several apps, including Nginx Proxy Manager which is used for many apps on the same machine and external ones without any issues. However with Nextcloud I always have problem with the reverse proxy config.

Nextcloud official package is installed with a trusted domain like cloud.domain.tld in addition to local IP. It's on port 9001 locally (default). I can access the it unencrypted on NAS_IP:9001. Then I configured Nginx to point to http NAS_IP 9001 with a *.domain.tld Let's Encrypt Certificate (as for any others app). Now I can access my Nextcloud via https://cloud.domain.tld and login without apparent issues, I can upload files, share file with external link, everything in WebUI look good except when I log out I get a page in http than is not reachable.
In admin panel I have a warning that my reverse proxy is not properly configured and link to this page to solve it: https://docs.nextcloud.com/server/l...ation_server/reverse_proxy_configuration.html

When I try to connect DAVx5 on Android, it says it cannot find CardDAV and CalDAV.
I added this to the custom config tab in Nginx:
Code:
location /.well-known/carddav {
    return 301 $scheme://$host/remote.php/dav;
}

location /.well-known/caldav {
    return 301 $scheme://$host/remote.php/dav;
}


I still have the error in DAVx5. I tried to change in the above $host with $host:$port. Still not working.
I tried to enter the same but in the custom location tab, thus I don't think I'm doing it properly as it ends up with Nextcloud being completely unreachable, when I remove it's working again in WebUI but still all other errors.

Then I tried to login to the Android app, but it fails, I end up logged on the WebUI in a webview within the Nextcloud app but the apps is not connected to the account properly and I cannot sync files or anything.
I didn't try the desktop client yet but I except similar issues as well.

I found someone config online with these more 2 lines before mine in the advanced tab (also on Nextcloud doc):
Code:
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

No sure what it does but it's not solving the issues and not make it worst either.

I modified the Nextcloud config.php file and added my TrueNAS IP as trusted_proxies, not sure if it's necessary if it's on the same machine but it was mention in Nextcloud help.
It tried to add forwarded_for_headers too, both with x_forwarded_for and without x, as I don't find the forward header in the console (F12) when I connect to Nextcloud though the Nginx reverse proxy address.

I modified the overwrite.cli.url that was pointing to localhost to point to https://cloud.domain.tld (strangely it was working before this change already) but it's not helping either.

I tried to add overwritehost and overwriteprotocol to the config.php but then Nextcloud give an internal server error. When I comment, it's working again. Maybe that's the missing part? I tried IP_TrueNAS:Nginx_port_http, IP_TrueNAS:Nginx_port_https, IP_TrueNAS:Nginx_webUI_port (you never know), IP_TrueNAS:Nextcloud_port. I don't know what else I can try. Even if I set only the overwriteprototcol to https it's then giving the internal server error...

What is super strange is that I still have an old Nextcloud install on an older machine (SynologyNAS), for which I setup the redirect from Nginx on this current TrueNAS, pointing to the old machine IP, port 443, and it's all working fine without any special advance configuration nor otuching the config.php file at all.

Any idea what is missing or wrong in my Nginx and Nextcloud config? I search many online posts and topics, both here, on Nextcloud communities and documentations, nothing helped so far.

EDIT:
I solve already one issue, the redirect for CalDAV and CardDAV was correct, but DAVx5 give me an error because I have 2FA enabled and I need to login with a one time use app password.
Remain the issue with the HTTP forbidden error that prevent to login on the Android Nextcloud app. I guess I'm super close to the solution, something in the config.php must be missing or wrong.

EDIT2:
OK everything is solved.
The Nginx extra parameters for CalDAV and CardDAV was correct but you must use an app password to login with DAVx5 or other app if you have 2FA enabled, otherwise it says that it cannot reach CalDAV and CardDAV services, which is a bit misleading, even if it also suggest that the password might be wrong, which it was not suggesting if my Nginx redirect was not correctly done, that was made me guess the problem.

For the http error, I was missing some parameters in the config.php of Nextcloud, you can check my anonymized config code below, I think the most important I had to add was:
  • Check that both the local TrueNAS IP+Nextcloud port as well as the Nextcloud domain name was in the trused_domains list.
  • 'overwrite.cli.url' => 'http://TRUENAS_LOCAL_IP:9001', #and not to the domain name, one of my mistake
  • add: 'overwriteprotocol' => 'https',
  • add: 'trusted_proxies' => array (
    0 => 'TRUENAS_LOCAL_IP',
    ),
  • add forward for headers (not sure if necessary, try without first):
  • 'forwarded_for_headers' => array (
    0 => 'HTTP_X_FORWARDED',
    1 => 'HTTP_FORWARDED_FOR',
    ),
Check below if something else is missing or wrong from your 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,
    ),
  ),
  'passwordsalt' => 'xxx',
  'secret' => 'xxx',
  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'TRUENAS_LOCAL_IP:9001',
    2 => 'cloud.domain.tld',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'pgsql',
  'version' => '27.0.2.1',
  'overwrite.cli.url' => 'http://TRUENAS_LOCAL_IP:9001',
  'overwriteprotocol' => 'https',
  'dbname' => 'nextcloud',
  'dbhost' => 'nextcloud-postgres:5432',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'oc_youradminuser,
  'dbpassword' => 'xxx',
  'installed' => true,
  'instanceid' => 'xxx',
  'memories.exiftool' => '/var/www/html/custom_apps/memories/exiftool-bin/exift>
  'memories.vod.path' => '/var/www/html/custom_apps/memories/exiftool-bin/go-vo>
  'memories.vod.ffmpeg' => '/usr/bin/ffmpeg',
  'memories.vod.ffprobe' => '/usr/bin/ffprobe',
  'twofactor_enforced' => 'true',
  'twofactor_enforced_groups' =>
  array (
    0 => 'admin',
  ),
  'twofactor_enforced_excluded_groups' =>
  array (
  ),
  'enabledPreviewProviders' =>
  array (
    0 => 'OC\\Preview\\Image',
    1 => 'OC\\Preview\\HEIC',
    2 => 'OC\\Preview\\TIFF',
    3 => 'OC\\Preview\\Movie',
  ),
  'memories.gis_type' => 2,
  'trusted_proxies' =>
  array (
    0 => 'TRUENAS_LOCAL_IP',
  ),
  'forwarded_for_headers' =>
  array (
    0 => 'HTTP_X_FORWARDED',
    1 => 'HTTP_FORWARDED_FOR',
  ),
  'default_phone_region' => 'XX',
  'filelocking.enabled' => true,
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'redis' =>
  array (
    'host' => 'TRUENAS_IP_OR_REDIS_SERVER_IP_IF_NOT_ON_TRUENAS',
    'port' => REDIS_PORT_HERE,
    'timeout' => 0.0,
    'password' => 'REDIS_PASSWORD_HERE',
  ),

  'maintenance' => false,
);


After that my Nextcloud with Nginx was fully working in every situation without error. I also added Redis on my TrueNAS with some line in the config to remove the memcache warning and the default_phone_region (change XX by your country as in the link from help) and then I was all green from Nextcloud admin settings.

Thanks for taking the time to go into such detail about your Scale+NC implementation. I'm hoping you might be able to help me since you seem to accomplished more than I have at this point.

I am also having the carddav and caldave errors. When you say you added

Code:
location /.well-known/carddav {
    return 301 $scheme://$host/remote.php/dav;
}

location /.well-known/caldav {
    return 301 $scheme://$host/remote.php/dav;
}


to the nginx custom locations, how exactly did you accomplish that? I think it is done here?
Screenshot 2024-03-25 075154.jpg


But when I enter any variation of the '/.well-known/carddav' etc. and point it to my NC IP (e.g. http://192.168.1.xx:9001), nginx manager takes the reverse proxy offline.

Thanks for your help!
 

slacks425

Dabbler
Joined
Aug 6, 2023
Messages
13
Thanks for taking the time to go into such detail about your Scale+NC implementation. I'm hoping you might be able to help me since you seem to accomplished more than I have at this point.

I am also having the carddav and caldave errors. When you say you added

Code:
location /.well-known/carddav {
    return 301 $scheme://$host/remote.php/dav;
}

location /.well-known/caldav {
    return 301 $scheme://$host/remote.php/dav;
}


to the nginx custom locations, how exactly did you accomplish that? I think it is done here?
View attachment 76942

But when I enter any variation of the '/.well-known/carddav' etc. and point it to my NC IP (e.g. http://192.168.1.xx:9001), nginx manager takes the reverse proxy offline.

Thanks for your help!
Hello,
sorry for the delay in my answer, I was traveling and away from my computer for some time. I have checked and in fact I didn't use the custom locations tab, I think I tried but it never worked as intended. In fact I have everything in advanced, exactly like this:
Code:
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

rewrite ^/\.well-known/carddav https://$server_name/remote.php/dav/ redirect;
rewrite ^/\.well-known/caldav https://$server_name/remote.php/dav/ redirect;

rewrite ^/.well-known/webfinger /public.php?service=webfinger last;

location /.well-known/webfinger {
    return 301 $scheme://$host:$server_port/index.php/.well-known/webfinger;
}

location /.well-known/nodeinfo {
    return 301 $scheme://$host:$server_port/index.php/.well-known/nodeinfo;
}


On other tabs I have,
Details: Block common exploits enabled, Websockets support enabled.
Custom locations: nothing
SSL: everything enabled except HDTS subdomains (not sure what it does).

I hope it will solve your install.
 

cdog89

Explorer
Joined
Jan 19, 2024
Messages
75
Hello,
sorry for the delay in my answer, I was traveling and away from my computer for some time. I have checked and in fact I didn't use the custom locations tab, I think I tried but it never worked as intended. In fact I have everything in advanced, exactly like this:
Code:
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

rewrite ^/\.well-known/carddav https://$server_name/remote.php/dav/ redirect;
rewrite ^/\.well-known/caldav https://$server_name/remote.php/dav/ redirect;

rewrite ^/.well-known/webfinger /public.php?service=webfinger last;

location /.well-known/webfinger {
    return 301 $scheme://$host:$server_port/index.php/.well-known/webfinger;
}

location /.well-known/nodeinfo {
    return 301 $scheme://$host:$server_port/index.php/.well-known/nodeinfo;
}


On other tabs I have,
Details: Block common exploits enabled, Websockets support enabled.
Custom locations: nothing
SSL: everything enabled except HDTS subdomains (not sure what it does).

I hope it will solve your install.
You are awesome! Yes, this removed the carddav caldav error that came up in the admin settings. Thanks for your help!
 

bluepr0

Dabbler
Joined
Jan 5, 2022
Messages
19
This also helped me. Thanks a lot, @slacks425. Shouldn't this be mentioned in the Nextcloud configuration on TrueNAS documentation somewhere at least?
 
Top