Nextcloud in Docker with Caddy proxy

I’m trying to install Nextcloud on my server with Docker using a Caddy reverse proxy. Caddy is working for other services so I will just copy the Caddyfile here.

My issue is that on the host machine with localhost it is working, with the IP it says it is not a trusted domain, and with my domain it says “502 Bad Gateway”. Please help I’ve already tried multiple configurations but can not get it working.

Caddyfile:

{domain} {
  tls {email}
  tls {
     dns godaddy
  }
   
  # Enable basic compression
  gzip
 
  # Service discovery via well-known
  redir /.well-known/carddav /remote.php/carddav 301
  redir /.well-known/caldav /remote.php/caldav 301
  
  proxy / http://nextcloud:8080 {
    # X-Forwarded-For, etc...
    transparent
    
    # Nextcloud best practices and security
    header_downstream Strict-Transport-Security "max-age=15552000;"
    header_downstream Referrer-Policy "strict-origin-when-cross-origin"
    header_downstream X-XSS-Protection "1; mode=block"
    header_downstream X-Content-Type-Options "nosniff"
    header_downstream X-Frame-Options "SAMEORIGIN"
  }
}

docker-compose file:

version: '3.7'

services:
  db:
    container_name: nextcloud-db
    image: mariadb
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - db:/var/lib/mysql
    env_file:
      - ./nextcloud/config/db.env
    environment:
      - MYSQL_ROOT_PASSWORD={pw}
    networks:
      - db

  app:
    container_name: nextcloud
    image: nextcloud
    ports:
      - 8080:80
    volumes:
      - nextcloud:/var/www/html
    env_file:
      - ./nextcloud/config/db.env
    environment:
      - MYSQL_HOST=db
      - NEXTCLOUD_TRUSTED_DOMAINS="localhost {host ip} {domain}"
    restart: always
    networks:
      - proxy
      - db
    depends_on:
      - db

volumes:
  db:
  nextcloud:

networks:
  db:

Figured it out. In the Caddyfile the nextcloud port should be 80 instead of 8080 as it is in the inner network.

Can you share the Caddyfile without errors ?