fastdfs+nginx+image_filter安裝與生成縮略圖


fastdfs簡介

  • 類似google FS的一個輕量級分布式文件系統,純C實現,支持linux、FreeBSD等UNIX系統;
  • 只能通過API訪問,不支持POXIS;
  • 文件不分塊存儲,上傳的文件和OS文件系統中的文件一一對應;
  • 下載文件支持http協議,可以使用內置web server,配置nginx模塊;
  • 文件存儲的適合范圍:大於4KB,小於500M;

 

   FastDFS服務端有兩個角色:跟蹤器(tracker)和存儲節點(storage)。

    Tracker Server:跟蹤服務器,主要做調度工作,在訪問上起到負載均衡的作用。在內存中記錄集群中group和storage server的信息,是連接client和storage servery的樞紐。tracker性能是非常高的,二三台tracker就可以管理上百個group。

    Storage Server:存儲服務器,文件和文件屬性都保存到存儲服務器上。

    所有的服務器是對等的,不存在mater-slave的關系,存儲服務器采用分組的方式,同組之間文件完全相同,raid1。不同組storage server不會通信,是有storage server主動向tracker server報告狀態,tracker server之間不會通信。

准備安裝

1、環境准備

由於機器有限,我這使用二台服務器做實驗,他們既做tracker,也做storage。

server01 IP:10.0.0.31

server02 IP:10.0.0.32

[root@nfs1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@nfs1 ~]# uname -r
3.10.0-327.el7.x86_64
[root@nfs1 ~]# getenforce 
Disabled
[root@nfs1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
#注意:磁盤空間留多一些,不然storage會報錯。

2、安裝fastdfs

安裝依賴

[root@nfs1 ~]# cd /usr/local/src/
[root@nfs1 ~]# git clone https://github.com/happyfish100/libfastcommon.git
[root@nfs1 ~]# cd libfastcommon/
[root@nfs1 ~]# ./make.sh
[root@nfs1 ~]# ./make.sh install
#make報錯需要安裝yum install gcc gcc-c++ make -y

安裝fastdfs,這次用的版本是5.0.4

[root@nfs1 ~]# cd /usr/local/src/
[root@nfs1 ~]# wget https://github.com/happyfish100/fastdfs/archive/V5.04.tar.gz
[root@nfs1 ~]# tar xf V5.04.tar.gz 
[root@nfs1 ~]# cd fastdfs-5.04/
[root@nfs1 ~]# ./make.sh 
[root@nfs1 ~]# ./make.sh install

這個版本的坑(啟動命令中路徑不對):

[root@nfs1 ~]# sed -i 's#/usr/local/bin#/usr/bin#g' /etc/init.d/fdfs_trackerd
[root@nfs1 ~]# sed -i 's#/usr/local/bin#/usr/bin#g' /etc/init.d/fdfs_storaged
#需要將/usr/local/bin修改為/usr/bin

3、配置環境

tracker配置文件

[root@nfs1 ~]# cd /etc/fdfs/
[root@nfs1 ~]# cp tracker.conf.sample  tracker.conf
[root@nfs1 ~]# mkdir /data/fdfs/tracker -p
[root@nfs1 ~]# vim tracker.conf
......
22 base_path=/data/fdfs/tracker
......
[root@nfs1 fdfs]# /etc/init.d/fdfs_trackerd start

storage配置文件

[root@nfs1 fdfs]# cd /etc/fdfs
[root@nfs1 fdfs]# mkdir /data/fdfs/storage/base -p
[root@nfs1 fdfs]# mkdir /data/fdfs/storage/store -p
[root@nfs1 fdfs]# cp storage.conf.sample storage.conf
[root@nfs1 fdfs]# vim storage.conf
...
 37 base_path=/data/fdfs/storage/base
105 store_path0=/data/fdfs/storage/store
...
114 tracker_server=10.0.0.31:22122
115 tracker_server=10.0.0.32:22122
...
[root@nfs1 fdfs]# /etc/init.d/fdfs_storaged start

client配置文件

[root@nfs1 fdfs]# mkdir /data/fdfs/client
[root@nfs1 fdfs]# cp client.conf.sample client.conf
[root@nfs1 fdfs]# vim client.conf
...
 10 base_path=/data/fdfs/client
 14 tracker_server=10.0.0.31:22122
 15 tracker_server=10.0.0.32:22122
...

 好的,配置成功了,參數我在這里不解釋了,可參看http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1941456&extra=page%3D1%26filter%3Ddigest%26digest%3D1

4、使用操作

[root@nfs1 fdfs]# /etc/init.d/fdfs_trackerd status
[root@nfs1 fdfs]# /etc/init.d/fdfs_storaged status
#在操作前我們可以看看這兩個服務是不是起來了,如果沒有起來,查看日志
[root@nfs1 fdfs]# tailf /data/fdfs/tracker/logs/trackerd.log
[root@nfs1 fdfs]# tailf /data/fdfs/storage/base/logs/storaged.log
##############我是分割線###################
[root@nfs1 fdfs]# fdfs_monitor /etc/fdfs/client.conf
server_count=2, server_index=1
 
tracker server is 10.0.0.32:22122
 
group count: 1
 
Group 1:
group name = group1
disk total space = 28657 MB
disk free space = 26333 MB
trunk free space = 0 MB
storage server count = 2
active server count = 2
storage server port = 23000
storage HTTP port = 8888
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0
 
    Storage 1:
        id = 10.0.0.31
        ip_addr = 10.0.0.31 (nfs1)  ACTIVE
...
    Storage 2:
        id = 10.0.0.32
        ip_addr = 10.0.0.32  ACTIVE

#上傳文件測試 [root@nfs1
~]# cd [root@nfs1 ~]# fdfs_upload_file /etc/fdfs/client.conf /etc/resolv.conf group1/M00/00/00/CgAAH1jexOiAKDWjAAAAMjpGoJk87.conf #下載文件測試 [root@nfs1 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/CgAAH1jexOiAKDWjAAAAMjpGoJk87.conf [root@nfs1 ~]# ll total 8 -rw-------. 1 root root 1240 Mar 11 17:27 anaconda-ks.cfg -rw-r--r-- 1 root root 50 Apr 1 05:07 CgAAH1jexOiAKDWjAAAAMjpGoJk87.conf [root@nfs1 ~]# cat CgAAH1jexOiAKDWjAAAAMjpGoJk87.conf # Generated by NetworkManager nameserver 10.0.0.2

5、常用命令

#可以查看命令幫助
fdfs_upload_file
#上傳文件
fdfs_upload_file /etc/fdfs/client.conf file
#下載文件
fdfs_download_file
#查看信息
fdfs_file_info
#刪除文件
fdfs_delete_file
#添加追加文件
fdfs_upload_appender /etc/fdfs/client.conf appent.txt 
#追加內容
fdfs_append_file /etc/fdfs/client.conf file_id appent2.txt
#監控狀態
fdfs_monitor /etc/fdfs/client.conf
#所有的命令都是需要接上配置文件的。

安裝nginx模塊

        我們在使用FastDFS部署一個分布式文件系統的時候,通過FastDFS的客戶端API來進行文件的上傳、下載、刪除等操作。同時通過FastDFS的HTTP服務器來提供HTTP服務。但是FastDFS的HTTP服務較為簡單,無法提供負載均衡等高性能的服務,所以FastDFS的開發者為我們提供了Nginx上使用的FastDFS模塊(也可以叫FastDFS的Nginx模塊),其使用非常簡單。
    FastDFS通過Tracker服務器,將文件放在Storage服務器存儲,但是同組之間的服務器需要復制文件,有延遲的問題.假設Tracker服務器將文件上傳到了10.0.0.31,文件ID已經返回客戶端,這時,后台會將這個文件復制到10.0.0.32,如果復制沒有完成,客戶端就用這個ID在10.0.0.32取文件,肯定會出現錯誤。這個fastdfs-nginx-module可以重定向連接到源服務器取文件,避免客戶端由於復制延遲的問題,出現錯誤。

        #fastdfs-nginx-module下載地址:鏈接:https://pan.baidu.com/s/1fngCkQeWKW6HiCdDwyRK8Q 密碼:v85v

