I had this problem last April:
Nextcloud “Internal Server Error” Mariadb Swag
I recently bought new server hardware, and in rebuilding this server, I had the same problems as last April. After troubleshooting, I turned off the firewall, and everything started working.
There are firewall rules that prevent the Nextcloud web container from communicating with the backend database container.