Nginx 整合 FastDFS 實現文件服務器

原文地址:Nginx 整合 FastDFS 實現文件服務器
博客地址:http://www.extlight.comnginx

1、前言

本篇銜接《FastDFS 環境搭建》內容進行講解,上篇文章咱們實現了圖片上傳的功能,可是沒法經過 http 進行訪問,本篇將解決該問題。git

建議讀者先瀏覽《FastDFS 環境搭建》再繼續瀏覽本篇文章。github

2、安裝

2.1 下載 fastdfs-nginx-module

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

2.2 安裝 nginx/fastdfs-nginx-module

筆者經過 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/

3、配置

3.1 修改 fastdfs-nginx-module 配置文件

複製 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 失敗,若是根據筆者介紹的步驟,應該不會出現缺漏文件的狀況。

3.2 修改 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

4、測試

打開瀏覽器訪問 http://192.168.10.110/light/M00/00/00/wKgKblvG1hmAafZBAADByGKuVUM369.jpg (上篇文章中筆者上傳圖片後 fdfs 返回的 url 地址),顯示效果:

image

圖片正常訪問,大功告成。

若是訪問不通,檢查是不是防火牆問題,或者查看 nginx 日誌。

相關文章
相關標籤/搜索