How to add a new trusted domain in Nextcloud, SSH or cPanel?

Sometimes you may need to access your Nextcloud instance from a different domain name or IP address than the one you initially configured. For example, you may have moved your server to a new location, or you may want to use a custom domain name instead of an IP address. In this case, you will encounter a trusted domain issue, which prevents you from logging in to Nextcloud. [This is because Nextcloud uses trusted domains to prevent host header poisoning, a type of attack that exploits the HTTP host header value to manipulate web applications

In this tutorial, Cloudamo will show you how to add a new trusted domain to your Nextcloud configuration, so that you can access your cloud server from any domain name or IP address you want. We will assume that you have already installed Nextcloud on your server, and that you know the current and the new domain name or IP address of your server. We will also assume that you have SSH access to your server, and that you have installed Nextcloud in the /var/www/html/nextcloud directory. If your setup is different, you may need to adjust the commands and paths accordingly.

Step 1: Log in to your server via SSH

The first step is to log in to your server via SSH, using your username and password. You can use any SSH client you prefer, such as PuTTY, Terminal, or Windows PowerShell. For example, if your server’s current IP address is 192.168.0.29, and your username is admin, you can use the following command to log in:

ssh admin@192.168.0.29

You may need to enter your password and accept the server’s fingerprint if this is the first time you connect to it.

Step 2: Edit the Nextcloud configuration file

The next step is to edit the Nextcloud configuration file, which is located in the config subdirectory of your Nextcloud installation. You can use any text editor you like, such as nano, vim, or emacs. For example, to edit the file with nano, you can use the following command:

sudo nano /var/www/html/nextcloud/config/config.php

You may need to enter your password again to use sudo.

In the configuration file, you will see a section called trusted_domains, which is an array that contains the domain names or IP addresses that Nextcloud accepts as valid. For example, the initial configuration may look like this:

'trusted_domains' => 
  array (
    0 => '192.168.0.29',
  ),

This means that Nextcloud only allows access from the IP address 192.168.0.29. To add a new trusted domain, you need to append a new item to the array, with a unique index and the new domain name or IP address as the value. For example, if you want to add the domain name cloud.example.com as a new trusted domain, you can modify the configuration file like this:

'trusted_domains' => 
  array (
    0 => '192.168.0.29',
    1 => 'cloud.example.com',
  ),

You can add as many trusted domains as you need, as long as they have different indexes and values. However, it is recommended to use only one domain name for your Nextcloud instance, as it is optimized for that

Step 3: Save and close the configuration file

The final step is to save and close the configuration file. If you are using nano, you can do this by pressing Ctrl+O to save, and then Ctrl+X to exit. You do not need to restart Nextcloud or the server for the changes to take effect.

cPanel

If you use cPanel, you can add a new trusted domain to Nextcloud using the following steps:

  • Log in to your cPanel account and go to File Manager.
  • Navigate to the public_html directory and find the config.php file of your Nextcloud installation.
  • Right-click on the file and select Edit.
  • In the editor, locate the trusted_domains section and add a new item to the array with the new domain name or IP address as the value. For example, if you want to add cloud.example.com as a new trusted domain, you can modify the configuration file like this:
'trusted_domains' => 
  array (
    0 => '192.168.0.29',
    1 => 'cloud.example.com',
  ),
  • Save and close the file. You do not need to restart Nextcloud or the server for the changes to take effect.

Conclusion

You have successfully added a new trusted domain to your Nextcloud configuration. You can now access your Nextcloud instance from the new domain name or IP address, as well as the old one. You should see the Nextcloud login page without any trusted domain issue.