在 Nextcloud 中僅對媒體內容啟用代理快取

3 min

language: ja bn en es hi pt ru zh-cn zh-tw

大家好,我是無能。

我的興趣是聽音樂。
我已經取消了所有訂閱服務,並將所有音樂等內容都自建了,但要說 Nextcloud 的不滿之處,那就是載入速度慢

環境
Apache 家用伺服器:Nextcloud 的存放位置
nginx VPS:反向代理兼快取伺服器

由於每次都會存取伺服器主機上的內容,這也會增加伺服器本身的負載,因此我決定改進這一點,重新審視作為反向代理的 VPS 端的代理快取設定。

我將快速寫下 nginx.conf 的語法。

        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;

}

實際上,
proxy_ignore_headers Cache-Control Expires;
proxy_hide_header Cache-Control;
我不確定在這裡呼叫 Cache-Control 兩次是否有意義,但總之它現在運作良好。

另外,關於 **/index.php/apps/music/api/file/**,我安裝了音樂播放外掛程式,由於該外掛程式本身可能會呼叫 Nextcloud 的 API,因此其運作方式與 Nextcloud 正常的媒體播放不同,所以我新增了這個設定。
沒有安裝的人應該不需要新增。

非常舒適(流暢)。

參考來源與修改心得

參考來源:How to use caching reverse proxy with NextCloud, problems with NC setting “cache-control”

proxy_pass https://x.x.x.x/remote.php/webdav/;
那位作者是這樣做的,但在我的情況下並沒有成功。
我發現 Nextcloud 內部有兩種情況:指定 /webdav/ 和指定 /dav/,透過分別撰寫這兩種語法,我成功了。

特別是當我一開始對整個 Nextcloud 進行快取時,操作變得一團糟,果然沒有成功(笑)。

我曾擔心內容太多會很快讓快取爆滿,但對於圖片和音樂等檔案來說,這種方法是有效的。

我還嘗試了其他各種方法來加速,如果我有心情會再更新。

Related Posts