原文地址:Nginx 整合 FastDFS 实现文件服务器
博客地址:http://www.extlight.comnginx
本篇衔接《FastDFS 环境搭建》内容进行讲解,上篇文章咱们实现了图片上传的功能,可是没法经过 http 进行访问,本篇将解决该问题。git
建议读者先浏览《FastDFS 环境搭建》再继续浏览本篇文章。github
Nginx 整合 FastDFS 须要依赖 fastdfs-nginx-module 模块,咱们须要将其下载下来。vim
git clone https://github.com/happyfish100/fastdfs-nginx-module.git
查看下载文件:浏览器
[root@fastdfs ~]# pwd /root [root@fastdfs ~]# ll total 1048 drwxr-xr-x. 11 root root 270 Oct 16 22:19 fastdfs drwxr-xr-x. 4 root root 59 Oct 17 02:56 fastdfs-nginx-module drwxr-xr-x. 6 root root 147 Oct 16 22:22 libfastcommon -rw-r--r--. 1 root root 49608 Oct 17 01:51 test.jpg
笔者经过 yum 方式安装 nginx,具体操做能够访问 《Nginx 快速入门》。服务器
如今出现一个尴尬的问题:nginx 安装第三方模块须要经过 configure 命令,所以咱们还得下载 nginx 源码对第三方模块进行编译安装。app
因为笔者已经提早安装 nginx ,键入命令:dom
[root@fastdfs ~]# nginx -V nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
能够看到 nginx 的版本以及安装好的 nginx 模块,咱们须要下载 nginx/1.14.0 的源码:测试
wget http://nginx.org/download/nginx-1.14.0.tar.gz tar -zxvf nginx-1.14.0.tar.gz cd nginx-1.14.0
进入到 nginx-1.14.0 目录后,咱们就看到 configure 命令了。ui
经过源码编译 nginx 模块须要安装依赖包(若是以前经过源码方式安装 nginx 能够忽略该步骤):
yum install pcre yum install pcre-devel yum install zlib yum install zlib-devel yum install openssl yum install openssl-devel
安装完成后,咱们如今须要拼接执行命令。
即 ./configure 已安装的模块 --add-module=/root/fastdfs-nginx-module/src,最终的结果以下:
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/root/fastdfs-nginx-module/src
执行上边的命令,编译好以后再执行 make 便可。注意,不须要 make install
最后替换 nginx 命令:
cp /usr/sbin/nginx /usr/sbin/nginx.bak cp ./objs/nginx /usr/sbin/
复制 mod_fastdfs 配置文件:
cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
修改 vim /etc/fdfs/mod_fastdfs.conf:
base_path=/data/fastdfs/tmp tracker_server=192.168.10.110:22122 group_name=light url_have_group_name = true store_path0=/data/fastdfs/storage
注意:请确保 /etc/fdfs 有以下 .conf 文件:
[root@fastdfs fdfs]# pwd /etc/fdfs [root@fastdfs fdfs]# ll total 84 -rw-r--r--. 1 root root 1462 Oct 17 01:50 client.conf -rw-r--r--. 1 root root 1461 Oct 16 22:26 client.conf.sample -rw-r--r--. 1 root root 955 Oct 17 04:34 http.conf -rw-r--r--. 1 root root 31172 Oct 17 04:35 mime.types -rw-r--r--. 1 root root 3744 Oct 17 04:22 mod_fastdfs.conf -rw-r--r--. 1 root root 7976 Oct 17 01:57 storage.conf -rw-r--r--. 1 root root 7978 Oct 16 22:26 storage.conf.sample -rw-r--r--. 1 root root 105 Oct 16 22:26 storage_ids.conf.sample -rw-r--r--. 1 root root 7442 Oct 16 23:34 tracker.conf -rw-r--r--. 1 root root 7441 Oct 16 22:26 tracker.conf.sample
缺乏配置文件,可能会形成后边启动 nginx 失败,若是根据笔者介绍的步骤,应该不会出现缺漏文件的状况。
vim /etc/nginx/conf.d/default.conf ,新增 location 配置:
server { server_name 192.168.10.110; location /light/M00 { root /data/fastdfs/storage/data; ngx_fastdfs_module; } ... }
保存文件,重启 nginx :
nginx -s reload
打开浏览器访问 http://192.168.10.110/light/M00/00/00/wKgKblvG1hmAafZBAADByGKuVUM369.jpg (上篇文章中笔者上传图片后 fdfs 返回的 url 地址),显示效果:
图片正常访问,大功告成。
若是访问不通,检查是不是防火墙问题,或者查看 nginx 日志。