Changing the domain name of a Nextcloud server is fairly straight forward, but it is not officially supported. That's okay, because you are an expert, and so you do it anyway.
1. Put your Nextcloud in maintenance mode
2. Stop the Apache server while making changes
3. Change the V-host settings. E.g. ServerName in your Apache configuration.
4. Change the trusted domain in your Nextcloud config/config.php file:
$CONFIG = array ( // ... 'trusted_domains' => array ( 0 => 'next.example.com', ), // ... )
5. Create a DNS A record for your new domain name that points to the IP address of your Nextcloud server.
6. (Recommended) redirect requests from the old domain to the new. E.g:
RewriteEngine on RewriteRule (.*) http://newdomain.example.com/$1 [R=301,L,QSA]
QSA will append the query string (if any) to the destination.
$1 is a backreference containing the requested path. E.g. Makes the client request the same path on the new domain.
7. (Optionally) remove the A record for your old domain name if no longer needed.
Changing the domain not officially supported
Changing the domain after the first setup is currently not supported by Nextcloud. That is mainly because Nextcloud’s apps don’t support changing the domain after they are set up.
According to the official documentation, changing the domain name may not be possible because some apps might not support it. Well, maybe those apps are just poorly made. Imo. Web applications should function independent from the domain, and if not, there should be a way to change the domain easily. E.g. Database entries and so on.
Many Nextcloud users might be first-time users, hobbyists experimenting, and so on. It is in our interest to make a possibly future domain-move an easy and smooth experience. Some, like myself, may have installed their first Nextcloud on a temporary or inconvenient domain, but people might hesitate moving because they fear having to start over from scratch.
- Domain Change - docs.nextcloud.com