1 背景
在网站架构初期,通常将用户上传的图片,附件等资料,保存在相应服务器下,但随着网站数据量的增加,图片服务器渐渐成为整个网站的短板,缘次催生了使用fastfds的想法。
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。
在大多数业务场景中,往往需要为FastDFS存储的文件提供http下载服务,而尽管FastDFS在其storage及tracker都内置了http服务, 但性能表现却不尽如人意;所以fastDFS增加了基于当前主流web服务器的扩展模块(包括nginx/apache),其用意在于利用web服务器直接对本机storage数据文件提供http服务,以提高文件下载的性能
FastDFS内置防盗链系统防止别人通过一些技术手段绕过本站的资源展示页面,盗用本站的资源,让绕开本站资源展示页面的资源链接失效。
其加密过程为:密匙=加密函数(文件名 + 时间 + 密码). 最后生成的地址形如:http://ip:port/group1/M00/00/00/wKgAV1giDreAR9SvAAN88BNmIM8434.jpg?token=7cdb525ee2a43c4c4df5b40eacd7310f&ts=1478660360
2 参考架构图
FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。
2.2 参考架构
说明: 在每一台storage服务器主机上部署Nginx及FastDFS扩展模块,由Nginx模块对storage存储的文件提供http下载服务, 仅当当前storage节点找不到文件时会向源storage主机发起redirect或proxy动作。 注:图中的tracker可能为多个tracker组成的集群;且当前FastDFS的Nginx扩展模块支持单机多个group的情况
3 几个概念
storage_id:指storage server的id,从FastDFS4.x版本开始,tracker可以对storage定义一组ip到id的映射,以id的形式对storage进行管理。而文件名写入的不再是storage的ip而是id,这样的方式对于数据迁移十分有利。 storage_sync_file_max_delay:指storage节点同步一个文件最大的时间延迟,是一个阈值;如果当前时间与文件创建时间的差距超过该值则认为同步已经完成。 anti_steal_token:指文件ID防盗链的方式,FastDFS采用token认证的方式进行文件防盗链检查。
4 开始安装
拷贝所需文件
libfastcommon
git clone https://github.com/happyfish100/libfastcommon.git
fastdfs
https://github.com/happyfish100/fastdfs/releases
nginx
http://nginx.org/en/download.html
FastDFS Nginx Module Source Code
https://sourceforge.net/projects/fastdfs/files/FastDFS%20Nginx%20Module%20Source%20Code/
5 安装libfastcommon
//说明:libfastcommon是fastDFS的依赖包,因此先装libfastcommon
# cd libfastcommon
# ./make.sh
# ./make.sh install
//libfastcommon.so 默认安装到了/usr/lib64/libfastcommon.so,而FastDFS主程序设置的lib目录是/usr/local/lib,因此需要设置软链接(如果已存在,可以忽略)。
# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
6 安装FastDFS
# cd FastDFS
# ./make.sh
# ./make.sh install
# ll /usr/bin/fdfs_*
//查看/etc/fdfs文件,如果存在则安装成功
7配置
7.1.1 修改tracker.conf
# vim tracker.conf
disabled=false #启用配置文件
port=22122 #设置 tracker 的端口号
base_path=/data/fastdfs/tracker #设置 tracker 的数据文件和日志目录(需预先创建)
http.server_port=8080 #设置 http 端口号
注:这个http.server_port=8080 指的是在tracker服务器上启动http服务进程,如:apache或者nginx 启动时所监听的端口,这个似乎是可以不用管的,因为tracker本身就没有安装http服务
7.1.2 运行tracker
# fdfs_trackerd /etc/fdfs/tracker.conf restart
7.1.3 查看端口
# netstat -antp | grep trackerd
7.1.4 查看日志
# less /data/fastdfs/tracker/logs/trackerd.log
7.2 storage
7.2.1 修改storage.conf
# vim storage.conf
disabled=false#启用配置文件
group_name=group1 #组名,根据实际情况修改
port=23000 #设置 storage 的端口号
base_path=/data/fastdfs #设置 storage 的根目录用于存储数据和日志(需预先创建)
store_path_count=1 #存储路径个数,需要和 store_path 个数匹配
store_path0=/data/fastdfs/storage #存储路径
tracker_server=10.10.10.124:22122 #tracker 服务器的 IP 地址和端口号
http.server_port=8080 #设置storage上启动的http服务的端口号,如安装的nginx的端口号
7.2.2 运行fdfs_storaged
# fdfs_storaged /etc/fdfs/storage.conf restart
7.2.3 查看是否端口已启用
# netstat -antp | grep storage
7.2.4 查看日志
# less /data/fastdfs/storage/logs/storaged.log
7.3 其他命令
# fdfs_monitor /etc/fdfs/storage.conf
# fdfs_monitor /etc/fdfs/client.conf
如果查看到ACTIVE,则说明成功
8 安装nginx
# cd nginx-1.10.1
# cp -rf /usr/include/fast* /usr/local/include/
# ./configure --prefix=/usr/local/nginx --add-module=../fastdfs-nginx-module/src
# make
# make install
# vim /usr/local/nginx/conf/nginx.conf
//修改为如下:
listen 8080;
location ~/group1/M00 {
root /data/fastdfs/storage/data;
ngx_fastdfs_module;
}
//说明:安装过程中可能因为依赖软件没有安装而报错,需要安装,安装命令如下:
yum install pcre*
yum install zlib*
8.1 修改 fastdfs的nginx模块的配置文件 mod_fastdfs.conf
# vim /etc/fdfs/mod_fastdfs.conf
base_path=/data/fastdfs #保存日志目录
tracker_server=10.10.10.124:22122 #tracker 服务器的 IP 地址以及端口号
storage_server_port=23000 #storage 服务器的端口号
group_name=group1 #当前服务器的 group 名
url_have_group_name = true #文件 url 中是否有 group 名
store_path_count=1 #存储路径个数,需要和 store_path 个数匹配
store_path0=/data/fastdfs/storage #存储路径
http.need_find_content_type=true #从文件扩展名查找文件类型 ( nginx 时 为true)
group_count = 1 #设置组的个数
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/storage
8.2 修改/etc/fdfs/http.conf
# vim /etc/fdfs/http.conf
http.anti_steal.check_token=true #开启token认证
8.3 启动nginx
# /usr/local/nginx/sbin/nginx (第一次开启的命令)
# /usr/local/nginx/sbin/nginx –s reload(重启的命令)
8.4 查看日志./logs/error.log
less /usr/local/nginx/logs/error.log