多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 7 Nginx上部署fastDFS **Nginx安装fastDFS模块,主要是安装在fastDFS,storage存储服务器上,而不是tracker和client上。** 目的实际是为了,当输入类似 http://192.168.2.108/group1/M00/00/00/wKgCbFem0l2ALSbFAAEYXfRAMkc536.png URL地址,其中192.168.2.108 是一个storage服务器。 能够通过nginx的web服务功能,直接返回图片。 ### 注意:全部安装条件在确保之前的fastDFS 的Tracker和storage和client可以正常使用。 ### (1) 解压Nginx的fastDFS模块包 ```bash tar -zxvf fastdfs-nginx-module_v1.16.tar.gz cd fastdfs-nginx-module/ ``` 会发现里面有个INSTALL 和 src目录。这个不需要make而是需要重新编译一下storage的Nginx模块 ### (2) 安装和编译Nginx 并添加fastDFS模块 ```bash cd nginx-1.10.1/ ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/fastdfs-nginx-module/src ``` 其中 /usr/local/src/fastdfs-nginx-module/src 是刚才解压的fastdfs模块的绝对路径,就是在编译nginx时候,连同这个模块一起编译。 ```bash make sudo make install ``` > 如果中途出现编译错误,找不到fastdfs_define.h等头文件。 >原因可能是nginx是会想/usr/local/include/fastdfs/去找, 而不是去/usr/include/fastdfs/去找 >将/usr/include/fastdfs/和/usr/include/fastcommon/两个文件夹拷贝到/usr/local/include/下即可。 ### (3) fastdfs-nginx-module 配置文件 将fastdfs-nginx-module/src/mod_fastdfs.conf 拷贝到/etc/fdfs/下 ```bash cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ ``` 修改mod_fastdfs.conf --修改文件: ```bash base_path=/usr/fastdfs/storage #保存日志目录,跟storage.conf一致即可 tracker_server=192.168.2.102:22122 #tracker服务器的IP地址以及端口号,确保跟storage.conf一致即可 storage_server_port=23000 #storage服务器的端口号,确保跟storage.conf一致即可 group_name=group1 #当前服务器的group名,确保跟storage.conf一致即可 url_have_group_name = true #文件url中是否有group名,这个最好设置为true,这样方便nginx服务器解析 store_path_count=1 #存储路径个数,需要和store_path个数匹配 store_path0=/usr/fastdfs/fastdfs0 #存储路径,跟storage.conf一致即可 group_count = 1 #设置组的个数 [group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/usr/fastdfs/fastdfs0 #如果group_count =2 ,那就再配置一个 ``` ### 4. 建立软连接 ```bash ln -s /usr/fastdfs/fastdfs0/data /usr/fastdfs/fastdfs0/data/M00 ``` ### 5. 配置nginx ```bash location /group1/M00 { root /usr/fastdfs/fastdfs0/data; ngx_fastdfs_module; } ``` 启动Nginx,观察/usr/local/nginx/logs/errors.log 是否有ERROR存在。 如果有类似如下错误 ```bash ERROR - file: ini_file_reader.c, line: 315, include file "http.conf" not exists, line: "#include http.conf" ERROR - file: /root/Open_Package/fastdfs-nginx-module/src/common.c, line: 155, load conf file "/etc/fdfs/mod_fastdfs.conf" fail, ret code: 2 2016/08/06 23:51:13 [alert] 12734#0: worker process 12735 exited with fatal code 2 and cannot be respawned ``` 类似这种错误会导致nginx-worker启动失败,致使nginx无法提供web服务。 表示/etc/fdfs/下缺少http.conf 和mime.types文件 从fastdfs-5.05/conf/http.conf 拷贝到/etc/fdfs/下 从nginx-1.10.1/conf/mime.types 拷贝到/etc/fdfs/下。 ### (6)启动storage和nginx ```bash /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart /usr/local/nginx/sbin/nginx -s reload ``` 通过fdfs_monitor /etc/fdfs/storage.conf 看看本台storage服务器是否处于ACTIVE状态。如果可以表示可以通过client上传和下载文件。 ### (7)测试 在client服务器 使用命令上传 ```bash fdfs_test ./conf/client.conf upload 1.png ``` ```bash This is FastDFS client test program v5.05 Copyright (C) 2008, Happy Fish / YuQing FastDFS may be copied only under the terms of the GNU General Public License V3, which may be found in the FastDFS source kit. Please visit the FastDFS Home Page http://www.csource.org/ for more detail. [2016-08-07 17:47:10] DEBUG - base_path=/usr/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0 tracker_query_storage_store_list_without_group: server 1. group_name=, ip_addr=192.168.2.108, port=23000 group_name=group1, ip_addr=192.168.2.108, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKgCbFenA6SAShwgAAEYXfRAMkc895.png source ip address: 192.168.2.108 file timestamp=2016-08-07 17:47:16 file size=71773 file crc32=4097847879 example file url: http://192.168.2.108/group1/M00/00/00/wKgCbFenA6SAShwgAAEYXfRAMkc895.png storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKgCbFenA6SAShwgAAEYXfRAMkc895_big.png source ip address: 192.168.2.108 file timestamp=2016-08-07 17:47:16 file size=71773 file crc32=4097847879 example file url: http://192.168.2.108/group1/M00/00/00/wKgCbFenA6SAShwgAAEYXfRAMkc895_big.png ``` 其中 http://192.168.2.108/group1/M00/00/00/wKgCbFenA6SAShwgAAEYXfRAMkc895.png 变为这个文件最终存放的web url路径。192.168.2.108 应该是storage服务器的ip地址。 打开浏览器 输入 http://192.168.2.108/group1/M00/00/00/wKgCbFenA6SAShwgAAEYXfRAMkc895.png 得到图片显示,则表示storage已经成功配置nginx服务。