Nginx+FastDFS搭建图片服务器
Nginx+FastDFS搭建图片服务器……………………………………………………………………………. 1
1. 环境……………………………………………………………………………………………………………… 7
1.1. centos7…………………………………………………………………………………………………… 7
1.1.1. yum install -y gcc-c++ #需要依赖gcc………………………………………………. 7
1.1.2. yum install -y pcre #使用于支持rewrite板块 pcre-8.32-17…………………. 8
1.1.3. yum install -y libevent #FastDFS依赖libevent库 libevent-2.0.21-4.el7.x86_64 8
1.1.4. yum install -y zlib zlib-devel #使用于支持gzip板块 zlib-1.2.7-17.el7.x86_64 8
2. 依赖…………………………………………………………………………………………………………….. 8
2.1. libfastcommon-master.zip #libfastcommon包含了FastDFS运行所需要的少量基础 8
2.1.1. https://github.com/happyfish100/libfastcommon/releases……………………. 8
2.2. fastdfs-nginx-module-master.zip……………………………………………………………….. 8
2.2.1. https://codeload.github.com/happyfish100/fastdfs-nginx-module/zip/master 8
2.3. FastDFS版本:fastdfs-5.11.zip…………………………………………………………………. 8
2.3.1. https://github.com/happyfish100/fastdfs/archive/V5.11.zip…………………… 8
2.4. Nginx版本:nginx-1.14.tar.gz………………………………………………………………….. 8
2.4.1. http://nginx.org/en/download.html…………………………………………………….. 8
3. 安装依赖……………………………………………………………………………………………………… 8
3.1. libfastcommon………………………………………………………………………………………… 8
3.1.1. 安装libfastcommon………………………………………………………………………….. 8
3.1.1.1. unzip libfastcommon-1.0.39.zip……………………………………………………. 9
3.1.1.2. mv libfastcommon-1.0.39 /usr/local/libfastcommon………………………. 9
3.1.1.3. cd /usr/local/libfastcommon/………………………………………………………. 9
3.1.1.4. ./make.sh #编译…………………………………………………………………………. 9
3.1.1.5. ./make.sh install #安装……………………………………………………………….. 9
3.1.1.6. # 检查确认/usr/lib64 和 /usr/lib 目录下都生成了 libfastcommon.so (有些版本可能/usr/lib下不会生成) ls /usr/lib64 | grep libfastcommon.so ls /usr/lib | grep libfastcommon.so 9
3.2. tracker…………………………………………………………………………………………………… 9
3.2.1. 安装tracker…………………………………………………………………………………….. 9
3.2.1.1. unzip fastdfs-5.11.zip…………………………………………………………………… 9
3.2.1.2. mv fastdfs-5.11 /usr/local/FastDFS……………………………………………….. 9
3.2.1.3. cd /usr/local/FastDFS/…………………………………………………………………. 9
3.2.1.4. ./make.sh && ./make.sh install #编译之后接着进行安装……………… 9
3.2.1.5. #确认安装成功,没有报错后,拷贝配置文件到/etc/fdfs…………………. 9
3.2.1.6. cp /usr/local/FastDFS/conf/* /etc/fdfs/…………………………………………. 9
3.2.1.7. #,进入/etc/fdfs 目录, 假如没有tracker.conf就拷贝一份tracker.conf.sample 去掉sample 9
3.2.1.8. cp tracker.conf.sample track.conf………………………………………………… 9
3.2.2. 配置tracker…………………………………………………………………………………… 10
3.2.2.1. 配置tracker.conf……………………………………………………………………… 10
3.2.2.1.1. vi track.conf # 修改以下配置………………………………………………. 10
3.2.2.1.1.1. base_path=/home/fastdfs #基础目录,以后的data 和日志目录都会放在此目录下 http.server_port=80 #配置http服务端口,这个端口跟后面nginx的监听端口对应 10
3.2.2.1.2. 创立目录……………………………………………………………………………. 10
3.2.2.1.2.1. mkdir /home/fastdfs……………………………………………………… 10
3.2.3. 启动tracker…………………………………………………………………………………… 10
3.2.3.1. /usr/bin/fdfs_trackerd /etc/fdfs/track.conf restart……………………….. 10
3.2.4. 检查能否启动成功…………………………………………………………………………. 10
3.2.4.1. #检查能否启动成功:默认端口22122 ps -ef | grep fdfs………………. 10
3.2.4.2. #检查/home/fastdfs下能否生成data / logs 两个目录 ls /home/fastdfs/ 10
3.3. storage………………………………………………………………………………………………… 10
3.3.1. 安装storage…………………………………………………………………………………… 10
3.3.1.1. cd /etc/fdfs………………………………………………………………………………. 10
3.3.1.1.1. #假如没有storage.conf 拷贝一份storage.conf.sample 命名为storage.conf cp storage.conf.sample storage.conf…………………………………………………………… 11
3.3.1.1.2. #配置storage.conf vi storage.conf #修改如下配置………………… 11
3.3.1.1.2.1. group_name=group1 #配置组名,同一个组的storage 互为备份 11
3.3.1.1.2.2. base_path=/home/fastdfs #基础目录……………………………… 11
3.3.1.1.2.3. #store存放文件的位置(store_path), 可以配置多个, 记得创立路径 store_path0=/home/fdfs_storage……………………………………………………….. 11
3.3.1.1.2.4. #假如有多个挂载磁盘则定义多个store_path,如下 #store_path1=….. #store_path2=………………………………………………………………………………….. 11
3.3.1.1.2.5. #配置tracker服务器:IP,阿里云服务器假如需要外部访问请配置公网ip, 不要用私有ip tracker_server=192.168.112.130:22122………………………………………. 11
3.3.1.1.2.5.1. 子主题 1……………………………………………………………….. 11
3.3.1.1.2.5.1.1. #假如有多个tracker则配置多个tracker………….. 11
3.3.1.1.2.5.1.2. #tracker_server=192.168.112.131:22122…………….. 11
3.3.1.1.2.6. #配置http端口……………………………………………………………. 11
3.3.1.1.2.6.1. http.server_port=80………………………………………………… 11
3.3.2. 创立fdfs-storage目录………………………………………………………………….. 11
3.3.2.1. mkdir -p /home/fdfs_storage………………………………………………………. 12
3.3.3. 启动storage…………………………………………………………………………………. 12
3.3.3.1. /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart……………………. 12
3.3.4. 检查能否启动成功……………………………………………………………………….. 12
3.3.4.1. fdfs_monitor /etc/fdfs/storage.conf…………………………………………….. 12
3.3.5. 查看fdfs状态………………………………………………………………………………. 12
3.3.5.1. ps -ef | grep fdfs………………………………………………………………………… 12
3.3.6. 查看目录能否创立,/home/fdfs_storage/data 应该会自动生成256个文件夹 12
3.3.6.1. ls /home/fdfs_storage/data………………………………………………………… 12
4. 测试上传文件…………………………………………………………………………………………….. 12
4.1. 自带测试用户端………………………………………………………………………………….. 12
4.1.1. cd /etc/fdfs…………………………………………………………………………………… 12
4.1.2. cp client.conf.sample client.conf…………………………………………………….. 12
4.1.3. 修改client.conf…………………………………………………………………………….. 12
4.1.3.1. vi client.conf……………………………………………………………………………… 12
4.1.3.1.1. base_path=/home/fastdfs…………………………………………………….. 12
4.1.3.1.2. tracker_server=192.168.112.130:22122…………………………………. 12
4.1.4. 测试上传图片………………………………………………………………………………. 12
4.1.4.1. /usr/bin/fdfs_test /etc/fdfs/client.conf upload /download/123.jpg….. 13
4.1.4.1.1. 成功提醒……………………………………………………………………………. 13
4.1.5. 检查文件能否存在……………………………………………………………………….. 13
4.1.5.1. cd /home/fdfs_storage/data/00/00 #查看能否存在……………………… 13
4.1.6. 目前还不行进行下载……………………………………………………………………. 13
5. FastDFS 和Nginx整合………………………………………………………………………………… 13
5.1. fastdfs-nginx-module…………………………………………………………………………….. 13
5.1.1. 解压和安装………………………………………………………………………………….. 13
5.1.1.1. 解压…………………………………………………………………………………………. 13
5.1.1.1.1. unzip fastdfs-nginx-module-master.zip…………………………………… 13
5.1.1.2. 迁移到指定目录……………………………………………………………………….. 13
5.1.1.2.1. mv fastdfs-nginx-module-master /usr/local/fastdfs-nginx-module 13
5.1.1.3. 修改config文件……………………………………………………………………….. 13
5.1.1.3.1. # 将文件中的所有 /usr/local/ 路径改为 /usr/ , #假如后面步骤中编译nginx报错把下面两项做以下修改 ngx_module_incs=”/usr/include/fastdfs /usr/include/fastcommon/” CORE_INCS=”$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/”. 13
5.1.1.4. 拷贝配置文件到/etc/fdfs…………………………………………………………… 13
5.1.1.4.1. cp mod_fastdfs.conf /etc/fdfs/ #修改 /etc/fdfs/mod_fastdfs.conf ; vi /etc/fdfs/mod_fastdfs.conf #======修改以下内容======== base_path=/home/fastdfs #保存日志的路径 tracker_server=192.168.112.130:22122 #track_server配置的服务端口 url_have_group_name=true #url中能否包含group名称 store_path0=/home/fdfs_storage #指定文件存储路径(必需和storage.conf配置相同)………………………………………………………………………. 14
5.1.1.5. 检查/usr/lib 和usr/lib64下能否有 libfdfsclient.so……………………… 14
5.1.1.5.1. ls /usr/lib | grep libfdfsclient.so ls /usr/lib64 | grep libfdfsclient.so 14
5.1.1.6. 创立nginx/client目录……………………………………………………………….. 14
5.1.1.6.1. mkdir -p /var/temp/nginx/client……………………………………………. 14
5.2. 安装nginx…………………………………………………………………………………………… 14
5.2.1. 解压到指定目录…………………………………………………………………………… 14
5.2.1.1. tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/…………………………………….. 14
5.2.2. 编译nginx,加入fastdfs板块…………………………………………………………. 14
5.2.2.1. #进入解压目录, 日志目录可自己定义,主要是add-modoule cd /usr/local/nginx-1.14.0 #增加板块 ./configure –prefix=/usr/local/nginx –add-module=/usr/local/fastdfs-nginx-module/src 14
5.2.3. 编译并安装………………………………………………………………………………….. 15
5.2.3.1. #在nginx1.14.0目录下执行 make && make install…………………….. 15
5.2.4. 修改编译后nginx配置…………………………………………………………………. 15
5.2.4.1. vi /usr/local/nginx/conf/nginx.conf #======增加配置=========== server { listen 80; server_name 192.168.112.130; location /group1/M00/ { root /home/fdfs_storage/data; ngx_fastdfs_module; } }……………………………………………………………………….. 15
5.2.5. 启动nginx……………………………………………………………………………………. 15
5.2.5.1. /usr/local/nginx/sbin/nginx…………………………………………………………. 15
5.2.6. 检查能否启动成功……………………………………………………………………….. 15
5.2.6.1. ps -ef | grep nginx……………………………………………………………………… 15
5.2.7. 测试上传图片………………………………………………………………………………. 15
5.2.7.1. /usr/bin/fdfs_test /etc/fdfs/client.conf upload /download/123.jpg….. 15
5.2.8. 查看nginx的日志………………………………………………………………………… 15
5.2.8.1. tail -f -n 100 /var/log/nginx/access.log………………………………………… 15
6. 项目中如何用fastdfs………………………………………………………………………………. 15
6.1. 导入jar………………………………………………………………………………………………. 16
6.1.1. maven………………………………………………………………………………………….. 16
6.1.1.1. net.oschina.zcx7878 fastdfs-client-java 1.27.0.0 16
6.2. 配置文件…………………………………………………………………………………………….. 16
6.2.1. fdfs.conf……………………………………………………………………………………….. 16
6.2.1.1. # 连接tracker服务器超时时长 connect_timeout = 10 # socket连接超时时长 network_timeout = 30 # 文件内容编码 charset = UTF-8 # tracker服务器端口 http.tracker_http_port = 80 http.anti_steal_token = no #密码 http.secret_key = 123456 # tracker服务器IP和端口(可以写多个) tracker_server = 192.168.58.128:22122……………………………………………………… 16
7. 测试代码……………………………………………………………………………………………………. 16
7.1. 新建项目…………………………………………………………………………………………….. 17
7.1.1. demo……………………………………………………………………………………………. 17
7.1.1.1. public class FastdfsTest { @Test public void testUpload() throws Exception { //1、把FastDFS提供的jar包增加到工程中 //2、初始化全局配置。加载一个配置文件。 ClientGlobal.init(“F:\wolf-shop\src\main\resources\fdfs.conf”); //3、创立一个TrackerClient对象。 TrackerClient trackerClient = new TrackerClient(); //4、创立一个TrackerServer对象。 TrackerServer trackerServer = trackerClient.getConnection(); //5、公告一个StorageServer对象,null。 StorageServer storageServer = null; //6、取得StorageClient对象。 StorageClient storageClient = new StorageClient(trackerServer, storageServer); //7、直接调使用StorageClient对象方法上传文件就可。 String[] result = storageClient.upload_file(“F:\redis_demo\src\main\resources\logo.png”, “png”, null); StringBuilder sb = new StringBuilder(“http://192.168.112.130/”); sb.append(result[0]).append(“/”).append(result[1]); System.out.println(“图片访问地址: “+sb.toString()); } }…………………………………………………………………………………. 17
8. 权限控制……………………………………………………………………………………………………. 17
8.1. 服务端开启token验证………………………………………………………………………… 17
8.1.1. 修改http.conf………………………………………………………………………………. 18
8.1.1.1. 修改http.conf # vim /etc/fdfs/http.conf 设置为true表示开启token验证 http.anti_steal.check_token=true 设置token失效的时间单位为秒(s) http.anti_steal.token_ttl=1800 密钥,跟用户端配置文件的fastdfs.http_secret_key保持一致 http.anti_steal.secret_key=FASTDFS1234567890 假如token检查失败,返回的页面 http.anti_steal.token_check_fail=/ljzsg/fastdfs/page/403.html……………………… 18
8.2. 配置用户端…………………………………………………………………………………………. 18
8.2.1. # token 防盗链功能 fastdfs.http_anti_steal_token=true # 密钥 fastdfs.http_secret_key=FASTDFS1234567890………………………………………………… 18
8.3. 用户端生成token………………………………………………………………………………… 18
8.3.1. 访问文件需要带上生成的token以及unix时间戳,所以返回的token是token和时间戳的拼接 18
8.3.1.1. package com.example.nginxdfs.test; import org.csource.common.MyException; import org.csource.fastdfs.*; import org.junit.Test; import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException; import java.time.Instant; public class FastdfsTest { @Test public void testUpload() throws Exception { //1、把FastDFS提供的jar包增加到工程中 //2、初始化全局配置。加载一个配置文件。 ClientGlobal.init(“F:\\gitDemo\\nginxdfs\\src\\main\\resources\\fdfs.conf”); //3、创立一个TrackerClient对象。 TrackerClient trackerClient = new TrackerClient(); //4、创立一个TrackerServer对象。 TrackerServer trackerServer = trackerClient.getConnection(); //5、公告一个StorageServer对象,null。 StorageServer storageServer = null; //6、取得StorageClient对象。 StorageClient storageClient = new StorageClient(trackerServer, storageServer); //7、直接调使用StorageClient对象方法上传文件就可。 String[] result = storageClient.upload_file(“F:\\gitDemo\\nginxdfs\\src\\main\\resources\\1.jpg”, “png”, null); StringBuilder sb = new StringBuilder(“http://10.2.45.57/”); sb.append(result[0]).append(“/”).append(result[1]); String httpSecretKey=”FastDFS1234567890charleslai”; String lastUrl= getToken(sb.toString(),httpSecretKey); System.out.println(“图片访问地址: “+sb.toString()+”?”+lastUrl); } /** * 获取访问服务器的token,拼接到地址后面 * * @param getFilename 文件路径 group1/M00/00/00/wKgzgFnkTPyAIAUGAAEoRmXZPp876.jpeg * @param httpSecretKey 密钥 * @return 返回token,如: token=078d370098b03e9020b82c829c205e1f&ts=1508141521 */ public static String getToken(String getFilename, String httpSecretKey){ // unix seconds int ts = (int) Instant.now().getEpochSecond(); // token String token = “null”; try { token = ProtoCommon.getToken(getFilename, ts, httpSecretKey); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } StringBuilder sb = new StringBuilder(); sb.append(“token=”).append(token); sb.append(“&ts=”).append(ts); return sb.toString(); } }……………………………………………………………………………… 18
1. 环境
1.1. centos7
1.1.1. yum install -y gcc-c++ #需要依赖gcc
1.1.2. yum install -y pcre #使用于支持rewrite板块 pcre-8.32-17
1.1.3. yum install -y libevent #FastDFS依赖libevent库 libevent-2.0.21-4.el7.x86_64
1.1.4. yum install -y zlib zlib-devel #使用于支持gzip板块 zlib-1.2.7-17.el7.x86_64
2. 依赖
2.1. libfastcommon-master.zip #libfastcommon包含了FastDFS运行所需要的少量基础
2.1.1. https://github.com/happyfish100/libfastcommon/releases
2.2. fastdfs-nginx-module-master.zip
2.2.1. https://codeload.github.com/happyfish100/fastdfs-nginx-module/zip/master
2.3. FastDFS版本:fastdfs-5.11.zip
2.3.1. https://github.com/happyfish100/fastdfs/archive/V5.11.zip
2.4. Nginx版本:nginx-1.14.tar.gz
2.4.1. http://nginx.org/en/download.html
3. 安装依赖
3.1. libfastcommon
3.1.1. 安装libfastcommon
3.1.1.1. unzip libfastcommon-1.0.39.zip
3.1.1.2. mv libfastcommon-1.0.39 /usr/local/libfastcommon
3.1.1.3. cd /usr/local/libfastcommon/
3.1.1.4. ./make.sh #编译
3.1.1.5. ./make.sh install #安装
3.1.1.6. # 检查确认/usr/lib64 和 /usr/lib 目录下都生成了 libfastcommon.so (有些版本可能/usr/lib下不会生成)
ls /usr/lib64 | grep libfastcommon.so
ls /usr/lib | grep libfastcommon.so
3.2. tracker
3.2.1. 安装tracker
3.2.1.1. unzip fastdfs-5.11.zip
3.2.1.2. mv fastdfs-5.11 /usr/local/FastDFS
3.2.1.3. cd /usr/local/FastDFS/
3.2.1.4. ./make.sh && ./make.sh install #编译之后接着进行安装
3.2.1.5. #确认安装成功,没有报错后,拷贝配置文件到/etc/fdfs
3.2.1.6. cp /usr/local/FastDFS/conf/* /etc/fdfs/
3.2.1.7. #,进入/etc/fdfs 目录, 假如没有tracker.conf就拷贝一份tracker.conf.sample 去掉sample
3.2.1.8. cp tracker.conf.sample track.conf
3.2.2. 配置tracker
3.2.2.1. 配置tracker.conf
3.2.2.1.1. vi track.conf # 修改以下配置
3.2.2.1.1.1. base_path=/home/fastdfs #基础目录,以后的data 和日志目录都会放在此目录下
http.server_port=80 #配置http服务端口,这个端口跟后面nginx的监听端口对应
3.2.2.1.2. 创立目录
3.2.2.1.2.1. mkdir /home/fastdfs
3.2.3. 启动tracker
3.2.3.1. /usr/bin/fdfs_trackerd /etc/fdfs/track.conf restart
3.2.4. 检查能否启动成功
3.2.4.1. #检查能否启动成功:默认端口22122
ps -ef | grep fdfs
3.2.4.2. #检查/home/fastdfs下能否生成data / logs 两个目录
ls /home/fastdfs/
3.3. storage
3.3.1. 安装storage
3.3.1.1. cd /etc/fdfs
3.3.1.1.1. #假如没有storage.conf 拷贝一份storage.conf.sample 命名为storage.conf
cp storage.conf.sample storage.conf
3.3.1.1.2. #配置storage.conf
vi storage.conf #修改如下配置
3.3.1.1.2.1. group_name=group1 #配置组名,同一个组的storage 互为备份
3.3.1.1.2.2. base_path=/home/fastdfs #基础目录
3.3.1.1.2.3. #store存放文件的位置(store_path), 可以配置多个, 记得创立路径
store_path0=/home/fdfs_storage
3.3.1.1.2.4. #假如有多个挂载磁盘则定义多个store_path,如下
#store_path1=…..
#store_path2=……
3.3.1.1.2.5. #配置tracker服务器:IP,阿里云服务器假如需要外部访问请配置公网ip, 不要用私有ip
tracker_server=192.168.112.130:22122
3.3.1.1.2.5.1. 子主题 1
3.3.1.1.2.5.1.1. #假如有多个tracker则配置多个tracker
3.3.1.1.2.5.1.2. #tracker_server=192.168.112.131:22122
3.3.1.1.2.6. #配置http端口
3.3.1.1.2.6.1. http.server_port=80
3.3.2. 创立fdfs-storage目录
3.3.2.1. mkdir -p /home/fdfs_storage
3.3.3. 启动storage
3.3.3.1. /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
3.3.4. 检查能否启动成功
3.3.4.1. fdfs_monitor /etc/fdfs/storage.conf
3.3.5. 查看fdfs状态
3.3.5.1. ps -ef | grep fdfs
3.3.6. 查看目录能否创立,/home/fdfs_storage/data 应该会自动生成256个文件夹
3.3.6.1. ls /home/fdfs_storage/data
4. 测试上传文件
4.1. 自带测试用户端
4.1.1. cd /etc/fdfs
4.1.2. cp client.conf.sample client.conf
4.1.3. 修改client.conf
4.1.3.1. vi client.conf
4.1.3.1.1. base_path=/home/fastdfs
4.1.3.1.2. tracker_server=192.168.112.130:22122
4.1.4. 测试上传图片
4.1.4.1. /usr/bin/fdfs_test /etc/fdfs/client.conf upload /download/123.jpg
4.1.4.1.1. 成功提醒
4.1.5. 检查文件能否存在
4.1.5.1. cd /home/fdfs_storage/data/00/00 #查看能否存在
4.1.6. 目前还不行进行下载
5. FastDFS 和Nginx整合
5.1. fastdfs-nginx-module
5.1.1. 解压和安装
5.1.1.1. 解压
5.1.1.1.1. unzip fastdfs-nginx-module-master.zip
5.1.1.2. 迁移到指定目录
5.1.1.2.1. mv fastdfs-nginx-module-master /usr/local/fastdfs-nginx-module
5.1.1.3. 修改config文件
5.1.1.3.1. # 将文件中的所有 /usr/local/ 路径改为 /usr/ ,
#假如后面步骤中编译nginx报错把下面两项做以下修改
ngx_module_incs=”/usr/include/fastdfs /usr/include/fastcommon/”
CORE_INCS=”$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/”
5.1.1.4. 拷贝配置文件到/etc/fdfs
5.1.1.4.1. cp mod_fastdfs.conf /etc/fdfs/
#修改 /etc/fdfs/mod_fastdfs.conf ;
vi /etc/fdfs/mod_fastdfs.conf
#======修改以下内容========
base_path=/home/fastdfs #保存日志的路径
tracker_server=192.168.112.130:22122 #track_server配置的服务端口
url_have_group_name=true #url中能否包含group名称
store_path0=/home/fdfs_storage #指定文件存储路径(必需和storage.conf配置相同)
5.1.1.5. 检查/usr/lib 和usr/lib64下能否有 libfdfsclient.so
5.1.1.5.1. ls /usr/lib | grep libfdfsclient.so
ls /usr/lib64 | grep libfdfsclient.so
5.1.1.6. 创立nginx/client目录
5.1.1.6.1. mkdir -p /var/temp/nginx/client
5.2. 安装nginx
5.2.1. 解压到指定目录
5.2.1.1. tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/
5.2.2. 编译nginx,加入fastdfs板块
5.2.2.1. #进入解压目录, 日志目录可自己定义,主要是add-modoule
cd /usr/local/nginx-1.14.0
#增加板块
./configure
–prefix=/usr/local/nginx
–add-module=/usr/local/fastdfs-nginx-module/src
5.2.3. 编译并安装
5.2.3.1. #在nginx1.14.0目录下执行
make && make install
5.2.4. 修改编译后nginx配置
5.2.4.1. vi /usr/local/nginx/conf/nginx.conf
#======增加配置===========
server {
listen 80;
server_name 192.168.112.130;
location /group1/M00/ {
root /home/fdfs_storage/data;
ngx_fastdfs_module;
}
}
5.2.5. 启动nginx
5.2.5.1. /usr/local/nginx/sbin/nginx
5.2.6. 检查能否启动成功
5.2.6.1. ps -ef | grep nginx
5.2.7. 测试上传图片
5.2.7.1. /usr/bin/fdfs_test /etc/fdfs/client.conf upload /download/123.jpg
5.2.8. 查看nginx的日志
5.2.8.1. tail -f -n 100 /var/log/nginx/access.log
6. 项目中如何用fastdfs
6.1. 导入jar
6.1.1. maven
6.1.1.1.
net.oschina.zcx7878
fastdfs-client-java
1.27.0.0
6.2. 配置文件
6.2.1. fdfs.conf
6.2.1.1. # 连接tracker服务器超时时长
connect_timeout = 10
# socket连接超时时长
network_timeout = 30
# 文件内容编码
charset = UTF-8
# tracker服务器端口
http.tracker_http_port = 80
http.anti_steal_token = no
#密码
http.secret_key = 123456
# tracker服务器IP和端口(可以写多个)
tracker_server = 192.168.58.128:22122
7. 测试代码
7.1. 新建项目
7.1.1. demo
7.1.1.1. public class FastdfsTest {
@Test
public void testUpload() throws Exception {
//1、把FastDFS提供的jar包增加到工程中
//2、初始化全局配置。加载一个配置文件。
ClientGlobal.init(“F:\wolf-shop\src\main\resources\fdfs.conf”);
//3、创立一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
//4、创立一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
//5、公告一个StorageServer对象,null。
StorageServer storageServer = null;
//6、取得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//7、直接调使用StorageClient对象方法上传文件就可。
String[] result = storageClient.upload_file(“F:\redis_demo\src\main\resources\logo.png”, “png”, null);
StringBuilder sb = new StringBuilder(“http://192.168.112.130/”);
sb.append(result[0]).append(“/”).append(result[1]);
System.out.println(“图片访问地址: “+sb.toString());
}
}
8. 权限控制
8.1. 服务端开启token验证
8.1.1. 修改http.conf
8.1.1.1. 修改http.conf
# vim /etc/fdfs/http.conf
设置为true表示开启token验证
http.anti_steal.check_token=true
设置token失效的时间单位为秒(s)
http.anti_steal.token_ttl=1800
密钥,跟用户端配置文件的fastdfs.http_secret_key保持一致
http.anti_steal.secret_key=FASTDFS1234567890
假如token检查失败,返回的页面
http.anti_steal.token_check_fail=/ljzsg/fastdfs/page/403.html
8.2. 配置用户端
8.2.1. # token 防盗链功能
fastdfs.http_anti_steal_token=true
# 密钥
fastdfs.http_secret_key=FASTDFS1234567890
8.3. 用户端生成token
8.3.1. 访问文件需要带上生成的token以及unix时间戳,所以返回的token是token和时间戳的拼接
8.3.1.1. package com.example.nginxdfs.test;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
public class FastdfsTest {
@Test
public void testUpload() throws Exception {
//1、把FastDFS提供的jar包增加到工程中
//2、初始化全局配置。加载一个配置文件。
ClientGlobal.init(“F:\\gitDemo\\nginxdfs\\src\\main\\resources\\fdfs.conf”);
//3、创立一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
//4、创立一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
//5、公告一个StorageServer对象,null。
StorageServer storageServer = null;
//6、取得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//7、直接调使用StorageClient对象方法上传文件就可。
String[] result = storageClient.upload_file(“F:\\gitDemo\\nginxdfs\\src\\main\\resources\\1.jpg”, “png”, null);
StringBuilder sb = new StringBuilder(“http://10.2.45.57/”);
sb.append(result[0]).append(“/”).append(result[1]);
String httpSecretKey=”FastDFS1234567890charleslai”;
String lastUrl= getToken(sb.toString(),httpSecretKey);
System.out.println(“图片访问地址: “+sb.toString()+”?”+lastUrl);
}
/**
* 获取访问服务器的token,拼接到地址后面
*
* @param getFilename 文件路径 group1/M00/00/00/wKgzgFnkTPyAIAUGAAEoRmXZPp876.jpeg
* @param httpSecretKey 密钥
* @return 返回token,如: token=078d370098b03e9020b82c829c205e1f&ts=1508141521
*/
public static String getToken(String getFilename, String httpSecretKey){
// unix seconds
int ts = (int) Instant.now().getEpochSecond();
// token
String token = “null”;
try {
token = ProtoCommon.getToken(getFilename, ts, httpSecretKey);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
sb.append(“token=”).append(token);
sb.append(“&ts=”).append(ts);
return sb.toString();
}
}
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Nginx+FastDFS搭建图片服务器