jacky4955 发表于 2018-11-8 08:12:06

FastDFS + Nginx module-morrowind

  前言
  新项目使用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的错误日志中会看到类似下面的报错信息
  ERROR - file: fastdfs-nginx-module/src//common.c, line: 155, load conf file "/etc/fdfs/mod_fastdfs.conf" fail, ret code: 2
  ERROR - file: ini_file_reader.c, line: 394, include file "http.conf" not exists, line: "#include http.conf"
  ERROR - file: shared_func.c, line: 970, open file /etc/fdfs/mime.types fail, errno: 2, error info: No such file or directory


页: [1]
查看完整版本: FastDFS + Nginx module-morrowind