构建可靠的文件上传解决方案:详解nginx-fastdfs和upload-module的安装与配置

模块包的安装

全部安装条件在确保之前的FastDFS 的tracker、storage和client可以正常使用。
模块包源码包下载地址

备份原有nginx

为避免端口直接的冲突,建议备份原有的nginx服务。

cd /usr/local
sudo mv nginx bk-nginx-20221106

下载fastdfs-nginx-module

wget https://github.com/happyfish100/fastdfs-nginx-module/archive/refs/tags/V1.22.tar.gz
tar -zxvf V1.22.tar.gz 
cd fastdfs-nginx-module-1.22/

cd fastdfs-nginx-module-1.22/ 会发现里面有个INSTALL 和 src目录,这个不需要make而是需要重新编译一下storage的Nginx模块。

查看fastdfs-nginx-module模块src决对路径(后面要用决对路径):

# pwd
/home/fly/fastdfs-nginx-module-1.22

下载nginx-upload-module

(1)下载原始的版本(原始版本有bug):

wget http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz

(2)解压:

tar -zxvf nginx_upload_module-2.2.0.tar.gz

(3)下载修改过的nginx upload模块,替换部分原始的文件:

git clone https://github.com/winshining/nginx-upload-module.git

(4)拷贝当前该目录的内容到nginx_upload_module-2.2.0:

cd nginx-upload-module
cp -arf * ../nginx_upload_module-2.2.0

(5)获取到路径:

cd ../nginx_upload_module-2.2.0
pwd

显示:

/home/fly/nginx_upload_module-2.2.0

安装和编译Nginx 并添加FastDFS模块

进入到nginx源码目录(笔者路径:/home/fly/nginx-1.16.1)配置添加模块:

#进入到nginx源码目录,(笔者路径:/home/fly/nginx-1.16.1)
cd nginx-1.16.1/
# 配置添加模块:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-openssl=../openssl-1.1.1g --add-module=/home/fly/fastdfs-nginx-module-1.22/src --add-module=/home/fly/nginx_upload_module-2.2.0

注意–add-module的路径,用自己的路径,最好是绝对路径,防止错误。
其中/home/fly/fastdfs-nginx-module-1.22/src 是刚才下载的的fastdfs_nginx_module模块的绝对路径,就是在编译Nginx时候,连同这个模块一起编译。

configure时, 出现以下信息表示添加成功:

configuring additional modules
adding module in /home/fly/fastdfs-nginx-module-1.22/src
 + ngx_http_fastdfs_module was configured
adding module in /home/fly/nginx_upload_module-2.2.0
 + ngx_http_upload_module was configured

给 nginx 目录下的 objs/Makefile 文件中增加头文件目录

(1)打开objs/Makefile 文件:

vim objs/Makefile

(2)添加:

ALL_INCS = -I src/core \
	-I /usr/include/fastdfs \
	-I /usr/include/fastcommon \
	-I src/event \
	-I src/event/modules \
	-I src/os/unix \
	-I ../openssl-1.1.1g/.openssl/include \
	-I objs \
	-I src/http \
	-I src/http/modules \
	-I src/http/v2 \
	-I /usr/local/include

在这里插入图片描述
特别需要注意加入两行后 -I 和 \ 的颜色要和原来一致,否则报错:

Makefile:8: recipe for target 'build' failedmake: *** [build] Error 2

重新编译及安装nginx

make
sudo make install

安装成功后:通过nginx -V 测试编译是否正常。

sudo /usr/local/nginx/sbin/nginx -V

显示:

nginx version: nginx/1.16.1
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) 
built with OpenSSL 1.1.1g  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-openssl=../openssl-1.1.1g --add-module=/home/fly/fastdfs-nginx-module-1.22/src --add-module=/home/fly/nginx_upload_module-2.2.0

fastdfs-nginx-module 的配置(注意细节)

拷贝fastdfs-nginx-module配置文件

(1)切换到fastdfs-nginx-module-1.22/src路径(笔者的路径是/home/fly/fastdfs-nginx-module-1.22/src):

cd /home/fly/fastdfs-nginx-module-1.22/src
# 查看文件
ls

(2)将fastdfs-nginx-module-1.22/src/mod_fastdfs.conf 拷贝到/etc/fdfs/下:

sudo cp mod_fastdfs.conf /etc/fdfs/

因为/etc/fdfs/是默认的加载路径。

拷贝fastdfs/conf 配置文件

(1)切换到fastdfs源码路径(笔者的路径是/home/fly/fastdfs):

cd /home/fly/fastdfs

(2)拷贝相关文件conf/http.conf和conf/mime.types到/etc/fdfs/。

sudo cp conf/http.conf /etc/fdfs/
sudo cp conf/mime.types /etc/fdfs/

修改配置文件

创建目录(一定要记得):

mkdir -p /home/fastdfs/mod_fastdfs

修改配置:

vim /etc/fdfs/mod_fastdfs.conf

base_path =/home/fastdfs/mod_fastdfs :保存日志目录
tracker_server =114.215.169.66:22122 :tracker服务器的IP地址以及端口号, 确保跟storage.conf一致即可。
store_path0=/home/fastdfs/storage:确保路径正确并存在。

base_path =/home/fastdfs/mod_fastdfs
# Tracker 服务器IP和端口修改
tracker_server=114.215.169.66:22122
# url 中是否包含 group 名称,改为 true,包含 group
url_have_group_name = true
# store_path0的路径必须和storage.conf的配置一致
store_path0=/home/fastdfs/storage
# 其它的一般默认即可,例如
group_name=group1
storage_server_port=23000
store_path_count=1

主要修改base_path 、tracker_server、url_have_group_name、store_path0。

配置nginx

vim /usr/local/nginx/conf/nginx.conf

配置为支持 group0-group9,以及 M00-M99,以便于以后扩容。

location ~/group([0-9])/M([0-9])([0-9]) {
	ngx_fastdfs_module;
}

具体位置:
ngx_fastdfs_module

重启nginx

配置完fastdfs-nginx-module后需要重启nginx。

# 停止:
sudo /usr/local/nginx/sbin/nginx -s stop
# 启动:
sudo /usr/local/nginx/sbin/nginx

如果出现如下错误。需要先停止nginx再启动nginx:

nginx: [emerg] unknown directive "ngx_fastdfs_module" in
/usr/local/nginx/conf/nginx.conf:92

指定配置文件的方式启动:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

查看nginx是否启动正常:

ps -ef | grep nginx

成功则显示:

root      53763      1  0 22:13 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    53764  53763  0 22:13 ?        00:00:00 nginx: worker process

如果木有work进程说明启动是异常的,一般有以下原因:

  1. base_path对应的路径木有创建。
  2. tracker_server 配置错误。
  3. store_path0 配置错误。

测试环境

服务器中测试上传。

touch fly.txt

echo "You are not strong, no one brave for you" > fly.txt

sudo /usr/bin/fdfs_upload_file /etc/fdfs/client.conf fly.txt

得到:

group1/M00/00/00/wKgAaGNnwtmAYy4NAAAAKTIQHvk741.txt

拿到存储位置:group1/M00/00/00/wKgAaGNnwtmAYy4NAAAAKTIQHvk741.txt,如果是group1开头,查看group1中storage文件是否存在。

浏览器输入:

http://192.168.10.104/group1/M00/00/00/wKgAaGNnwtmAYy4NAAAAKTIQHvk741.txt

浏览器能显示对应的文本信息。

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lion Long

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值