How to set reverse proxy using Caddy

Due to the long distance and bad route of my server which holds Navidrome and Nextcloud, the usage of them is not always pleased. In order to get a smooth experience, I decided to set a reverse proxy for them using my best route server. For a certain reason, I must use Caddy as my web server for reverse proxy.

How to set

Assume that your reserve proxy server domain is reverse.com, your Navidrome server is navidrome.com, your Nextcloud server is nextcloud.com .

For Navidrome

Edit your Caddyfile

1
2
3
4
5
reverse.com {
reverse_proxy https://navidrome.com {
header_up Host {http.reverse_proxy.upstream.hostport}
}
}
1
caddy reload --config Caddyfile_path

For Nextcloud

This differs from the former as it needs additional settings.

Log into your server which holds Nextcloud.

1
vim /var/www/nextcloud/config/config.php
1
2
3
4
5
6
7
8
9
'trusted_domains' => 
array (
0 => 'nextcloud.com',
1 => 'reverse.com',
),
'trusted_proxies' =>
array (
0 => 'reverse_proxy_server_ip_address',
),

Then exit and log into your reverse proxy server as root.

Edit your Caddyfile

1
2
3
4
5
reverse.com {
reverse_proxy nextcloud.com {
header_up Host {upstream_hostport}
}
}
1
caddy reload --config Caddyfile_path

Reference

  1. https://caddy.community/t/setting-up-nextcloud-behind-caddy/14787
  2. https://caddy.community/t/caddy-reverse-proxy-nextcloud-collabora-vaultwarden-with-local-https/12052
  3. https://docs.nextcloud.com/server/21/admin_manual/configuration_server/reverse_proxy_configuration.html