FastDFS是一款开源的轻量级分布式文件系统,其主要特点是高性能、可扩展性、高可靠性和开源免费等。FastDFS主要解决了海量数据存储问题,适合大规模的图片或者音视频文件等大文件存储。
Nginx是一款高性能的Web服务器,也可以用来作为负载均衡服务器。在FastDFS中,我们可以使用Nginx作为文件的访问入口,实现文件的负载均衡和高可用。
本文将详细讲解在Centos7环境下如何配置FastDFS和Nginx实现分布式文件存储系统,并提供两个实例作为参考。
shell
yum install -y git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon/
./make.sh
./make.sh install
cd ..
shell
git clone https://github.com/happyfish100/fastdfs.git
cd fastdfs/
./make.sh
./make.sh install
cd ..
shell
mkdir /fastdfs/tracker
cp fastdfs/conf/tracker.conf.sample /etc/fdfs/tracker.conf
vi /etc/fdfs/tracker.conf
修改以下内容:
```ini
base_path=/fastdfs/tracker
tracker_server=127.0.0.1:22122
# 修改成自己tracker服务器IP或hostname地址和端口号
allow_hosts=127.0.0.1
# 允许访问的客户端IP,默认只允许本地访问
port=22122
# 默认tracker端口号
```
shell
mkdir -p /fastdfs/storage/data
mkdir -p /fastdfs/storage/logs
cp fastdfs/conf/storage.conf.sample /etc/fdfs/storage.conf
vi /etc/fdfs/storage.conf
修改以下内容:
```ini
base_path=/fastdfs/storage
tracker_server=127.0.0.1:22122
# 修改成自己tracker服务器IP或hostname地址和端口号
store_path0=/fastdfs/storage/data
# 数据存储路径
log_file=/fastdfs/storage/logs/storage.log
# 日志文件路径
port=23000
# 默认storage端口号,可使用多个storage节点,需注意端口号不能重复
```
shell
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
shell
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/test.jpg
返回结果为:
group1/M00/00/00/wKgNAl5_MJOAavfuAABaLmLAaqA711.jpg
shell
/usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKgNAl5_MJOAavfuAABaLmLAaqA711.jpg /root/test_download.jpg
文件会下载到/root/目录下。
shell
yum install -y epel-release
yum install -y nginx
shell
vi /etc/nginx/nginx.conf
添加以下内容:
```nginx
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
location / {
proxy_pass http://fastdfs;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
include /etc/nginx/conf.d/*.conf;
}
```
shell
vi /etc/nginx/conf.d/fastdfs.conf
添加以下内容:
nginx
upstream fastdfs {
server 127.0.0.1:8888;
# 修改成FastDFS tracker所在服务器IP和端口号
}
shell
systemctl start nginx
shell
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/test.jpg
返回结果为:
group1/M00/00/00/wKgNAl5_MJOAavfuAABaLmLAaqA711.jpg
首先配置FastDFS和Nginx,可参考上面的内容。
然后上传需要访问的静态文件到FastDFS中,执行以下命令:
shell
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/static/index.html
返回一个文件的URL地址,例如:
group1/M00/00/00/wKgNAl5_T9GAAvfuAABaLmLAaxE757.html
shell
vi /etc/nginx/conf.d/static.conf
添加以下内容:
nginx
location /static/ {
proxy_pass http://fastdfs;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
首先配置FastDFS和Nginx,可参考上面的内容。
然后使用一个图床工具,例如PicGo,上传图片到FastDFS中。
在Nginx虚拟主机中配置图片访问路径,执行以下命令:
shell
vi /etc/nginx/conf.d/pic.conf
添加以下内容:
nginx
location /pic/ {
proxy_pass http://fastdfs;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
本文链接:http://task.lmcjl.com/news/14331.html