Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

CentOS 安裝配置 nginx-gridfs 讓 Nginx 直接讀取 MongoDB GridFS 大檔案

編輯:系統常見問題解答

MongoDB GridFS 適合儲存影片、高解析度圖片等超大檔案(小檔案建議直接存檔案系統)。以下記錄在 CentOS 上編譯安裝 nginx-gridfs 模組,使 Nginx 能直接讀取 GridFS 中的 MP4 影片。

1. 取得原始碼

git clone https://github.com/rjhunjhunwala/nginx-gridfs
cd nginx-gridfs
git submodule init
git submodule update

(若無 git:yum -y install git)

2. 編譯安裝 Nginx 並掛載模組

./configure --prefix=/opt/nginx --add-module=/opt/software/nginx-gridfs
make
make install

3. 配置 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)。

4. 前端播放範例(Video.js)

<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 開銷。

適用場景

  • 影音串流平台、監控錄像存取、大型圖片素材庫。
  • 結合 Nginx 快取(proxy_cache)可進一步提升熱門內容分發效能。
Copyright © Windows教程網 All Rights Reserved