什麼是FastDFS?
FastDFS是一個開源的分佈式文件系統,她對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題。特別適合以文件爲載體的在線服務,如相冊網站、視頻網站等等。
FastDFS是一個應用級分佈式文件存儲服務,其採用中心型結構(類似GFS、HDFS、TFS等),主要用於大中型網站存儲資源文件。FastDFS具有輕量級,支持高併發放訪問,負載均衡,可擴展等優點。而FastDFS最大的亮點就是對小文件的存儲性能較好,這主要來自於其文件名策略。
1.小文件存儲性能優化
小文件的性能瓶頸主要來自於對元數據服務器(如FastDFS中的TrackerServer或TFS中的NameServer)的訪問,因爲當文件本身大小很小時,元數據存儲所佔空間與文件內容存儲所佔空間的比例就變得較大,訪問元數據所消耗資源與訪問文件內容所消耗資源的比例也變得較大。因此,通常對小文件存儲的優化方法主要有兩大類思路:一是減少訪問元數據的次數,比如Cache預取;二是減少元數據所佔的存儲空間,比如FastDFS使用的文件名策略。
2. FastDFS文件名策略
FastDFS中的文件名是在向StorageServer存儲文件時由系統指定的,文件名中包含了VolumeID和FileID。也就是說,當客戶要讀取某個文件時,通過在客戶端對文件名進行解析,就可以知道該文件存儲在哪個Volume上和它在StorageServer中的FileID。但是此時用戶還不能讀取文件,因爲他不知道Volume內各個StorageServer的ip地址,也不知道應該從Volume內的哪個StorageServer中讀取。所以用戶需手持欲訪問的文件的VolumeID向TrackerServer詢問,TrackerServe會均衡當前各StorageServer的IO負載狀況,返回一個最佳的StorageServer的ip地址。最後用戶與該StorageServer連接,出示欲訪問文件的FileID,StorageServer上會維持一個FileID對應偏移量的表,從而得到欲訪問文件的偏移量。
可見,FastDFS的文件名策略將文件存儲位置信息隱含在文件名中,從而減少了元數據量,達到了優化小文件存儲性能的作用。
FastDFS服務端有兩個角色:跟蹤器(tracker)和存儲節點(storage)。跟蹤器主要做調度工作,在訪問上起負載均衡的作用。
上傳交互過程編輯
-
client詢問tracker上傳到的storage,不需要附加參數;
-
tracker返回一臺可用的storage;
-
client直接和storage通訊完成文件上傳。
FastDFS file download
下載交互過程編輯
-
client詢問tracker下載文件的storage,參數爲文件標識(卷名和文件名);
-
tracker返回一臺可用的storage;
-
client直接和storage通訊完成文件下載。
需要說明的是,client爲使用FastDFS服務的調用方,client也應該是一臺服務器,它對tracker和storage的調用均爲服務器間的調用。
存儲節點存儲文件,完成文件管理的所有功能:存儲、同步和提供存取接口,FastDFS同時對文件的meta data進行管理。所謂文件的meta data就是文件的相關屬性,以鍵值對(key value pair)方式表示,如:width=1024,其中的key爲width,value爲1024。文件meta data是文件屬性列表,可以包含多個鍵值對。
FastDFS系統結構如下圖所示:
準備好四個文件
下載地址:
https://github.com/happyfish100
CentOS 7 FastDFS搭建
安裝libfastcommon
上傳到服務器,解壓:
[root@localhost home]# unzip libfastcommon-master.zip
進入libfastcommon-1.0.36目錄:
[root@localhost home]# cd libfastcommon-master/
[root@localhost libfastcommon-master]# ll
total 32
drwxr-xr-x. 2 root root 114 Dec 6 11:49 doc
-rw-r--r--. 1 root root 10179 Dec 6 11:49 HISTORY
-rw-r--r--. 1 root root 674 Dec 6 11:49 INSTALL
-rw-r--r--. 1 root root 1607 Dec 6 11:49 libfastcommon.spec
-rwxr-xr-x. 1 root root 3253 Dec 6 11:49 make.sh
drwxr-xr-x. 2 root root 191 Dec 6 11:49 php-fastcommon
-rw-r--r--. 1 root root 2776 Dec 6 11:49 README
drwxr-xr-x. 3 root root 4096 Dec 6 11:49 src
[root@localhost libfastcommon-master]#
用yum安裝gcc:
Yum命令相當好用,是RedHad和CentOS從指定服務器下載RPM包並自動安裝。我個人比較喜歡。
[root@localhost libfastcommon-master]# yum -y install gcc-c++
Complete! 執行完成了!
這個時候分別執行./make.sh和./make.sh install,正常情況是可以成功的。
[root@localhost libfastcommon-master]# ./make.sh
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o hash.o hash.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o chain.o chain.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o shared_func.o shared_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o ini_file_reader.o ini_file_reader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o logger.o logger.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o sockopt.o sockopt.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o base64.o base64.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o sched_thread.o sched_thread.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o http_func.o http_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o md5.o md5.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o pthread_func.o pthread_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o local_ip_func.o local_ip_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o avl_tree.o avl_tree.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o ioevent.o ioevent.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o ioevent_loop.o ioevent_loop.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_task_queue.o fast_task_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_timer.o fast_timer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o process_ctrl.o process_ctrl.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_mblock.o fast_mblock.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o connection_pool.o connection_pool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_mpool.o fast_mpool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_allocator.o fast_allocator.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_buffer.o fast_buffer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o multi_skiplist.o multi_skiplist.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o flat_skiplist.o flat_skiplist.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o system_info.o system_info.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o fast_blocked_queue.o fast_blocked_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o id_generator.o id_generator.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o char_converter.o char_converter.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o char_convert_loader.o char_convert_loader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o common_blocked_queue.o common_blocked_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o multi_socket_client.o multi_socket_client.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o skiplist_set.o skiplist_set.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o json_parser.o json_parser.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o hash.lo hash.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o chain.lo chain.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o shared_func.lo shared_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o ini_file_reader.lo ini_file_reader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o logger.lo logger.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o sockopt.lo sockopt.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o base64.lo base64.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o sched_thread.lo sched_thread.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o http_func.lo http_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o md5.lo md5.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o pthread_func.lo pthread_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o local_ip_func.lo local_ip_func.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o avl_tree.lo avl_tree.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o ioevent.lo ioevent.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o ioevent_loop.lo ioevent_loop.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_task_queue.lo fast_task_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_timer.lo fast_timer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o process_ctrl.lo process_ctrl.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_mblock.lo fast_mblock.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o connection_pool.lo connection_pool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_mpool.lo fast_mpool.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_allocator.lo fast_allocator.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_buffer.lo fast_buffer.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o multi_skiplist.lo multi_skiplist.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o flat_skiplist.lo flat_skiplist.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o system_info.lo system_info.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o fast_blocked_queue.lo fast_blocked_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o id_generator.lo id_generator.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o char_converter.lo char_converter.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o char_convert_loader.lo char_convert_loader.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o common_blocked_queue.lo common_blocked_queue.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o multi_socket_client.lo multi_socket_client.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o skiplist_set.lo skiplist_set.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -fPIC -o json_parser.lo json_parser.c
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -o libfastcommon.so -shared hash.lo chain.lo shared_func.lo ini_file_reader.lo logger.lo sockopt.lo base64.lo sched_thread.lo http_func.lo md5.lo pthread_func.lo local_ip_func.lo avl_tree.lo ioevent.lo ioevent_loop.lo fast_task_queue.lo fast_timer.lo process_ctrl.lo fast_mblock.lo connection_pool.lo fast_mpool.lo fast_allocator.lo fast_buffer.lo multi_skiplist.lo flat_skiplist.lo system_info.lo fast_blocked_queue.lo id_generator.lo char_converter.lo char_convert_loader.lo common_blocked_queue.lo multi_socket_client.lo skiplist_set.lo json_parser.lo -lm -ldl -lpthread
ar rcs libfastcommon.a hash.o chain.o shared_func.o ini_file_reader.o logger.o sockopt.o base64.o sched_thread.o http_func.o md5.o pthread_func.o local_ip_func.o avl_tree.o ioevent.o ioevent_loop.o fast_task_queue.o fast_timer.o process_ctrl.o fast_mblock.o connection_pool.o fast_mpool.o fast_allocator.o fast_buffer.o multi_skiplist.o flat_skiplist.o system_info.o fast_blocked_queue.o id_generator.o char_converter.o char_convert_loader.o common_blocked_queue.o multi_socket_client.o skiplist_set.o json_parser.o
[root@localhost libfastcommon-master]# ./make.sh install
mkdir -p /usr/lib64
mkdir -p /usr/lib
mkdir -p /usr/include/fastcommon
install -m 755 libfastcommon.so /usr/lib64
install -m 644 common_define.h hash.h chain.h logger.h base64.h shared_func.h pthread_func.h ini_file_reader.h _os_define.h sockopt.h sched_thread.h http_func.h md5.h local_ip_func.h avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h fast_timer.h process_ctrl.h fast_mblock.h connection_pool.h fast_mpool.h fast_allocator.h fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h skiplist_common.h system_info.h fast_blocked_queue.h php7_ext_wrapper.h id_generator.h char_converter.h char_convert_loader.h common_blocked_queue.h multi_socket_client.h skiplist_set.h fc_list.h json_parser.h /usr/include/fastcommon
if [ ! -e /usr/lib/libfastcommon.so ]; then ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so; fi
[root@localhost libfastcommon-master]#
libfastcommon默認會被安裝到/usr/lib64/libfastcommon.so但是FastDFS的主程序卻在/usr/local/lib目錄下
這個時候我們就要建立一個軟鏈接了,實際上也相當於windows上的快捷方式。
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln: failed to create symbolic link ‘/usr/lib/libfastcommon.so’: File exists
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
[root@localhost libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
[root@localhost libfastcommon-master]#
安裝FastDFS
解壓FastDFS安裝包
[root@localhost home]# unzip fastdfs-master.zip
解壓後看到:
[root@localhost home]# ll
total 2144
drwx------. 2 dev1 dev1 62 Apr 11 2018 dev1
drwxr-xr-x. 12 root root 4096 Dec 8 10:17 fastdfs-master
-rw-r--r--. 1 root root 905173 Dec 10 20:24 fastdfs-master.zip
-rw-r--r--. 1 root root 22492 Dec 10 20:24 fastdfs-nginx-module-master.zip
drwxr-xr-x. 5 root root 153 Dec 10 20:37 libfastcommon-master
-rw-r--r--. 1 root root 218881 Dec 10 20:24 libfastcommon-master.zip
-rw-r--r--. 1 root root 1037527 Dec 10 20:24 nginx-1.17.6.tar.gz
進到剛解壓的目錄
[root@localhost home]# cd fastdfs-master/
[root@localhost fastdfs-master]# ./make.sh
[root@localhost fastdfs-master]# ./make.sh install
如果沒有報錯那麼就成功了。安裝log中會提示FastDFS安裝到了/etc/fdfs目錄下。
成功後查看安裝目錄:
[root@localhost fastdfs-master]# cd /etc/fdfs/
[root@localhost fdfs]# ll
total 28
-rw-r--r--. 1 root root 1909 Dec 10 20:40 client.conf.sample
-rw-r--r--. 1 root root 10246 Dec 10 20:40 storage.conf.sample
-rw-r--r--. 1 root root 620 Dec 10 20:40 storage_ids.conf.sample
-rw-r--r--. 1 root root 8128 Dec 10 20:40 tracker.conf.sample
[root@localhost fdfs]#
我們需要把這三個示例文件複製一份,去掉.sample。
[root@localhost fdfs]# cp client.conf.sample client.conf
[root@localhost fdfs]# cp storage.conf.sample storage.conf
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf
[root@localhost fdfs]# ll
total 52
-rw-r--r--. 1 root root 1909 Dec 10 20:40 client.conf
-rw-r--r--. 1 root root 1909 Dec 10 20:40 client.conf.sample
-rw-r--r--. 1 root root 10246 Dec 10 20:40 storage.conf
-rw-r--r--. 1 root root 10246 Dec 10 20:40 storage.conf.sample
-rw-r--r--. 1 root root 620 Dec 10 20:40 storage_ids.conf.sample
-rw-r--r--. 1 root root 8128 Dec 10 20:40 tracker.conf
-rw-r--r--. 1 root root 8128 Dec 10 20:40 tracker.conf.sample
[root@localhost fdfs]#
FastDFS安裝結束。
安裝tracker
創建tracker工作目錄
這個目錄可以自定義,用來保存tracker的data和log
根據個人習慣,我創建了下面的目錄:
[root@localhost ~]# mkdir /data
[root@localhost ~]# cd /data/
[root@localhost data]# mkdir fastdfs
[root@localhost data]# cd fastdfs/
[root@localhost fastdfs]# mkdir fastdfs_tracker
[root@localhost fastdfs]# cd fastdfs_tracker/
[root@localhost fastdfs_tracker]# pwd
/data/fastdfs/fastdfs_tracker
[root@localhost fastdfs_tracker]#
配置tracker
[root@localhost fastdfs-5.11]# cd /etc/fdfs/
[root@localhost fastdfs-5.11]# vim tracker.conf
[root@localhost fastdfs-5.11]#
最小化的CentOS7是沒有安裝vim的,可以把vim tracker.conf命令改成vi tracker.conf,也可以去下載一個vim
yum -y install vim
打開後重點關注下面4個配置:
disabled = false
port=22122
base_path=/data/fastdfs/fastdfs_tracker
http.server_port=6666 # 默認8080 9901
啓動tracker
保存配置後啓動tracker,命令如下:
service fdfs_trackerd start
如果不能啓動,或提示用systemctl可改用命令:
systemctl start fdfs_trackerd
成功後應該可以看到:
[root@localhost fdfs]# service fdfs_trackerd start
Reloading systemd: [ OK ]
Starting fdfs_trackerd (via systemctl): [ OK ]
[root@localhost fdfs]#
進行剛剛創建的tracker目錄,發現目錄中多了data和log兩個目錄
[root@localhost fdfs]# cd /data/fastdfs/fastdfs_tracker/
[root@localhost fastdfs_tracker]# ll
total 0
drwxr-xr-x. 2 root root 83 Dec 10 20:45 data
drwxr-xr-x. 2 root root 26 Dec 10 20:44 logs
[root@localhost fastdfs_tracker]#
最後我們需要給tracker加入開機啓動
[root@localhost fastdfs_tracker]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 Feb 20 2019 /etc/rc.d/rc.local
[root@localhost fastdfs_tracker]#
發現並沒有執行權限,需要加一下:
[root@localhost fastdfs_tracker]# chmod +x /etc/rc.d/rc.local
[root@localhost fastdfs_tracker]#
加完後應該是這樣的:
[root@localhost fastdfs_tracker]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Feb 20 2019 /etc/rc.d/rc.local
[root@localhost fastdfs_tracker]#
修改rc.local
[root@localhost fastdfs_tracker]# vim /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
service fdfs_trackerd start
查看一下tracker的端口監聽情況
[root@localhost fastdfs_tracker]# netstat -unltp|grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 20553/fdfs_trackerd
[root@localhost fastdfs_tracker]#
端口22122成功監聽。
安裝storage
storage的安裝與tracker很類似。
爲storage配置工作目錄
與tracker不現的是,由於storage還需要一個目錄用來存儲數據,所以我另外多建了一個fasdfs_storage_data
下面是我的目錄結構:
[root@localhost fastdfs]# cd /data/fastdfs
[root@localhost fastdfs]# mkdir fastdfs_storage
[root@localhost fastdfs]# mkdir fastdfs_storage_data
[root@localhost fastdfs]# ll
total 0
drwxr-xr-x. 2 root root 6 Dec 10 20:43 fastdfs_storage
drwxr-xr-x. 2 root root 6 Dec 10 20:43 fastdfs_storage_data
drwxr-xr-x. 4 root root 30 Dec 10 20:42 fastdfs_tracker
[root@localhost fastdfs]#
修改storage配置文件
修改storage.conf
[root@localhost fastdfs]# vim /etc/fdfs/storage.conf
disabled=false
group_name=group1 #組名,根據實際情況修改
port=23000 #設置storage的端口號,默認是23000,同一個組的storage端口號必須一致
base_path=/data/fastdfs/fastdfs_storage #設置storage數據文件和日誌目錄
store_path_count=1 #存儲路徑個數,需要和store_path個數匹配
》》》》》store_path0=/data/fastdfs/fastdfs_storage_data #實際文件存儲路徑
》》》》》base_path0=/data/fastdfs/fastdfs_storage_data #實際文件存儲路徑
tracker_server=192.168.31.100:22122 #我CentOS7的ip地址
http.server_port=8888 #設置 http 端口號
[root@localhost fastdfs]#
修改保存後創建軟引用
[root@localhost fastdfs]# ln -s /usr/bin/fdfs_storaged /usr/local/bin
[root@localhost fastdfs]#
啓動storage
service fdfs_storaged start
如果不能啓動,或提示用systemctl可改用命令:
systemctl start fdfs_storaged
成功後應該可以看到:
[root@localhost fastdfs]# service fdfs_storaged start
Starting fdfs_storaged (via systemctl): [ OK ]
[root@localhost fastdfs]#
同樣的,設置開機啓動:
修改rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
service fdfs_trackerd start
service fdfs_storaged start
查看一下服務是否啓動
[root@localhost fastdfs]# netstat -unltp | grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 20553/fdfs_trackerd
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 20817/fdfs_storaged
[root@localhost fastdfs]#
校驗整合
到這裏,fastdfs的東西都已安裝完成,最後我們還要確定一下,storage是否註冊到了tracker中去。
查看命令:
[root@localhost fastdfs]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
[2019-12-10 21:04:31] DEBUG - base_path=/data/fastdfs/fastdfs_storage, connect_timeout=5, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=1, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
server_count=1, server_index=0
tracker server is 192.168.31.100:22122
group count: 1
Group 1:
group name = group1
disk total space = 51175 MB
disk free space = 48527 MB
trunk free space = 0 MB
storage server count = 1
active server count = 1
storage server port = 23000
storage HTTP port = 8888
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0
Storage 1:
id = 10.254.193.118
ip_addr = 10.254.193.118 (anantes-651-1-49-net.w2-0.abo.wanadoo.fr) ACTIVE
http domain =
version = 6.04
join time = 2019-12-10 21:01:47
up time = 2019-12-10 21:01:47
total storage = 51175 MB
free storage = 48527 MB
upload priority = 10
store_path_count = 1
subdir_count_per_path = 256
storage_port = 23000
storage_http_port = 8888
current_write_path = 0
source storage id =
if_trunk_server = 0
connection.alloc_count = 256
connection.current_count = 0
connection.max_count = 0
total_upload_count = 0
success_upload_count = 0
total_append_count = 0
success_append_count = 0
total_modify_count = 0
success_modify_count = 0
total_truncate_count = 0
success_truncate_count = 0
total_set_meta_count = 0
success_set_meta_count = 0
total_delete_count = 0
success_delete_count = 0
total_download_count = 0
success_download_count = 0
total_get_meta_count = 0
success_get_meta_count = 0
total_create_link_count = 0
success_create_link_count = 0
total_delete_link_count = 0
success_delete_link_count = 0
total_upload_bytes = 0
success_upload_bytes = 0
total_append_bytes = 0
success_append_bytes = 0
total_modify_bytes = 0
success_modify_bytes = 0
stotal_download_bytes = 0
success_download_bytes = 0
total_sync_in_bytes = 0
success_sync_in_bytes = 0
total_sync_out_bytes = 0
success_sync_out_bytes = 0
total_file_open_count = 0
success_file_open_count = 0
total_file_read_count = 0
success_file_read_count = 0
total_file_write_count = 0
success_file_write_count = 0
last_heart_beat_time = 2019-12-10 21:04:20
last_source_update = 1970-01-01 08:00:00
last_sync_update = 1970-01-01 08:00:00
last_synced_timestamp = 1970-01-01 08:00:00
[root@localhost fastdfs]#
測試
前面已對FastDFS的安裝和配置,做了比較詳細的講解。FastDFS的基礎模塊都搭好了,現在開始測試下載。
配置客戶端
同樣的,需要修改客戶端的配置文件:
[root@localhost fastdfs]# vim /etc/fdfs/client.conf
base_path = /data/fastdfs/fastdfs_tracker
tracker_server = 192.168.31.100:22122
http.tracker_server_port = 6666 # 默認端口80
通過ftp上傳圖片到CentOS:
在我的windows上,我隨便拖了一張圖片上去。
[root@localhost fastdfs]# cd /home/
[root@localhost home]# ll
total 1148
-rw-r--r--. 1 root root 18832 Oct 12 12:58 123.png
drwx------. 2 dev1 dev1 62 Apr 11 2018 dev1
drwxr-xr-x. 12 root root 4096 Dec 8 10:17 fastdfs-5.11
-rw-r--r--. 1 root root 905173 Dec 10 11:38 fastdfs-5.11.zip
-rw-r--r--. 1 root root 22492 Dec 10 11:38 fastdfs-nginx-module-master.zip
drwxr-xr-x. 5 root root 153 Dec 10 19:48 libfastcommon-1.0.36
-rw-r--r--. 1 root root 218881 Dec 10 11:36 libfastcommon-1.0.36.zip
[root@localhost home]#
模擬上傳
確定圖片位置後,我們輸入上傳圖片命令:
[root@localhost home]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/123.jpg
group1/M00/00/00/wKgfZF3vl6WAJDW5AAvWFlS1kOw230.jpg
[root@localhost home]#
成功後會返回圖片的路徑:
[root@localhost~]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/123.png
group1/M00/00/00/wKgfZF3vl6WAJDW5AAvWFlS1kOw230.jpg
組名:group1
磁盤:M00
目錄:00/00
文件名稱:wKgfZF3vl6WAJDW5AAvWFlS1kOw230.png
我們上傳的圖片會被上傳到我們創建的storage_data目錄下,讓我們去看看:
[root@localhost fastdfs_storage_data]# cd /data/fastdfs/fastdfs_storage_data/data/
[root@localhost data]# ls
00 06 0C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A 60 66 6C 72 78 7E 84 8A 90 96 9C A2 A8 AE B4 BA C0 C6 CC D2 D8 DE E4 EA F0 F6 FC
01 07 0D 13 19 1F 25 2B 31 37 3D 43 49 4F 55 5B 61 67 6D 73 79 7F 85 8B 91 97 9D A3 A9 AF B5 BB C1 C7 CD D3 D9 DF E5 EB F1 F7 FD
02 08 0E 14 1A 20 26 2C 32 38 3E 44 4A 50 56 5C 62 68 6E 74 7A 80 86 8C 92 98 9E A4 AA B0 B6 BC C2 C8 CE D4 DA E0 E6 EC F2 F8 FE
03 09 0F 15 1B 21 27 2D 33 39 3F 45 4B 51 57 5D 63 69 6F 75 7B 81 87 8D 93 99 9F A5 AB B1 B7 BD C3 C9 CF D5 DB E1 E7 ED F3 F9 FF
04 0A 10 16 1C 22 28 2E 34 3A 40 46 4C 52 58 5E 64 6A 70 76 7C 82 88 8E 94 9A A0 A6 AC B2 B8 BE C4 CA D0 D6 DC E2 E8 EE F4 FA
05 0B 11 17 1D 23 29 2F 35 3B 41 47 4D 53 59 5F 65 6B 71 77 7D 83 89 8F 95 9B A1 A7 AD B3 B9 BF C5 CB D1 D7 DD E3 E9 EF F5 FB
[root@localhost data]# cd 00/
[root@localhost 00]# ls
00 06 0C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A 60 66 6C 72 78 7E 84 8A 90 96 9C A2 A8 AE B4 BA C0 C6 CC D2 D8 DE E4 EA F0 F6 FC
01 07 0D 13 19 1F 25 2B 31 37 3D 43 49 4F 55 5B 61 67 6D 73 79 7F 85 8B 91 97 9D A3 A9 AF B5 BB C1 C7 CD D3 D9 DF E5 EB F1 F7 FD
02 08 0E 14 1A 20 26 2C 32 38 3E 44 4A 50 56 5C 62 68 6E 74 7A 80 86 8C 92 98 9E A4 AA B0 B6 BC C2 C8 CE D4 DA E0 E6 EC F2 F8 FE
03 09 0F 15 1B 21 27 2D 33 39 3F 45 4B 51 57 5D 63 69 6F 75 7B 81 87 8D 93 99 9F A5 AB B1 B7 BD C3 C9 CF D5 DB E1 E7 ED F3 F9 FF
04 0A 10 16 1C 22 28 2E 34 3A 40 46 4C 52 58 5E 64 6A 70 76 7C 82 88 8E 94 9A A0 A6 AC B2 B8 BE C4 CA D0 D6 DC E2 E8 EE F4 FA
05 0B 11 17 1D 23 29 2F 35 3B 41 47 4D 53 59 5F 65 6B 71 77 7D 83 89 8F 95 9B A1 A7 AD B3 B9 BF C5 CB D1 D7 DD E3 E9 EF F5 FB
[root@localhost 00]# cd 00
[root@localhost 00]# ls
wKgfZF3vl6WAJDW5AAvWFlS1kOw230.png
[root@localhost 00]#
果然通過剛剛返回的路徑,我們成功找到了圖片。
我們仔細看一下,實際文件存儲路徑下有創建好的多級目錄。data下有256個1級目錄,每級目錄下又有256個2級子目錄,總共65536個文件,新寫的文件會以hash的方式被路由到其中某個子目錄下,然後將文件數據直接作爲一個本地文件存儲到該目錄中。
HTTP訪問文件
我們去瀏覽器用http請求訪問一下剛剛的圖片:
這裏寫圖片描述
我們發現,http不能直接訪問到圖片。這是爲什麼呢。
我去官網看了一原碼,在HISTORY中發現,原來早在4.05的時候,就remove embed HTTP support
Version 4.05 2012-12-30
* client/fdfs_upload_file.c can specify storage ip port and store path index
* add connection pool
* client load storage ids config
* common/ini_file_reader.c does NOT call chdir
* keep the mtime of file same
* use g_current_time instead of call time function
* remove embed HTTP support
HTTP請求不能訪問文件的原因
我們在使用FastDFS部署一個分佈式文件系統的時候,通過FastDFS的客戶端API來進行文件的上傳、下載、刪除等操作。同時通過FastDFS的HTTP服務器來提供HTTP服務。但是FastDFS的HTTP服務較爲簡單,無法提供負載均衡等高性能的服務,所以FastDFS的開發者——淘寶的架構師餘慶同學,爲我們提供了Nginx上使用的FastDFS模塊(也可以叫FastDFS的Nginx模塊)。
FastDFS通過Tracker服務器,將文件放在Storage服務器存儲,但是同組之間的服務器需要複製文件,有延遲的問題.假設Tracker服務器將文件上傳到了192.168.128.131,文件ID已經返回客戶端,這時,後臺會將這個文件複製到192.168.128.131,如果複製沒有完成,客戶端就用這個ID在192.168.128.131取文件,肯定會出現錯誤。這個fastdfs-nginx-module可以重定向連接到源服務器取文件,避免客戶端由於複製延遲的問題,出現錯誤。
正是這樣,FastDFS需要結合nginx,所以取消原來對HTTP的直接支持。
FastDFS的nginx模塊安裝
安裝nginx準備
將nginx-1.17.6.tar.gz上傳至linux
[root@localhost 00]# cd /home/
[root@lml74xunyuanfuwuqi home]# ll
總用量 2164
-rw-r--r-- 1 root root 18832 10月 12 12:58 123.png
drwx------ 2 dceq dceq 62 11月 15 18:14 dceq
drwxr-xr-x 12 root root 4096 12月 8 10:17 fastdfs-master
-rw-r--r-- 1 root root 905173 12月 10 11:38 fastdfs-master.zip
-rw-r--r-- 1 root root 22492 12月 10 11:38 fastdfs-nginx-module-master.zip
drwxr-xr-x 5 root root 153 12月 11 15:07 libfastcommon-master
-rw-r--r-- 1 root root 218881 12月 10 11:36 libfastcommon-master.zip
-rw-r--r-- 1 root root 1037527 12月 10 13:38 nginx-1.17.6.tar.gz
drwxr-xr-x 3 root root 21 12月 11 09:03 work
[root@localhost home]#
在安裝nginx之前要安裝nginx所需的依賴lib:
[root@localhost home]# yum -y install pcre pcre-devel
[root@localhost home]# yum -y install zlib zlib-devel
[root@localhost home]# yum -y install openssl openssl-devel
安裝nginx並添加fastdfs-nginx-module
解壓nginx,和fastdfs-nginx-module:
[root@localhost home]# tar -zxvf nginx-1.17.6.tar.gz
[root@localhost home]# unzip fastdfs-nginx-module-master.zip
解壓後進入nginx目錄編譯安裝nginx,並添加fastdfs-nginx-module:
[root@lml74xunyuanfuwuqi home]# cd nginx-1.17.6
[root@localhost nginx-1.17.6]# ./configure --prefix=/usr/local/nginx --add-module=/home/fastdfs-nginx-module-master/src
#解壓後fastdfs-nginx-module所在的位置
如果配置不報錯的話,就開始編譯:
[root@localhost nginx-1.17.6]# make
[root@localhost nginx-1.17.6]# make install
如果報錯的話,很可能是版本的原因,在我的第二篇博文中提供了我測試成功不報錯的版本下載。
nginx的默認目錄是/usr/local/nginx,安裝成功後查看:
[root@localhost nginx-1.17.6]# cd /usr/local/nginx/
[root@localhost nginx]# ll
total 4
drwxr-xr-x. 2 root root 4096 Dec 10 21:06 conf
drwxr-xr-x. 2 root root 40 Dec 10 21:06 html
drwxr-xr-x. 2 root root 6 Dec 10 21:06 logs
drwxr-xr-x. 2 root root 19 Dec 10 21:06 sbin
[root@lml74xunyuanfuwuqi nginx]# cd conf/
[root@localhost nginx]#
[root@lml74xunyuanfuwuqi conf]# vi nginx.conf
配置storage nginx
修改nginx.conf:
修改監聽端口 listen 9999, 新增location
[root@localhost nginx]# cd conf/
[root@localhost conf]# ll
total 68
-rw-r--r--. 1 root root 1077 Dec 10 21:06 fastcgi.conf
-rw-r--r--. 1 root root 1077 Dec 10 21:06 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 Dec 10 21:06 fastcgi_params
-rw-r--r--. 1 root root 1007 Dec 10 21:06 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Dec 10 21:06 koi-utf
-rw-r--r--. 1 root root 2223 Dec 10 21:06 koi-win
-rw-r--r--. 1 root root 5231 Dec 10 21:06 mime.types
-rw-r--r--. 1 root root 5231 Dec 10 21:06 mime.types.default
-rw-r--r--. 1 root root 2656 Dec 10 21:06 nginx.conf
-rw-r--r--. 1 root root 2656 Dec 10 21:06 nginx.conf.default
-rw-r--r--. 1 root root 636 Dec 10 21:06 scgi_params
-rw-r--r--. 1 root root 636 Dec 10 21:06 scgi_params.default
-rw-r--r--. 1 root root 664 Dec 10 21:06 uwsgi_params
-rw-r--r--. 1 root root 664 Dec 10 21:06 uwsgi_params.default
-rw-r--r--. 1 root root 3610 Dec 10 21:06 win-utf
[root@localhost conf]# vi nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 9999;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ~/group1/M00 {
root /data/fastdfs/fastdfs_storage_data/data;
ngx_fastdfs_module;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
[root@localhost conf]#
[root@localhost conf]#
然後進入FastDFS安裝時的解壓過的目錄,將http.conf和mime.types拷貝到/etc/fdfs目錄下:
[root@localhost conf]# cd /home/fastdfs-master/conf/
[root@localhost conf]# ll
total 88
-rw-r--r--. 1 root root 23981 Dec 8 10:17 anti-steal.jpg
-rw-r--r--. 1 root root 1909 Dec 8 10:17 client.conf
-rw-r--r--. 1 root root 965 Dec 8 10:17 http.conf
-rw-r--r--. 1 root root 31172 Dec 8 10:17 mime.types
-rw-r--r--. 1 root root 10246 Dec 8 10:17 storage.conf
-rw-r--r--. 1 root root 620 Dec 8 10:17 storage_ids.conf
-rw-r--r--. 1 root root 8128 Dec 8 10:17 tracker.conf
[root@localhost conf]#
[root@localhost conf]# cp http.conf /etc/fdfs/
[root@localhost conf]# cp mime.types /etc/fdfs/
[root@localhost conf]#
另外還需要把fastdfs-nginx-module安裝目錄中src目錄下的mod_fastdfs.conf也拷貝到/etc/fdfs目錄下:
[root@localhost conf]# cp /home/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
[root@localhost conf]#
對剛剛拷貝的mod_fastdfs.conf文件進行修改:
[root@localhost conf]# vim /etc/fdfs/mod_fastdfs.conf
! base_path=/data/fastdfs/fastdfs_storage #保存日誌目錄
! tracker_server=192.168.31.100:22122 #tracker服務器的IP地址以及端口號
! storage_server_port=23000 #storage服務器的端口號
! url_have_group_name = true #文件 url 中是否有 group 名
! store_path0=/data/fastdfs/fastdfs_storage_data #存儲路徑
group_count = 3 #設置組的個數,事實上這次只使用了group1
在文件的最後,設置group
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/fastdfs_storage_data
store_path1=/data/fastdfs/fastdfs_storage_data
# group settings for group #2
# # since v1.14
# # when support multi-group, uncomment following section as neccessary
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/fastdfs_storage_data
[group3]
group_name=group3
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/fastdfs_storage_data
[root@localhost conf]#
創建M00至storage存儲目錄的符號連接:
[root@localhost conf]# ln -s /data/fastdfs/fastdfs_storage_data/data/ /data/fastdfs/fastdfs_storage_data/data/M00
[root@localhost conf]#
啓動nginx:
[root@localhost conf]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=31687
[root@localhost conf]#
成功啓動:
[root@localhost conf]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=31687
[root@localhost conf]#
恭喜你,storage的nginx已配置成功。接下來,我們還要繼續配置tracker的nginx。
配置tracker nginx
再解壓一個nginx:
我在我自己的工作下再建了一個nginx2,把原來的nginx-1.12.0.tar.gz又解壓了一份到裏面
[root@localhost home]# mkdir nginx2
[root@localhost home]# tar -zxvf nginx-1.17.6.tar.gz -C nginx2/
[root@localhost home]# cd nginx2/nginx-1.17.6/
[root@localhost nginx-1.17.6]# ./configure --prefix=/usr/local/nginx2 --add-module=/home/fastdfs-nginx-module-master/src
[root@localhost nginx-1.17.6]# make && make install
接下來,一樣的還是修改nginx.conf,端口號可以不改,用80的。需將upstream指向tracker的nginx地址。
[root@localhost nginx-1.17.6]# vim /usr/local/nginx2/conf/nginx.conf
upstream fdfs_group1 {
server 127.0.0.1:9999;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00 {
proxy_pass http://fdfs_group1;
}
#location / {
# root html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
啓動nginx2:
[root@localhost nginx-1.17.6]# /usr/local/nginx2/sbin/nginx
可以成功訪問。
防火牆端口設置
成功了,爲什麼還要講這個呢。因爲有些同學到這裏,還是不到訪問,很可能是防火牆沒有開啓相應的端口。防火牆這個東西我建議大家還是不要關閉,雖然麻煩了一點。
查看已開啓的端口:
[root@localhost nginx-1.13.8]# firewall-cmd --zone=public --list-ports
20880/tcp 80/tcp 2181/tcp 23000/tcp 22122/tcp 9999/tcp
[root@localhost nginx-1.13.8]#
在我的CentOS上這些端口都是開放的。
storage:20880
tracker:23000
這兩個端口要開啓,到時候下一篇講fastdfs-client-javas可能會造成無法連接。
9999和80端口是提供給nginx訪問的。
開放端口號命令:–permanent表示永久生效,不加的話,重啓後不生效
firewall-cmd --zone=public --add-port=23000/tcp --permanent #開戶端口號
CentOS7 防火牆相關命令:
systemctl enable firewalld.service #開啓防火牆
systemctl stop firewalld.service #關閉防火牆(開機會仍會啓動)
systemctl disable firewalld.service #禁用防火牆(開機後不再啓動)
HTTP測試
現在我們再去訪問一下,原來我們上傳過的文件:
這個時候已經能成功訪問。