Search code examples
dockernginxraspberry-pireverse-proxynextcloud

502 Bad Gateway Error on Nextcloud Docker Container proxied through Subdomain on Nginx Webserver


I am running an Nginx Webserver on my Raspberry Pi 4. I am trying to configure a reverse proxy on subdomain to a Nextcloud Docker container. However I am getting a 502 Bad Gateway error when I try to visit this container in my browser. I have made sure to generate an SSL certificate for the subdomain I am trying to server Nextcloud over.

This is what the server block for my subdomain looks like:

server {
       listen 443 ssl;
       server_name subdomain.domain.tld;
       ssl_certificate /pathtokey/subdomain.domain.tld/fullchain.pem;
       ssl_certificate_key /pathtokey/subdomain.domain.tld/privkey;
       location / {
                proxy_pass https://127.0.0.1:9000/;
                proxy_ssl_server_name on;
       }
}

And this is what my docker-compose.yml file for Nextcloud looks like:

version: '2'

volumes:
  nextcloud:
  db:

services:
  db:
    image: linuxserver/mariadb
   # command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=<rootPassword>
      - MYSQL_PASSWORD=<mysqlPassword>
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  app:
    image: nextcloud:fpm
    ports:
      - 127.0.0.1:9000:9000
    links:
      - db
    volumes:
      - /mnt/hdd/nextcloud:/var/www/html
    restart: always

After changing the .yml file, I make sure to run docker-compose up -d. After changing nginx.conf file I run sudo systemctl restart nginx. I have also run sudo nginx -t.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

I am not sure where my mistake is in these configurations. I would appreciate any advice on how to fix this.


Solution

  • You are using nextcloud:fpm image which is only a php fpm instance without a web server.
    Your nginx proxy config is fine but it won't work because you will need nginx fastcgi_proxy for this to proxy request to a backend php instance.
    Here's a simple illustration:
    nginx(fastcgi) <-> php-fpm(nextcloud) <-> db

    1st solution:

    May refer to nextcloud official doc on how to configure nginx or simply copy the config: nginx configuration

    2nd solution:

    Use nextcloud:apache image instead. This image already included an apache web server and you can directly access it without needing another nginx instance.