FastDFS + Nginx module
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了FastDFS + Nginx module,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3674字,纯文字阅读大概需要6分钟。
内容图文
前言
新项目使用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端口
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 再次尝试
如若显示组名,需要将storage.conf 文件内的disabled=false 修改为disabled=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
本文出自 “morrowind” 博客,请务必保留此出处http://morrowind.blog.51cto.com/1181631/1826174
原文:http://morrowind.blog.51cto.com/1181631/1826174
内容总结
以上是互联网集市为您收集整理的FastDFS + Nginx module全部内容,希望文章能够帮你解决FastDFS + Nginx module所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。