Enabling proxy cache only for media content in Nextcloud
Hello, I am Incompetent.
My hobby is listening to music.
I canceled all subscription services and self-hosted all my music, etc., but if I had one complaint about Nextcloud, it would be its slow loading speed.
Environment
Apache home server: Where Nextcloud is hosted
nginx VPS: Reverse proxy and cache server
Since the content itself is accessed from the main server every time, leading to a load on the server itself, I wanted to improve this and decided to review the proxy cache on the VPS side, which also acts as a reverse proxy.
Here's the nginx.conf syntax quickly.
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://192.168.10.101/;
}
location /remote.php/dav/ {
# Caching
proxy_cache zone2;
add_header X-Cache-Status $upstream_cache_status; # debug
proxy_cache_valid 200 90d;
proxy_pass http://192.168.10.101/remote.php/dav/;
proxy_ignore_headers Cache-Control Expires;
proxy_hide_header Cache-Control;
proxy_hide_header Pragma;
}
location /remote.php/webdav/ {
proxy_cache zone2;
add_header X-Cache-Status $upstream_cache_status; # debug
proxy_cache_valid 200 90d;
proxy_pass http://192.168.10.101/remote.php/webdav/;
proxy_ignore_headers Cache-Control Expires;
proxy_hide_header Cache-Control;
proxy_hide_header Pragma;
}
location /index.php/apps/music/api/file/ {
proxy_cache zone2;
add_header X-Cache-Status $upstream_cache_status; # debug
proxy_cache_valid 200 90d;
proxy_pass http://192.168.10.101/index.php/apps/music/api/file/;
proxy_ignore_headers Cache-Control Expires;
proxy_hide_header Cache-Control;
proxy_hide_header Pragma;
}
Actually,
proxy_ignore_headers Cache-Control Expires;
proxy_hide_header Cache-Control;
I'm not sure if there's any meaning in calling Cache-Control twice here, but it works fine for now.

Also, regarding **/index.php/apps/music/api/file/**, I have installed a music playback plugin, and since the plugin itself probably calls the Nextcloud API, which differs from the normal Nextcloud media playback behavior, I added this. If you haven't installed it, you probably don't need to add it.

It's quite comfortable.
Reference and Modifications
Reference : How to use caching reverse proxy with NextCloud, problems with NC setting “cache-control”
proxy_pass https://x.x.x.x/remote.php/webdav/;
This person did it this way, but it didn't work for me.
I realized that there are two cases in Nextcloud, one specified with /webdav/ and another with /dav/, and I succeeded by writing the syntax for each.
Especially when I first tried to cache Nextcloud entirely, the behavior became messed up, and as expected, it didn't work (lol).
I thought the cache would fill up quickly with too much content, but it is effective for files like images and music.
I've also managed to speed it up in various other ways, so I'll update if I feel like it.