將下好的fastdfs-nginx-module模塊上傳至服務器,解壓,然后安裝nginx

1、查看nginx和fastdfs模塊
[root@storege01-34 conf]# cd /usr/local/src/ [root@storege01-34 src]# ll -rw-r--r-- 1 root root 17510 Aug 23 17:32 fastdfs-nginx-module_v1.16.tar.gz -rw-r--r-- 1 root root 990836 Nov 21 2017 nginx-1.13.7.tar.gz
2、解壓fastdfs模塊及修改fastdfs模塊的內容 [root@nfs1
~]# tar xf fastdfs-nginx-module_v1.16.tar.gz #需要修改 fastdfs-nginx-module/src/config , [root@nfs1 ~]# vim /usr/local/src/fastdfs-nginx-module/src/config #把 CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/" 改為 CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
3、安裝nginx依賴包 [root@nfs1
~]# yum install -y pcre pcre-devel openssl openssl-devel gd-devel
4、編譯nginx [root@nfs1 tools]#
tar xf nginx-1.8.1.tar.gz [root@nfs1 tools]# cd nginx-1.8.1/ [root@nfs1 nginx-1.8.1]# useradd -M -s /sbin/nologin www #沒有用戶的添加 [root@nfs1 nginx-1.8.1]# ./configure --prefix=/usr/local/nginx-1.8.1 --user=www --group=www --with-http_ssl_module --with-http_image_filter_module --add-module=/usr/local/src/fastdfs-nginx-module/src/ [root@nfs1 nginx-1.8.1]# make && make install

5、復制fastdfs配置文件 [root@nfs1 nginx
-1.8.1]# cd /usr/local/src/fastdfs-nginx-module/src [root@nfs1 src]# cp mod_fastdfs.conf /etc/fdfs/ [root@nfs1 src]# cd /usr/local/src/fastdfs-5.04/conf/ [root@nfs1 conf]# cp anti-steal.jpg http.conf mime.types /etc/fdfs/ #日志文件

6、創建日志文件 [root@nfs1
~]# touch /var/log/mod_fastdfs.log [root@nfs1 ~]# chown www.www /var/log/mod_fastdfs.log
7、修改nginx配置文件
[root@nfs1
~]# cd /usr/local/nginx/conf/ [root@nfs1 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf [root@nfs1 conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name 10.0.0.31; location /group1/M00 { root /data/fdfs/storage/store/; ngx_fastdfs_module; } location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

8、修改配置文件后檢查nginx配置及重啟nginx [root@nfs1 conf]#
/usr/local/nginx/sbin/nginx -t ngx_http_fastdfs_set pid=127697 [root@nfs1 conf]# vim /etc/fdfs/mod_fastdfs.conf 40 tracker_server=10.0.0.31:22122 41 tracker_server=10.0.0.32:22122 54 url_have_group_name = true 63 store_path0=/data/fdfs/storage/store/ 79 log_filename=/var/log/mod_fastdfs.log #重啟Nginx [root@nfs1 fdfs]# /usr/local/nginx/sbin/nginx -s stop ngx_http_fastdfs_set pid=127854 [root@nfs1 fdfs]# /usr/local/nginx/sbin/nginx ngx_http_fastdfs_set pid=127855

nginx的http_image_filter_module模塊說明

        http_image_filter_module是nginx提供的集成圖片處理模塊,支持nginx-0.7.54以后的版本,在網站訪問量不是很高磁盤有限不想生成多余的圖片文件的前提下可,就可以用它實時縮放圖片,旋轉圖片,驗證圖片有效性以及獲取圖片寬高以及圖片類型信息。

http_image_filter_module模塊的參數說明

