Nginx reverse proxy, what to write in nextcloud's config.php?

Hi everyone, i installed Nextcloud on a Freenas’s Jail via PBI. It does work perfectly if i access it locally, but i’m currently running a NGINX reverse proxy to handle SSL and to easily access all my jails. Since i cannot access nextcloud (it is the only jail giving me a headache), i was wondering what i have to put in the Nextcloud’s config.php file. I tried playing around with what i found in the documantation with no success so far.

Can anyone help me?

Thank you very much.

Bump :frowning:

Bump :sweat:

Bump :disappointed:

I’m not using Freenas, but I do have Nextcloud set up behind a NGINX reverse proxy. It sounds like your NGINX configuration is lacking. You need to make sure your configuration is correct in both places, starting with Nextcloud and then working on NGINX.

In Nextcloud’s config.php, configure the following:

  • trusted_domains: this should include the local host name for testing, unless you no longer need it, and the external host name to access it from outside the network through the reverse proxy
  • trusted_proxies: the IP address of the reverse proxy
  • Make sure Nextcloud is set to use HTTP, not HTTPS, and that it isn’t redirecting or anything crazy like that. Your NGINX instance will take care of that HTTPS, but will communicate directly with Nextcloud using HTTP.

In NGINX, you basically need to add and enable a server block which listens on port 443, sets the correct headers, and forwards to the IP of Nextcloud. I’ve included a sample configuration which is similar to my setup:

server {
    listen 80;
	server_name cloud.example.com;

	return 301 https://$server_name$request_uri;
}

server {
	listen 443 ssl;
	server_name cloud.example.com;

	access_log /var/log/nginx/cloud.example.com.access.log;
	error_log /var/log/nginx/cloud.example.com.error.log;

	client_max_body_size 0;
	underscores_in_headers on;

	ssl on;
	ssl_certificate /etc/letsencrypt/live/cloud.example.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/cloud.example.com/privkey.pem;

	ssl_stapling on;
	ssl_stapling_verify on;

	location / {
		proxy_headers_hash_max_size 512;
		proxy_headers_hash_bucket_size 64;
		proxy_set_header Host $host;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

		add_header Front-End-Https on;
		# whatever the IP of your cloud server is
		proxy_pass http://192.168.1.50;
	}
}

Beyond this, you’ll need to check basic things like the NGINX error and access logs to see what’s not working, and post more info here if you need more help.

2 Likes

Thank you, i tried using your suggestions but i reach a point where i have two outcomes:

  1. I set up both NGINX and config.php the way you suggested and as soon as i type “https://mydomain.net/nextcloud”, the URL becomes “https://mydomain.net/login” therefore resulting in a 404.

2)I add the overwrite_webroot line (setting it as /nextcloud) which results in a “ERR_TOO_MANY_REDIRECTS”.

Trust me when i say i have no clue what to do right now…

You need to focus on the NGINX configuration at this point. What’s happening is that NGINX is now passing the request to Nextcloud (so we know you’re making progress), but then Nextcloud sends a redirect from https://mydomain.net/nextcloud to https://mydomain.net/login. That means your browser sends a new request to https://mydomain.net/login that NGINX doesn’t know how to handle, because you probably didn’t define a location block for /login.
As you can see from my configuration, since I’m using a subdomain, I don’t worry about going to mydomain.net/nextcloud but instead I go to cloud.mydomain.net with / as the location so any subdirectories still fall under /.
But in your case, you could hypothetically add all possible locations that Nextcloud would use, starting with /login, but my instinct tells me this is the wrong approach because it’s hard to know all possible locations, and new locations may be added in the future, breaking your site.
So, I would either start by trying to add / as a location (assuming you will never need to run any other sites behind the reverse proxy), or see if you can change your cloud hostname to cloud.mydomain.net.
Or maybe there’s another better option that I’m not thinking of (I’m not an NGINX expert, just learned enough to get it working with my setup).

Maybe post your full NGINX config (minus any revealing information) to give a better idea of where you’re at with it.

What i don’t understand is that it happened already i had plugins generating links without the base URL i set up in NGINX. All i had to do with those was, in fact, set up a base URL and from then on they would start displaying properly. So far Nextcloud is the only one giving me a headache.

Anyway, my NGINX setup is pretty much standard. I added the HTTPS part and the config about reverse proxies are as follows:

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

 location /sonarr {
 proxy_pass http://192.168.192.202:8989;
 proxy_redirect off;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

 location /couchpotato {
 proxy_pass http://192.168.192.201:5050;
 proxy_redirect off;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

 location /headphones {
 proxy_pass http://192.168.192.203:8181;
 proxy_redirect off;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

Quick update, the “issue” is not on NGINX since trying to access nextcloud locally through 192.168.1.8/nextcloud after adding a webroot (/nextcloud) gives me “ERR_TOO_MANY_REDIRECTS”

bump :cold_sweat:

Bump :sweat:

Bumpy bump

Sorry, I haven’t received any email notifications from this thread in about a week, and I haven’t logged in here since then.

Ok, I see what you mean. Nextcloud seems to be in a redirect loop, so in that case you would need to modify the Nextcloud config. Have you changed any other parameters besides overwritewebroot? Maybe you need to use overwritehost as well?

Reverse Proxy Configuration

Also take a look at your htaccess.RewriteBase

Config.php Parameters

Another thing you mentioned was about using HTTPS. Are you using HTTPS with Nextcloud? If you are, that could be related to the problem because it would redirect from HTTP to HTTPS. I would recommend getting things working over HTTP first, then work on adding HTTPS later.

I am having the exact same problem. My config has been modified to add the domain of my nginx server to the trusted_domains along with several overwrite modifications (below). If there is an update on how to fix this, please let me know.

  'trusted_domains' => 
  array (
    0 => '192.168.1.85:444',
    1 => 'home.server.com',
  ),
  'overwrite.cli.url' => 'https://home.server.com',
  'overwritehost'     => 'home.server.com',
  'overwriteprotocol' => 'https',
  'overwritewebroot'  => '/nextcloud',

When you say you’re having the exact same problem, do you mean you’re getting a redirect loop and the browser returns “ERR_TOO_MANY_REDIRECTS”?
I think it would be helpful to see the results of both the NGINX access logs and Apache access logs, so we can see what’s causing the redirect loop. Without that, we’re just doing trial and error which is extremely difficult for someone without access to the machines.

Same redirect problem here,

a way to upload the log files as mine are a bit long? XD

Also not sure if i found the access log file for nginx

Ok, so you’re getting a redirect loop… Do you have the same configuration as the original poster? As in do you have nextcloud running with Apache, no ssl, behind a reverse proxy running nginx?
Otherwise troubleshooting steps will be different.

So far i’ve installed nextcloud via the pbi on freenas, (apache)

next ive installed a nginx server with letsencrypt
and set it up for reversed proxy
currently domain.net/nextcloud
(Not quite sure yet how to remove the /nextcloud from domain in nginx.
i just wanted to use NC for school and it seemed a fun project to tackle but 0_o)

nginx now should communicate with nextcloud over http
and nginx to client over https (the nginx errors are already secured x0)

i’m also open for teamviewer, might learn something from a guru doing his/hers magic :smiley:

Maybe nextcloud sees its coming from a https and tries to redirect its self? while it should not?

I’m no guru, but given enough time and patience I can probably help you solve your issue.
How about posting your nextcloud config (remove sensitive values), Apache config, and nginx config.