首页 > 技术文章 > 搭建单机版的FastDFS服务

lixinliang 2018-01-25 15:03 原文

一,原理讲解

  FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

   FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

从中可以看到,Client想上传图片,它先向Tracker进行询问,Tracker查看一下登记信息之后,告诉Client哪个storage当前空闲,Tracker会把IP和端口号都返回给Client,Client在拿到IP和端口号之后,便不再需要通过Tracker,直接便向Storage进行上传图片,Storage在保存图片的同时,会向Tracker进行汇报,告诉Tracker它当前是否还留有剩余空间,以及剩余空间大小。汇报完之后,Storage将服务器上存储图片的地址返回给Client,Client可以拿着这个地址进行访问图片。

 

二,环境:准备一台服务器,首先保证可以上网,本次搭建是基于centos6.8。

三,搭建步骤:

1.1准备软件包

[root@fastdfs ~]# mkdir /usr/local/software 

[root@fastdfs software]# ll 

总用量 1248  

-rw-r--r--. 1 root root  17510 11月 28 00:19 fastdfs-nginx-module_v1.16.tar.gz  

-rw-r--r--. 1 root root 345400 11月 28 00:20 FastDFS_v5.05.tar.gz  

-rw-r--r--. 1 root root 102378 11月 28 00:19 libfastcommon-master.zip  

-rw-r--r--. 1 root root 804164 11月 28 01:11 nginx-1.6.2.tar.gz

 

 2.1 安装libfastcommon
 
[root@fastdfs software]# unzip libfastcommon-master.zip -d /usr/local/fast/ 
 
2.2 进入目录

[root@fastdfs software]# cd /usr/local/fast/libfastcommon-master/  

[root@fastdfs libfastcommon-master]# ll 

总用量 28  

-rw-r--r--. 1 root root 2913 2月  27 2015 HISTORY  

-rw-r--r--. 1 root root  582 2月  27 2015 INSTALL  

-rw-r--r--. 1 root root 1342 2月  27 2015 libfastcommon.spec  

-rwxr-xr-x. 1 root root 2151 2月  27 2015 make.sh  

drwxr-xr-x. 2 root root 4096 2月  27 2015 php-fastcommon  

-rw-r--r--. 1 root root  617 2月  27 2015 README  

drwxr-xr-x. 2 root root 4096 2月  27 2015 src 

 

2.3 编译

 

[root@fastdfs libfastcommon-master]# ./make.sh 

 

2.4 安装

 

[root@fastdfs libfastcommon-master]# ./make.sh install  

 

3.1 创建软链接

 

FastDFS主程序设置的目录为/usr/local/lib/,所以我们需要创建/ usr/lib64/下的一些核心执行程序的软连接文件。如下所示。

[root@fastdfs libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so  

[root@fastdfs libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so  

[root@fastdfs libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so  

[root@fastdfs libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so  

 

4,安装FastDFS

 

4.1  进入到cd /usr/local/software下,解压FastDFS_v5.05.tar.gz文件 

 

[root@fastdfs fast]# cd /usr/local/software/  

[root@fastdfs software]# ll  

总用量 1248  

-rw-r--r--. 1 root root  17510 4月  11 03:28 fastdfs-nginx-module_v1.16.tar.gz  

-rw-r--r--. 1 root root 345400 4月  11 03:28 FastDFS_v5.05.tar.gz  

-rw-r--r--. 1 root root 102378 4月  11 03:28 libfastcommon-master.zip  

-rw-r--r--. 1 root root 804164 4月  11 03:29 nginx-1.6.2.tar.gz  

[root@fastdfs software]# tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local/fast/

 

4.2  编译安装

 

[root@fastdfs software]# cd /usr/local/fast/FastDFS/  

[root@fastdfs FastDFS]# ./make.sh  

[root@fastdfs FastDFS]# ./make.sh install 

安装完后,服务脚本位置如下:

[root@fastdfs FastDFS]# cd /etc/init.d/ && ls | grep fdfs  

fdfs_storaged  

fdfs_trackerd  

 配置文件位置如下:

[root@fastdfs init.d]# cd /etc/fdfs/  

[root@fastdfs fdfs]# ll  

总用量 20  

-rw-r--r--. 1 root root 1461 4月  11 04:10 client.conf.sample  

-rw-r--r--. 1 root root 7829 4月  11 04:10 storage.conf.sample  

-rw-r--r--. 1 root root 7102 4月  11 04:10 tracker.conf.sample  

FastDFS一系列执行脚本如下,可以看到有上传文件脚本、下载文件脚本等等。

[root@fastdfs fdfs]# cd /usr/bin/ && ls | grep fdfs  

fdfs_appender_test  

fdfs_appender_test1  

fdfs_append_file  

fdfs_crc32  

fdfs_delete_file  

fdfs_download_file  

fdfs_file_info  

fdfs_monitor  

fdfs_storaged  

fdfs_test  

fdfs_test1  

fdfs_trackerd  

fdfs_upload_appender  

fdfs_upload_file 

 

4.3  修改配置文件

   因为FastDFS服务脚本设置的bin目录为/usr/local/bin/下,但是实际我们安装在了/u sr/bin/下面。所以我们需要修改FastDFS配置文件中的路径,也就是需要修改两个配置文件, 使用命令vim /etc/init.d/fdfs_storaged进入编辑模式,然后直接输入":",光标会定位到最后一行,在":"后输入"%s+/usr/local/bin+/usr/bin",如下图所示。输入完之后回车,会提示修改了7处。为了确保所有的/usr/local/bin都被替换了,我们可以再打开文件确认一下。

 接着修改第二个配置文件,我们使用命令vim /etc/init.d/fdfs_trackerd进入编辑模式,接着按照上面那样输入":%s+/usr/local/bin+/usr/bin "并按回车,同样会提醒我们修改了7处。

 

5,配置跟踪器

5.1  进入到/etc/fdfs目录并且复制一份tracker.conf.sample并命名为tracker.conf,如下所示。

 

[root@fastdfs bin]# cd /etc/fdfs/  

[root@fastdfs fdfs]# ll  

总用量 20  

-rw-r--r--. 1 root root 1461 4月  11 04:10 client.conf.sample  

-rw-r--r--. 1 root root 7829 4月  11 04:10 storage.conf.sample  

-rw-r--r--. 1 root root 7102 4月  11 04:10 tracker.conf.sample  

[root@fastdfs fdfs]# cp tracker.conf.sample tracker.conf  

5.2  使用命令vim /etc/fdfs/tracker.conf进入编辑模式,然后修改base_path的值为/fastdfs/tracker,如下图所示

 

 5.3 我们在上图配置文件中配置的/fastdfs/tracker目前是不存在的,因此我们需要创建一下该目录

 

[root@fastdfs fdfs]# mkdir -p /fastdfs/tracker  

 

 5.4   配置防火墙,放开tracker使用的端口22122,使用命令vim /etc/sysconfig/iptables进入编辑模式,添加一行内容-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT,如下图所示。

 

 5.5 重启防火墙

 

[root@fastdfs fdfs]# service iptables restart  

iptables:将链设置为政策 ACCEPT:filter           [确定]  

iptables:清除防火墙规则:                                 [确定]  

iptables:正在卸载模块:                                     [确定]  

iptables:应用防火墙规则:                                 [确定]  

 

5.6  在启动tracker之前,/fastdfs/tracker目录下是没有任何文件的,如下所示

 

[root@fastdfs tracker]# cd /fastdfs/tracker/ && ll  

总用量 0 

启动tracker,启动完之后,可以看到这个目录下多了两个目录data和logs。如下所示。

[root@fastdfs tracker]# /etc/init.d/fdfs_trackerd start   

Starting FastDFS tracker server:   

[root@fastdfs tracker]# cd /fastdfs/tracker/ && ll  

总用量 8  

drwxr-xr-x. 2 root root 4096 4月  11 05:01 data  

drwxr-xr-x. 2 root root 4096 4月  11 05:01 logs

 

5.7  设置开机自启动,在rc.local文件中添加/etc/init.d/fdfs_trackerd start,如下所示。

 

[root@fastdfs tracker]# vim /etc/rc.d/rc.local  

#!/bin/sh  

#  

# This script will be executed *after* all the other init scripts.  

# You can put your own initialization stuff in here if you don't  

# want to do the full Sys V style init stuff.  

touch /var/lock/subsys/local  

/etc/init.d/fdfs_trackerd start 

 

6,配置FastDFS存储

6.1  进入/etc/fdfs目录,复制一份storage.conf.sample文件并命名为storage.conf,如下所示。

 

[root@fastdfs tracker]# cd /etc/fdfs/  

[root@fastdfs fdfs]# ll  

总用量 28  

-rw-r--r--. 1 root root 1461 4月  11 04:10 client.conf.sample  

-rw-r--r--. 1 root root 7829 4月  11 04:10 storage.conf.sample  

-rw-r--r--. 1 root root 7098 4月  11 04:45 tracker.conf  

-rw-r--r--. 1 root root 7102 4月  11 04:10 tracker.conf.sample 

 

[root@fastdfs fdfs]# cp storage.conf.sample storage.conf  

[root@fastdfs fdfs]# ll  

总用量 36  

-rw-r--r--. 1 root root 1461 4月  11 04:10 client.conf.sample  

-rw-r--r--. 1 root root 7829 4月  11 05:07 storage.conf  

-rw-r--r--. 1 root root 7829 4月  11 04:10 storage.conf.sample  

-rw-r--r--. 1 root root 7098 4月  11 04:45 tracker.conf  

-rw-r--r--. 1 root root 7102 4月  11 04:10 tracker.conf.sample  

 

6.2  修改storage.conf文件 ,我们使用命令vim /etc/fdfs/storage.conf进入编辑模式,对以下四项进行修改,192.168.156.13是我的虚拟机的IP,大家根据自己虚拟机的IP自行设置。

 

base_path=/fastdfs/storage  

store_path0=/fastdfs/storage  

tracker_server=192.168.156.13:22122  

http.server_port=8888  

 

6.3  创建存储目录,如下所示。

 [root@fastdfs fdfs]# mkdir -p /fastdfs/storage  

 

6 .4  配置防火墙,允许外界访问storage的默认端口23000,如下所示。

 

[root@fastdfs fdfs]# vim /etc/sysconfig/iptables  

# Firewall configuration written by system-config-firewall  

# Manual customization of this file is not recommended.  

*filter  

:INPUT ACCEPT [0:0]  

:FORWARD ACCEPT [0:0]  

:OUTPUT ACCEPT [0:0]  

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  

-A INPUT -p icmp -j ACCEPT  

-A INPUT -i lo -j ACCEPT  

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT  

-A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT  

-A INPUT -j REJECT --reject-with icmp-host-prohibited  

-A FORWARD -j REJECT --reject-with icmp-host-prohibited  

COMMIT 

 

添加完之后,重启防火墙,如下所示。

[root@fastdfs fdfs]# service iptables restart  

iptables:将链设置为政策 ACCEPT:filter           [确定]  

iptables:清除防火墙规则:                                 [确定]  

iptables:正在卸载模块:                                    [确定]  

iptables:应用防火墙规则:                                 [确定]  

 

6.5   在启动storage之前,/fastdbf/storage目录下是没有任何文件的

 

 [root@fastdfs fdfs]# cd /fastdfs/storage/ && ll  

总用量 0 

启动storage,启动后再看/fastdfs/storage目录,可以看到多了data和logs。

[root@fastdfs storage]# /etc/init.d/fdfs_storaged start  

Starting FastDFS storage server:   

[root@fastdfs storage]# cd /fastdfs/storage/ && ll  

总用量 8  

drwxr-xr-x. 68 root root 4096 4月  11 05:21 data  

drwxr-xr-x.  2 root root 4096 4月  11 05:20 logs  

 

6.6   查看FastDFS tracker和storage 是否启动成功,当看到如下所示信息时说明都启动成功了。

 

[root@fastdfs storage]# ps -ef | grep fdfs  

root       2124      1  0 05:01 ?        00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf  

root       2228      1  0 05:21 ?        00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf  

root       2238   1360  0 05:22 pts/0    00:00:00 grep fdfs  

 

6.7   我们进入到 /fastdfs/storage/data/目录下,可以看到两级共256*256个目录,每级都是从00到FF,如下只是列出了第一级的目录,点进去每个目录都还有00到FF共256个目录。

 

[root@fastdfs storage]# cd /fastdfs/storage/data/ && ls  

00  09  12  1B  24  2D  36  3F  48  51  5A  63  6C  75  7E  87  90  99  A2  AB  B4  BD  C6  CF  D8  E1  EA  F3  FC  

01  0A  13  1C  25  2E  37  40  49  52  5B  64  6D  76  7F  88  91  9A  A3  AC  B5  BE  C7  D0  D9  E2  EB  F4  FD  

02  0B  14  1D  26  2F  38  41  4A  53  5C  65  6E  77  80  89  92  9B  A4  AD  B6  BF  C8  D1  DA  E3  EC  F5  fdfs_storaged.pid  

03  0C  15  1E  27  30  39  42  4B  54  5D  66  6F  78  81  8A  93  9C  A5  AE  B7  C0  C9  D2  DB  E4  ED  F6  FE  

04  0D  16  1F  28  31  3A  43  4C  55  5E  67  70  79  82  8B  94  9D  A6  AF  B8  C1  CA  D3  DC  E5  EE  F7  FF  

05  0E  17  20  29  32  3B  44  4D  56  5F  68  71  7A  83  8C  95  9E  A7  B0  B9  C2  CB  D4  DD  E6  EF  F8  storage_stat.dat  

06  0F  18  21  2A  33  3C  45  4E  57  60  69  72  7B  84  8D  96  9F  A8  B1  BA  C3  CC  D5  DE  E7  F0  F9  sync  

07  10  19  22  2B  34  3D  46  4F  58  61  6A  73  7C  85  8E  97  A0  A9  B2  BB  C4  CD  D6  DF  E8  F1  FA  

08  11  1A  23  2C  35  3E  47  50  59  62  6B  74  7D  86  8F  98  A1  AA  B3  BC  C5  CE  D7  E0  E9  F2  FB  

 

6.8  设置storage开机自启动,添加一行/etc/init.d/fdfs_storaged start,如下所示。

 

[root@fastdfs data]# vim /etc/rc.local   

#!/bin/sh  

#  

# This script will be executed *after* all the other init scripts.  

# You can put your own initialization stuff in here if you don't  

# want to do the full Sys V style init stuff.  

touch /var/lock/subsys/local  

/etc/init.d/fdfs_trackerd start  

/etc/init.d/fdfs_storaged start  

 

7, 测试图片上传

7.1  进入到/etc/fdfs目录下并复制一份client.conf.sample并更名为client.conf,如下所示。

 

[root@fastdfs data]# cd /etc/fdfs  

[root@fastdfs fdfs]# ll  

总用量 36  

-rw-r--r--. 1 root root 1461 4月  11 04:10 client.conf.sample  

-rw-r--r--. 1 root root 7820 4月  11 05:12 storage.conf  

-rw-r--r--. 1 root root 7829 4月  11 04:10 storage.conf.sample  

-rw-r--r--. 1 root root 7098 4月  11 04:45 tracker.conf  

-rw-r--r--. 1 root root 7102 4月  11 04:10 tracker.conf.sample  

 

[root@fastdfs fdfs]# cp client.conf.sample client.conf  

[root@fastdfs fdfs]# ll  

总用量 40  

-rw-r--r--. 1 root root 1461 4月  11 05:40 client.conf  

-rw-r--r--. 1 root root 1461 4月  11 04:10 client.conf.sample  

-rw-r--r--. 1 root root 7820 4月  11 05:12 storage.conf  

-rw-r--r--. 1 root root 7829 4月  11 04:10 storage.conf.sample  

-rw-r--r--. 1 root root 7098 4月  11 04:45 tracker.conf  

-rw-r--r--. 1 root root 7102 4月  11 04:10 tracker.conf.sample  

 

7.2   使用命令vim /etc/fdfs/client.conf进入编辑模式并修改如下两项内容,如下所示。

 

base_path=/fastdfs/tracker  

tracker_server=192.168.156.13:22122  

 

7. 3  我们找到命令的脚本位置,并且使用命令,进行文件的上传。

 

[root@fastdfs bin]# cd /usr/bin/ && ls | grep fdfs  

fdfs_appender_test  

fdfs_appender_test1  

fdfs_append_file  

fdfs_crc32  

fdfs_delete_file  

fdfs_download_file  

fdfs_file_info  

fdfs_monitor  

fdfs_storaged  

fdfs_test  

fdfs_test1  

fdfs_trackerd  

fdfs_upload_appender  

fdfs_upload_file 

 

下面使用fdfs_upload_file脚本进行文件上传操作,如下所示。可以看到已经上传成功了,返回的是图片的保存位置:group1/M00/00/00/wKicDVjr_ayAE4VVAAHk-VzqZ6w020.jpg

[root@fastdfs bin]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf  /usr/local/software/3.jpg  

group1/M00/00/00/wKicDVjr_ayAE4VVAAHk-VzqZ6w020.jpg  

 

8,   FastDFS与nginx相结合

8.1  安装fastdfs-nginxmodule_v1.16.tar.gz(fast与nginx相结合的模块安装包), 进入 /usr/local/software目录并解压,如下所示。

 

[root@fastdfs nginx-1.6.2]# cd /usr/local/software/  

[root@fastdfs software]# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/  

fastdfs-nginx-module/  

fastdfs-nginx-module/src/  

fastdfs-nginx-module/src/ngx_http_fastdfs_module.c  

fastdfs-nginx-module/src/mod_fastdfs.conf  

fastdfs-nginx-module/src/config  

fastdfs-nginx-module/src/common.h  

fastdfs-nginx-module/src/common.c  

fastdfs-nginx-module/INSTALL  

fastdfs-nginx-module/HISTORY 

 

8.2   进入到/usr/local/fast目录下可以看到解压的fastdfs-nginx-module目录,然后进入到fastdfs-nginx-module/src/目录下,可以看到config文件。

[root@fastdfs software]# cd /usr/local/fast/  

[root@fastdfs fast]# ll  

总用量 12  

drwxr-xr-x. 10 8980 users 4096 4月  11 04:10 FastDFS  

drwxrwxr-x.  3  500   500 4096 5月   4 2014 fastdfs-nginx-module  

drwxr-xr-x.  4 root root  4096 4月  11 03:30 libfastcommon-master  

 [root@fastdfs fast]# cd fastdfs-nginx-module/src/  

[root@fastdfs src]# ll  

总用量 76  

-rw-rw-r--. 1 500 500 33207 8月  30 2013 common.c  

-rw-rw-r--. 1 500 500  3479 1月   3 2012 common.h  

-rw-rw-r--. 1 500 500   447 11月  4 2010 config  

-rw-rw-r--. 1 500 500  3679 3月  30 2013 mod_fastdfs.conf  

-rw-rw-r--. 1 500 500 28542 5月   4 2014 ngx_http_fastdfs_module.c

 

 修改该conf文件,我们把文件的第四行配置中的/usr/local/include都改为/usr/include,共两处。

 

 

 

8.3   安装NGINX  步骤省略,在安装NGINX时候加上模块     

[root@fastdfs nginx-1.6.2]#   ./configure --add-module=/usr/local/fast/fastdfs-nginx-module/src/

[root@fastdfs nginx-1.6.2]# make && make install 

 

8.4   复制fastdfs-nginx-module中的配置文件,到/etc/fdfs目录中,如下所示。

[root@fastdfs fdfs]# cd /usr/local/fast/fastdfs-nginx-module/src/  

[root@fastdfs src]# ll  

-rw-rw-r--. 1 500 500 33207 8月  30 2013 common.c  

-rw-rw-r--. 1 500 500  3479 1月   3 2012 common.h  

-rw-rw-r--. 1 500 500   435 4月  11 06:09 config  

-rw-rw-r--. 1 500 500  3679 3月  30 2013 mod_fastdfs.conf  

-rw-rw-r--. 1 500 500 28542 5月   4 2014 ngx_http_fastdfs_module.c  

[root@fastdfs src]# cp /usr/local/fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ 

 

 我们到 /etc/fdfs/ 目录下,修改我们刚copy过来的mod_fastdfs.conf 文件,需要修改的项如下,其中第一项是超时时长,第三项是允许外界通过http方式访问资源。

 

connect_timeout=10  

tracker_server=192.168.156.13:22122  

url_have_group_name = true  

store_path0=/fastdfs/storage 

 

 复制FastDFS里的2个文件,到/etc/fdfs目录中,如下所示。

[root@fastdfs fdfs]# cd /usr/local/fast/FastDFS/conf/  

[root@fastdfs conf]# ll  

总用量 84  

-rw-r--r--. 1 8980 users 23981 12月  2 2014 anti-steal.jpg  

-rw-r--r--. 1 8980 users  1461 12月  2 2014 client.conf  

-rw-r--r--. 1 8980 users   858 12月  2 2014 http.conf  

-rw-r--r--. 1 8980 users 31172 12月  2 2014 mime.types  

-rw-r--r--. 1 8980 users  7829 12月  2 2014 storage.conf  

-rw-r--r--. 1 8980 users   105 12月  2 2014 storage_ids.conf  

-rw-r--r--. 1 8980 users  7102 12月  2 2014 tracker.conf  

[root@fastdfs conf]# cp http.conf mime.types /etc/fdfs/  

 

创建一个软连接,在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据 的目录,如下所示。

[root@fastdfs conf]# ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00  

 

进入到/usr/local/nginx/conf/目录下,修改nginx.conf文件,如下图所示。

 

修改的内容如下图示

 

 

可以直接复制下面的内容。

listen       8888;  

location ~/group([0-9])/M00 {   

    ngx_fastdfs_module;  

}  

 

设置nginx开机自启动,这样下次重启设备之后,tracker、storage、nginx都自动启动了,直接就可以使用服务,如下所示。

 

 [root@fastdfs ~]# vim /etc/rc.d/rc.local   

#!/bin/sh  

#  

# This script will be executed *after* all the other init scripts.  

# You can put your own initialization stuff in here if you don't  

# want to do the full Sys V style init stuff.  

touch /var/lock/subsys/local  

/etc/init.d/fdfs_trackerd start  

/etc/init.d/fdfs_storaged start  

/usr/local/nginx/sbin/nginx

 

启动nginx,如下所示。

 [root@fastdfs conf]# /usr/local/nginx/sbin/nginx   

ngx_http_fastdfs_set pid=6809  

 

8.5   在通过8888端口访问图片之前先配置下防火墙,允许外界访问8888端口,添加的一行是-A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT,如下图所示。

 

 

 

配置完防火墙后重启防火墙

 

[root@fastdfs conf]# service iptables restart  

iptables:将链设置为政策 ACCEPT:filter            [确定]  

iptables:清除防火墙规则:                                 [确定]  

iptables:正在卸载模块:                                   [确定]  

iptables:应用防火墙规则:                                 [确定]  

 

9 测试项目成果

现在我们便可以通过http的方式访问我们刚才上传的图片了(我们刚才上传图片返回的地址是group1/M00/00/00/wKicDVjr_ayAE4VVAAHk-VzqZ6w020.jpg),如下图所示。

现在我们便可以通过http的方式访问我们刚才上传的图片了(我们刚才上传图片返回的地址是group1/M00/00/00/wKicDVjr_ayAE4VVAAHk-VzqZ6w020.jpg),如下图所示。

 

 

 10,生产中遇到的问题

例如NGINX的转发问题:

在http中定义upsream模块:

upstream file_server {
server 192.168.156.13:8888 max_fails=2 fail_timeout=30s;
}

 

在server中定义location 模块   (单独监听端口,例如8888)

listen 8888

location ~/group([0-9])/M00 {

ngx_fastdfs_module;
}

location /image/ {
proxy_pass http://file_server/group1/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

 @1  通常会遇到怎么讲上传的图片地址改为~~/image/

则必须要加上面的location模块以及upstream模块,还要修改 

#  vim /etc/fdfs/storage.conf

group_name=image     默认是group1

 

@2 如果不修改图片链接地址的话,那么只修改下面的代码就可以了  

listen       8888;  

location ~/group([0-9])/M00 {   

    ngx_fastdfs_module;  

}  

 
 
测试成功!生产当中单机搭建fastdfs成功!  感谢参考的那些文献!

推荐阅读