image_filter off;
#關閉模塊


image_filter test;
#確保圖片是jpeg gif png否則返415錯誤


image_filter size;
#輸出有關圖像的json格式:例如以下顯示{ "img" : { "width": 100, "height": 100, "type": "gif" } } 出錯顯示:{}


image_filter rotate 90|180|270;
#旋轉指定度數的圖像,參數能夠包括變量,單獨或一起與resize crop一起使用。


image_filter resize width height;
#按比例降低圖像到指定大小,公降低一個能夠還有一個用"-"來表示,出錯415,參數值可包括變量,能夠與rotate一起使用,則兩個一起生效。


image_filter crop width height;
#按比例降低圖像比較大的側面積和還有一側多余的載翦邊緣,其他和rotate一樣。沒太理解


image_filter_buffer 10M;
#設置讀取圖像緩沖的最大大小,超過則415錯誤。


image_filter_interlace on;
#假設啟用,終於的圖像將被交錯。對於JPEG,終於的圖像將在“漸進式JPEG”格式。


image_filter_jpeg_quality 95;
#設置變換的JPEG圖像的期望質量。可接受的值是從1到100的范圍內。較小的值通常意味着既降低圖像質量,降低數據傳輸,推薦的最大值為95。參數值能夠包括變量。


image_filter_sharpen 100;
#添加了終於圖像的清晰度。銳度百分比能夠超過100。零值將禁用銳化。參數值能夠包括變量。


image_filter_transparency on;
#定義是否應該透明轉換的GIF圖像或PNG圖像與調色板中指定的顏色時,能夠保留。透明度的損失將導致更好的圖像質量。在PNG的Alpha通道總是保留透明度。

再前面編譯nginx已經制定了http_image_filter_module模塊了,http_image_filter_module模塊依賴gd-devel包所以需要安裝gd-devel包(前面也已經安裝了)

 nginx實現圖片縮略圖的配置如下:

# cat /usr/local/nginx/conf/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /usr/local/nginx/logs/error.log;
pid /run/nginx.pid;

 /usr/share/nginx/README.dynamic.

events {
    worker_connections 1024;
}

http {
    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  /usr/local/nginx/logs/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /usr/local/nginx-1.13.7/conf/mime.types;
    default_type        application/octet-stream;

    server {
        listen       80;
        server_name  localhost;
        root         /data/nginx/resource;
    location ~* \.(eot|ttf|woff|woff2|svg|otf)$ {
            root /data/nginx/resource/;
            add_header Access-Control-Allow-Origin *;
        }
        location static/* {
            root /data/nginx/resource/;
        }
        location ~ group1/M00/(.+)_([0-9]+)x([0-9]+)\.(jpg|gif|png) {           #這段往下為生成縮略圖的配置
            alias /data0/fastdfs/storage/storage0/data;
            ngx_fastdfs_module;
            set $w $2;
            set $h $3;           
 
            if ($w != "0") {
                rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ group1/M00$1.$4 break;
            }
 
            if ($h != "0") {
                rewrite group1/M00(.+)_(\d+)x(\d+)\.(jpg|gif|png)$ group1/M00$1.$4 break;
            }
    
            image_filter resize $w $h;
              
            image_filter_buffer 2M;
 
            try_files group1/M00$1.$4 $1.jpg;
      }
                            
                             
        location ~ group1/M00/(.+)\.?(.+){
             alias /data0/fastdfs/storage/storage0/data;
             ngx_fastdfs_module;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

配置后重啟nginx!!!

# /usr/local/nginx/sbin/nginx -s reload

往fastdfs中上傳圖片

# fdfs_upload_file /etc/fdfs/client.conf test.png
group1/M00/00/00/wKgCEVvGzDSAJROvAAHgQeZ5IGY364.png

訪問圖片

1、訪問原圖:www.71net.com/group1/M00/00/00/wKgCEVvGzDSAJROvAAHgQeZ5IGY364.png

        其中www.71net.com為你的域名或IP地址

2、訪問縮略圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM