MongoDB GridFS 適合儲存影片、高解析度圖片等超大檔案(小檔案建議直接存檔案系統)。以下記錄在 CentOS 上編譯安裝 nginx-gridfs 模組,使 Nginx 能直接讀取 GridFS 中的 MP4 影片。
git clone https://github.com/rjhunjhunwala/nginx-gridfs
cd nginx-gridfs
git submodule init
git submodule update
(若無 git:yum -y install git)
./configure --prefix=/opt/nginx --add-module=/opt/software/nginx-gridfs
make
make install
nginx.conf GridFS 位置區塊location /video {
gridfs dbname;
root_collection=fs;
field=filename;
type=string;
user=username;
pass=passwd;
mongo 127.0.0.1:27017;
}
參數說明:
• dbname:MongoDB 資料庫名稱。
• root_collection:GridFS 集合前綴(預設 fs)。
• field:查詢欄位(如 filename、_id、content_type)。
• type:欄位類型(string、objectid 等)。
• user/pass:MongoDB 認證帳密(可選)。
• mongo:MongoDB 連線位址(支援 Replica Set)。
<video class="video-js vjs-default-skin" controls width="640" height="264" preload="auto">
<source src="https://127.0.0.1/video/filename.mp4" type='video/mp4' />
</video>
引用 Video.js 即可在瀏覽器直接播放 GridFS 中的影片,無需將檔案落地到磁碟,節省儲存空間與 I/O 開銷。
proxy_cache)可進一步提升熱門內容分發效能。