前言
新項目使用FastDFS 已經有一段時間了,最近又與Nginx 做了結合。趁有時間,做下整理。
需求
使用FastDFS 存儲資源(圖檔、文檔、音頻、視訊...)檔案,使用Nginx 提供資源請求服務。
PS:FastDFS特别适合大中型網站使用,是以這裡選型時一定要注意。不是說小型不适用,而是一定要想清楚,是否一上來就有必要使用DFS這類應用。對于中小型網站,使用本地檔案系統、NFS或Samba,就可以很好的解決此類需求。
網站的架構是随着業務增長而演變的,而非一蹴而就。
具體還是需要根據自身需求以及人員能力而定。
解決方案
确定使用FastDFS+Nginx 做圖檔服,就來先簡單說下FastDFS的工作方式。
Clinet 詢問tracker 可用storage 節點并傳回給Client,Client 擷取到可用節點資訊後直接和storage 通訊完成檔案上傳。
下載下傳與上傳唯一的差別就是上傳時詢問storage 不需要參數,而下載下傳時需要檔案辨別(組名和檔案名)參數
相關名詞
FastDFS 分為tracker和storage ,隻能通過Clinet API 通路。
tracker 為跟蹤服務,負責排程工作
storage 為存儲服務,檔案都儲存在storage 伺服器上
group 組,也是卷。同組内的檔案是完全相同的
檔案辨別 由組名和檔案名(含路徑)構成
meta data 檔案相關屬性,鍵值對(Key Value Pair)方式
通信協定
協定包由兩部分構成:header 和body
header 共10位元組,格式如下:
8 bytes body length
1 byte command
1 byte status
body 資料包格式取決于具體指令,body可以為空
安裝
源碼目錄:/usr/local/src
nginx module 目錄:/usr/lib64/fastdfs/
FastDFS 項目位址 https://github.com/happyfish100/fastdfs
新版需要libfastcommon庫 https://github.com/happyfish100/libfastcommon.git
nginx module https://github.com/happyfish100/fastdfs-nginx-module.git
下載下傳源碼:
$cd /usr/local/src/
$git clone https://github.com/happyfish100/libfastcommon.git
$git clone https://github.com/happyfish100/fastdfs.git
$git clone https://github.com/happyfish100/fastdfs-nginx-module.git
安裝libfastcommon
$cd /usr/local/src/libfastcommon
$sudo /bin/sh make.sh
$sudo /bin/sh make.sh install
安裝FastDFS
$cd /usr/local/src/fastdfs
$sudo /bin/sh make.sh
$sudo /bin/sh make.sh install
#或使用DESTDIR 指定安裝目錄
$sudo DESTDIR=/usr/local/fdfs ./make.sh install
$sudo cp conf/* /etc/fdfs/
$sudo cp init.d/* /etc/init.d/
$sudo chkconfig fdfs_storaged on
$sudo chkconfig fdfs_trackerd on
修改配置檔案為實際值
tracker.conf
bind_addr=
port=22122
base_path=/home/yuqing/fastdfs
store_group=group2
reserved_storage_space = 10%
run_by_group=
run_by_user=
allow_hosts=*
http.server_port=8080
storage.conf
group_name=group1
bind_addr=
port=23000
base_path=/home/yuqing/fastdfs
max_connections=256
store_path0=/home/yuqing/fastdfs
tracker_server=192.168.209.121:22122
run_by_group=
run_by_user=
allow_hosts=*
http.domain_name=
http.server_port=8888 #對應nginx或web端口
mod_fastdfs.conf
base_path=/tmp
tracker_server=192.168.209.121:22122
storage_server_port=23000
group_name=group2
url_have_group_name = false
store_path0=/home/yuqing/fastdfs
log_level=info
log_filename=
fdfs_storaged
#将/usr/local/bin替換為實際執行目錄,并修改 CONF為直接路徑
$sed -i 's/\/usr\/local/\bin/\/usr\/bin/g' /etc/init.d/fdfs_storaged
CONF=/etc/fdfs/fdfs_storaged
fdfs_trackerd
#将/usr/local/bin替換為實際執行目錄
$sed -i 's/\/usr\/local/\bin/\/usr\/bin/g' /etc/init.d/fdfs_trackerd
CONF=/etc/fdfs/tracker.conf
安裝nginx module
拷貝庫檔案到lib64目錄下
mv /usr/local/src/fastdfs-nginx-module/src /usr/lib64/fastdfs
重新編譯nginx 增加fastdfs module
./configure --with-http_v2_module --add-module=/usr/lib64/fastdfs --with-http_geoip_module
make && make install
server nginx restart
增加nginx 配置
location /M00 {
root /home/yuqing/fastdfs/data;
ngx_fastdfs_module;
}
這樣一來就完成了整個安裝和配置
可通過如下指令測試是否可用
fdfs_test conf/client.conf upload test.txt
執行成功後,會傳回如下資訊
file timestamp=2016-07-13 19:44:40
file size=28
file crc32=751420880
example file url: http://10.200.6.3/group2/M00/00/13/CsgGA1eGKaiAQOJOAAAAHCzJxdA233_big.txt
若能通路到傳回的url,則說明配置成功,如若報錯,可去掉組名group2 再次嘗試
如若顯示組名,需要将mod_fastdfs.conf 檔案内的url_have_group_name = false 修改為url_have_group_name = true
需要注意的是,一定要将fastdfs源碼目錄下的conf/http.conf 和 conf/mime.types 拷貝到/etc/fdfs目錄下,如fastdfs 配置目錄下缺少這兩個檔案,nginx 是無法正常運作的,在nginx的錯誤日志中會看到類似下面的報錯資訊