Nextcloud on NethServer 8 behind Nginx Proxy Manager and Pi-hole: how I fixed the 502 Bad Gateway

In my homelab I installed Nextcloud through NethServer 8 and wanted to reach it from a public hostname, going through Nginx Proxy Manager (NPM) as the reverse proxy. Internal DNS is handled by Pi-hole. It looks like a trivial setup, but I ran into a 502 Bad Gateway that cost me some time. Here is how I fixed it, hoping it saves someone else the trouble.

The layout

  • Pi-hole resolves the public name (here cloud.esempio.it) to the proxy’s IP, so from the LAN requests always go through NPM.
  • Nginx Proxy Manager (192.168.0.21) terminates TLS and forwards the request to the backend.
  • NethServer 8 (192.168.0.22) hosts Nextcloud.

In Pi-hole’s Local DNS records the service record therefore points to the proxy, while the NethServer machine has its own separate internal name:

DomainIP
cloud.esempio.it192.168.0.21 (proxy)
srv-neth01.esempio.it192.168.0.22 (NethServer)

The proxy forwards to the backend by IP, not by name: if it used the public name, Pi-hole would resolve it to the proxy itself and you would get a loop.

The question: which IP and which port in the proxy host?

The answer is the NethServer machine’s IP and port 443 with the https scheme:

  • Scheme: https
  • Forward Hostname / IP: 192.168.0.22
  • Forward Port: 443
  • Websockets Support: on

On NethServer 8, routing to Nextcloud is done by Traefik based on the request’s hostname. That is why the proxy must forward the original Host header (NPM does this by default) and the name configured in the Nextcloud module must match the one used by the client.

The problem: 502 Bad Gateway

With the configuration above I still got a 502. A 502 means the proxy cannot talk to the backend, not that something is missing in DNS. So the place to start is testing the backend directly from the proxy server:

curl -vk https://192.168.0.22 -H "Host: cloud.esempio.it"

and, while I was at it, the port that appeared in my custom configuration:

curl -v http://192.168.0.22:11000

The results:

  • port 11000 returned No route to host;
  • port 443 answered correctly with a 302 to https://cloud.esempio.it/login. The backend certificate was self-signed (ns8.test), which is normal for NethServer 8: NPM does not verify it, so it is not a problem.

The cause

In the proxy host’s Custom Nginx Configuration I had pasted a location / block taken from the official Nextcloud AIO documentation:

location / {
    proxy_pass http://192.168.0.22:11000;
    ...
}

Port 11000 belongs to Nextcloud AIO’s internal Apache, which does not exist with NethServer’s Nextcloud module. That proxy_pass overrode the Scheme/IP/port fields set in the Details tab, so NPM was trying to connect to a port nobody was listening on: hence the 502.

The fix

I removed the location / block and kept only the useful options in the custom configuration:

client_max_body_size 0;
proxy_ssl_server_name on;
proxy_ssl_name $host;
proxy_buffering off;
  • client_max_body_size 0; removes limits on upload size;
  • proxy_ssl_server_name on; and proxy_ssl_name $host; make the proxy send the backend an SNI with the hostname: essential when Traefik sits behind it, since it picks the service by name;
  • proxy_buffering off; helps with large uploads and downloads.

The other directives (Host, Upgrade, X-Forwarded-*) are not needed: NPM adds them on its own, especially with Websockets Support on.

After saving, the Nextcloud login page opens correctly from the public name.

After the first login: trusting the proxy

When running behind a reverse proxy, Nextcloud may show warnings about proxy headers in Administration → Overview, or generate redirects and links over http. In that case you need to tell it to trust the proxy’s IP and to always use https. On NethServer 8 you can do this with occ from the module instance:

runagent -m nextcloud1 occ config:system:set trusted_proxies 0 --value=192.168.0.21
runagent -m nextcloud1 occ config:system:set overwriteprotocol --value=https

The instance name (nextcloud1) can be found with runagent -l. If occ cannot be reached this way, the same change can be made in the installation’s config.php.

What I learned

  1. A 502 is not a DNS problem. If you see the proxy’s error page, DNS has already done its job: look at the proxy → backend leg.
  2. Test the backend with curl from the proxy, setting the Host header. Two commands tell you what it really answers.
  3. Watch out for configurations copied from guides for a different product. The Nextcloud AIO ones (port 11000) do not apply to NethServer’s Nextcloud module.
  4. Traefik needs SNI. The two proxy_ssl_* directives prevent problems when the backend picks the service by name.