FastDFS + Nginx文件上传
2021/9/8 7:06:03
本文主要是介绍FastDFS + Nginx文件上传,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Nginx
环境依赖
- gcc
yum install gcc-c++
- pcre
yum install -y pcre pcre-devel
- zlib
**yum install -y zlib zlib-devel
- openssl
yum install -y openssl openssl-devel
具体操作
- 下载
wget http://nginx.org/download/nginx-1.12.0.tar.gz
- 解压
tar -zxvf nginx-1.12.0.tar.gz
- 配置
./configure
- 编译、安装
make && make install
- 验证
cd /usr/local/nginx
,存在conf、sbin、html文件夹,安装成功!
使用
- 启动
/usr/local/nginx/sbin/nginx
,使用虚拟机ip进行访问。
- 停止
cd /usr/local/nginx/sbin && ./nginx -s quit
(推荐)
- 再启
- 先停再启
cd /usr/local/nginx/sbin && ./nginx -s quit && ./nginx
(推荐) - 直接重启
cd /usr/local/nginx/sbin && ./nginx -s reload
- 先停再启
FastDFS
Tracker
- 环境、库、安装包
- gcc
yum install gcc-c++
- libevent
yum -y install libevent
- libfastcommon_url_1
cd /usr/local && tar -zxvf libfastcommon-1.0.43.tar.gz && cd libfastcommon-1.0.43 && ./make.sh && ./make.sh install
- tracker_url_2
cd /usr/local/ && tar -zxvf fastdfs-6.06.tar.gz && cd fastdfs-6.06/ && ./make.sh && ./make.sh install
- gcc
- 配置文件拷贝
cd conf/ && cp ./* /etc/fdfs/
-
配置
cd /etc/fdfs/ && vim tracker.conf
- 端口保持默认:22122
- 修改元数据存储目录:base_path = /home/dj/dev/fastdfs
-
启动
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
Storage
-
环境、库、安装包
- libevent、libfastcommon、storage(同tracker)
-
配置
cd /etc/fdfs/ && vim storage.conf
- 修改:base_path = /home/dj/dev/fastdfs
- 修改:store_path0 = /home/dj/dev/fastdfs
- 修改:tracker_server = 192.168.1.11:22122(保留一个即可)
-
启动
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
fastdfs-nginx-module
下载
fastdfs-nginx-module_url:
cd /usr/local && tar -zxvf fastdfs-nginx-module-1.22.tar.gz && cd fastdfs-nginx-module-1.22
文件拷贝
cp /usr/local/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
配置
vim /etc/fdfs/mod_fastdfs.conf
- 修改:tracker_server=192.168.1.11:22122
- 修改:url_have_group_name = true
- 修改:store_path0:/home/dj/dev/fastdfs
重新编译、安装
前往nginx安装目录并执行:
cd /home/dj/dev/nginx/nginx-1.12.0/ && ./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src && make && make install
修改配置文件
vim /usr/local/nginx/conf/nginx.conf
location ~/group([0-9]) { ngx_fastdfs_module; }
Java客户端
建一个Maven工程
- 添加依赖
<dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
- 配置文件
fastdfs.connect_timeout_in_seconds = 5 fastdfs.network_timeout_in_seconds = 30 fastdfs.charset = UTF-8 fastdfs.http_anti_steal_token = false fastdfs.http_secret_key = FastDFS1234567890 fastdfs.http_tracker_http_port = 80 fastdfs.tracker_servers = 192.168.1.11:22122 fastdfs.connection_pool.enabled = true fastdfs.connection_pool.max_count_per_entry = 500 fastdfs.connection_pool.max_idle_time = 3600 fastdfs.connection_pool.max_wait_time_in_ms = 1000
文件上传
package top.dj; import lombok.extern.slf4j.Slf4j; import org.csource.common.NameValuePair; import org.csource.fastdfs.*; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; /** * @Author: DengJia * @Date: 2021/9/7 21:49 * @Description: */ @SpringBootTest @Slf4j public class DFSTest { private final static Logger logger = LoggerFactory.getLogger(DFSTest.class); @Test void testUpload() { try { ClientGlobal.initByProperties("fastdfs-client.properties"); TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient1 client = new StorageClient1(trackerServer, storageServer); NameValuePair nvp[] = null; //上传到文件系统 String fileId = client.upload_file1("F:\\ps\\centos-docker.png", "png", nvp); logger.info(fileId); } catch (Exception e) { e.printStackTrace(); } } }
这篇关于FastDFS + Nginx文件上传的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-29Nginx发布学习:从入门到实践的简单教程
- 2024-10-28Nginx发布:新手入门教程
- 2024-10-21nginx 怎么设置文件上传最大20M限制-icode9专业技术文章分享
- 2024-10-17关闭 nginx的命令是什么?-icode9专业技术文章分享
- 2024-09-17Nginx实用篇:实现负载均衡、限流与动静分离
- 2024-08-21宝塔nginx新增8022端口方法步骤-icode9专业技术文章分享
- 2024-08-21nginx配置,让ws升级为wss访问的方法步骤-icode9专业技术文章分享
- 2024-08-15nginx ws代理配置方法步骤-icode9专业技术文章分享
- 2024-08-14nginx 让访问带有/relid的地址返回404 ,例子 /relid-x-0.36-y-131.html-icode9专业技术文章分享
- 2024-08-14nginx 判断地址有/statics/的路径,指向到/home/html/statics/目录-icode9专业技术文章分享