配置nginx爲storage server提供http訪問接口:html
==============================================================================node
背景:nginx
在大多數業務場景中,每每須要爲FastDFS存儲的文件提供http下載服務,而儘管FastDFS在其storage及tracker都內置了http服務, 但性能表現卻不盡如人意;git
做者餘慶在後來的版本中增長了基於當前主流web服務器的擴展模塊(包括nginx/apache),其用意在於利用web服務器直接對本機storage數據文件提供http服務,以提升文件下載的性能。
github
描述:web
nginx 配置在storage節點上(本例以storage節點的node2爲例);apache
由於 nginx 要想鏈接 fastdfs 就必需要有相應的模塊,因此要編譯安裝 nginx 把fastdfs-nginx-module 模塊編譯進去
centos
步驟以下:
瀏覽器
1.首先下載 fastdfs-nginx-module 和 編譯安裝nginx 的源碼包bash
# 從官方站點上克隆 fastdfs-nginx-module 模塊 [root@centos7 ~]# git clone https://github.com/happyfish100/fastdfs-nginx-module.git Cloning into 'fastdfs-nginx-module'... remote: Counting objects: 57, done. remote: Total 57 (delta 0), reused 0 (delta 0), pack-reused 57 Unpacking objects: 100% (57/57), done. # 準備好的nginx源碼包以下 [root@centos7 fastdfs]# ls fastdfs-nginx-module nginx-1.10.0.tar.gz [root@centos7 fastdfs]# tar xf nginx-1.10.0.tar.gz [root@centos7 fastdfs]# ls fastdfs-nginx-module nginx-1.10.0 nginx-1.10.0.tar.gz
2.準備編譯安裝的環境
yum groupinstall "Development Tools" "Server Platfrom Development" -y yum install pcre-devel openssl-devel zlib-devel -y
3.建立 nginx 用戶編譯安裝nginx,指明 fastdfs-nginx-module 模塊的位置
[root@centos7 nginx-1.10.0]# useradd -r nginx [root@centos7 nginx-1.10.0]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src [root@centos7 nginx-1.10.0]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre --add-module=/root/fastdfs/fastdfs-nginx-module/src [root@centos7 nginx-1.10.0]# make && make install
三、複製配置文件
# cp fastdfs-nginx/module/src/mod_fastdfs.conf /etc/fdfs/ # cp fastdfs-5.0.8/conf/{httpd.conf,mime.types} /etc/fdfs/ 注意:fastdfs-5.0.8爲fastdfs源碼目錄;
四、配置/etc/fdfs/mod_fastdfs.conf配置文件,部分指令相似以下所示;
base_path=/data/fdfs/storage tracker_server=192.168.1.112:22122 storage_server_port=23000 group_name=group1 url_have_group_name = true store_path_count=1 store_path0=/data/fdfs/storage/0 [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/data/fdfs/storage/0
五、配置nginx,編輯nginx.conf,在server中添加相似以下內容:
location ~ /group[0-9]+/M00/ { root /data/fdfs/storage/0/data/; ngx_fastdfs_module; }
六、爲存儲文件路徑建立連接至M00,此示例中使用的storage_path0爲/data/fdfs/storage/0;
# ln -sv /data/fdfs/storage/0 /data/fdfs/storage/0/M00 [root@centos7 ~]# ll /data/fdfs/storage/0/ total 12 drwxr-xr-x 258 root root 8192 Mar 24 11:50 data lrwxrwxrwx 1 root root 20 Mar 24 11:23 M00 -> /data/fdfs/storage/0
七、啓動nginx服務便可;
[root@centos7 sbin]# pwd /usr/local/nginx/sbin [root@centos7 sbin]# ./nginx -t ngx_http_fastdfs_set pid=27041 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@centos7 sbin]# ./nginx [root@centos7 sbin]# ps aux| grep 'nginx' root 27011 0.0 0.1 52880 1160 ? Ss 11:25 0:00 nginx: master process ./nginx nginx 27012 0.0 0.2 55524 2516 ? S 11:25 0:00 nginx: worker process
八、上傳文件,測試:
# node3 節點上傳文件 [root@centos7 fdfs]# fdfs_upload_file /etc/fdfs/client.conf /usr/share/wallpapers/Autumn/contents/p_w_picpaths/1600x1200.jpg group1/M00/00/00/wKgBcVjUlzqAEyeUAAxFCUUQ0Sc981.jpg [root@centos7 fdfs]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKgBcVjUlzqAEyeUAAxFCUUQ0Sc981.jpg source storage id: 0 source ip address: 192.168.1.113 file create timestamp: 2017-03-24 11:49:14 file size: 804105 file crc32: 1158730023 (0x4510D127) [root@centos7 fdfs]# fdfs_upload_file /etc/fdfs/client.conf /usr/share/wallpapers/Hanami/contents/p_w_picpaths/1920x1200.jpg group1/M00/00/00/wKgBcljUGkCAEkdsAAqAdF8ZgSw271.jpg [root@centos7 fdfs]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKgBcljUGkCAEkdsAAqAdF8ZgSw271.jpg source storage id: 0 source ip address: 192.168.1.114 file create timestamp: 2017-03-24 02:56:00 file size: 688244 file crc32: 1595506988 (0x5F19812C)
9.在瀏覽器中訪問路徑的URL爲fid: