FastDFS安裝與nginx反向代理配置

操作系統 Ubuntu Server

nginx相關軟件

nginx-1.10.1
http://nginx.org/en/download.html
nginx清除緩存模塊 ngx_cache_purge-2.3
http://labs.frickle.com/nginx_ngx_cache_purge/
pcre-8.36
https://sourceforge.net/projects/pcre/files/pcre/8.36/
zlib庫
http://zlib.net/zlib-1.2.8.tar.gz

FastDFS相關軟件

FastDFS_v5.08
https://sourceforge.net/projects/fastdfs/files/FastDFS%20Server%20Source%20Code/stats/timeline
或者https://github.com/happyfish100/fastdfs
FastDFS Nginx Module 1.16
http://sourceforge.net/projects/fastdfs/files/FastDFS%20Nginx%20Module%20Source%20Code/
或者https://github.com/happyfish100/fastdfs-nginx-module
libfastcommon
https://github.com/happyfish100/libfastcommon
其中github上的代碼是最新的,sourceforge上不一定是最新的,當前FastDFS版本爲5.08。
網絡拓撲圖

tracker的安裝與配置

以下操作是在 tracker 10.10.10.80上操作。
通過rz工具或者FileZilla,把軟件上傳到Ubuntu的/usr/local/src目錄下。

安裝libfastcommon

root@tracker:/usr/local/src# unzip libfastcommon-master.zip 
root@tracker:/usr/local/src# cd libfastcommon-master
root@tracker:/usr/local/src/libfastcommon-master# ls
HISTORY  INSTALL  libfastcommon.spec  make.sh  php-fastcommon  README  src
root@tracker:/usr/local/src/libfastcommon-master# ./make.sh 
root@tracker:/usr/local/src/libfastcommon-master# ./make.sh install

安裝FastDFS (FastDFS_v5.08.tar.gz)

root@tracker:/usr/local/src# tar zxvf FastDFS_v5.08.tar.gz 
root@tracker:/usr/local/src# cd FastDFS
root@tracker:/usr/local/src/FastDFS# ./make.sh
root@tracker:/usr/local/src/FastDFS# ./make.sh install 

到這裏FastDFS在 tracker上安裝完成,所有的可執行文件位於 /usr/bin 目錄下,以fdfs開頭的文件:

root@tracker:~$ ll /usr/bin/fdfs_*
-rwxr-xr-x 1 root root  379725 Jun 16 19:25 /usr/bin/fdfs_appender_test*
-rwxr-xr-x 1 root root  375174 Jun 16 19:25 /usr/bin/fdfs_appender_test1*
-rwxr-xr-x 1 root root  356800 Jun 16 19:25 /usr/bin/fdfs_append_file*
-rwxr-xr-x 1 root root  356994 Jun 16 19:25 /usr/bin/fdfs_crc32*
-rwxr-xr-x 1 root root  356827 Jun 16 19:25 /usr/bin/fdfs_delete_file*
-rwxr-xr-x 1 root root  357754 Jun 16 19:25 /usr/bin/fdfs_download_file*
-rwxr-xr-x 1 root root  357928 Jun 16 19:25 /usr/bin/fdfs_file_info*
-rwxr-xr-x 1 root root  377621 Jun 16 19:25 /usr/bin/fdfs_monitor*
-rwxr-xr-x 1 root root 1313605 Jun 16 19:25 /usr/bin/fdfs_storaged*
-rwxr-xr-x 1 root root  389327 Jun 16 19:25 /usr/bin/fdfs_test*
-rwxr-xr-x 1 root root  387896 Jun 16 19:25 /usr/bin/fdfs_test1*
-rwxr-xr-x 1 root root  539675 Jun 16 19:25 /usr/bin/fdfs_trackerd*
-rwxr-xr-x 1 root root  357728 Jun 16 19:25 /usr/bin/fdfs_upload_appender*
-rwxr-xr-x 1 root root  363268 Jun 16 19:25 /usr/bin/fdfs_upload_file*

所有的配置文件在 /etc/fdfs/ 目錄下:

root@tracker:~$ ll /etc/fdfs/
total 28
drwxr-xr-x   2 root root 4096 Jun 16 19:33 ./
drwxr-xr-x 117 root root 4096 Jun 16 19:44 ../
-rwxr-xr-x   1 root root 1463 Jun 16 19:32 client.conf.sample
-rwxr-xr-x   1 root root 7927 Jun 16 19:25 storage.conf.sample
-rwxr-xr-x   1 root root 7202 Jun 16 19:33 tracker.conf.sample

配置

①進入到 /etc/fdfs 目錄下,將tracker.conf.sample重命名爲tracker.conf。

root@tracker:/etc/fdfs# mv tracker.conf.sample tracker.conf

②創建數據文件和日誌文件目錄

root@tracker:/etc/fdfs# mkdir -pv /data/fastdfs/tracker
mkdir: 已創建目錄 "/data"
mkdir: 已創建目錄 "/data/fastdfs"
mkdir: 已創建目錄 "/data/fastdfs/tracker"

③編輯 tracker.conf 文件,測試的時候只需要修改以下參數即可

disabled=false                        #啓用配置文件
port=22122                            #設置 tracker 的端口號
base_path=/data/fastdfs/tracker       #設置 tracker 的數據文件和日誌目錄(需預先創建)
http.server_port=8888                 #設置 http 端口號
這個http.server_port=8888 指的是在tracker服務器上啓動http服務進程,如:apache或者nginx 啓動時所監聽的端口

運行

直接使用 fdfs_trackerd 來啓動tracker進程,然後使用netstat 查看端口是否起來。

root@tracker:/etc/fdfs# fdfs_trackerd /etc/fdfs/tracker.conf restart
root@tracker:/etc/fdfs# netstat -antp | grep trackerd
tcp   0  0 0.0.0.0:22122   0.0.0.0:*   LISTEN   14520/fdfs_trackerd 
root@tracker:/etc/fdfs# 

也可以查看日誌看服務是否正常運行:

root@tracker:~# cat /data/fastdfs/tracker/logs/trackerd.log
[2016-06-28 11:55:55] INFO - FastDFS v5.08, base_path=/data/fastdfs/tracker, run_by_group=, run_by_user=, connect_timeout=30s, network_timeout=60s, port=22122, bind_addr=, max_connections=2048, accept_threads=1, work_threads=4, store_lookup=2, store_group=, store_server=0, store_path=0, reserved_storage_space=10.00%, download_server=0, allow_ip_count=-1, sync_log_buff_interval=10s, check_active_interval=120s, thread_stack_size=64 KB, storage_ip_changed_auto_adjust=1, storage_sync_file_max_delay=86400s, storage_sync_file_max_time=300s, use_trunk_file=0, slot_min_size=256, slot_max_size=16 MB, trunk_file_size=64 MB, trunk_create_file_advance=0, trunk_create_file_time_base=02:00, trunk_create_file_interval=86400, trunk_create_file_space_threshold=20 GB, trunk_init_check_occupying=0, trunk_init_reload_from_binlog=0, trunk_compress_binlog_min_interval=0, use_storage_id=0, id_type_in_filename=ip, storage_id_count=0, rotate_error_log=0, error_log_rotate_time=00:00, rotate_error_log_size=0, log_file_keep_days=0, store_slave_file_use_link=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s
[2016-06-28 11:56:01] INFO - file: tracker_relationship.c, line: 383, selecting leader...
[2016-06-28 11:56:02] INFO - file: tracker_relationship.c, line: 401, I am the new tracker leader 10.10.10.80:22122

⑤設置開機自動啓動

root@tracker:/etc# vi rc.local 
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

至此tracker上的安裝與配置結束。

storage的安裝與配置

兩臺機器(10.10.10.81, 10.10.10.82)配置完全一樣,以81爲例。

安裝

利用上傳 FastDFS_v5.08.tar.gz 和 libfastcommon-master.zip 至 storage服務器的/usr/local/src 目錄下,解壓安裝 libfastcommon,這個和前面安裝tracker一樣。

安裝libfastcommon
root@storage1:/usr/local/src# unzip libfastcommon-master.zip 
root@storage1:/usr/local/src# cd libfastcommon-master
root@storage1:/usr/local/src/libfastcommon-master# ls
HISTORY  INSTALL  libfastcommon.spec  make.sh  php-fastcommon  README  src
root@storage1:/usr/local/src/libfastcommon-master# ./make.sh 
root@storage1:/usr/local/src/libfastcommon-master# ./make.sh install
安裝FastDFS
FastDFS_v5.08.tar.gz
root@storage1:/usr/local/src# tar zxvf FastDFS_v5.08.tar.gz 
root@storage1:/usr/local/src# cd FastDFS
root@storage1:/usr/local/src/FastDFS# ./make.sh
root@storage1:/usr/local/src/FastDFS# ./make.sh install 

配置

進入到 /etc/fdfs 目錄下,複製/usr/local/src/FastDFS/conf/* 下面的所有文件到當前目錄下(/etc/fdfs):

root@storage1:/etc/fdfs$ ll
total 96
drwxr-xr-x  2 root root  4096 Jun 17 16:07 ./
drwxr-xr-x 97 root root  4096 Jun 17 15:28 ../
-rwxr-xr-x  1 root root 23981 Jun 16 20:06 anti-steal.jpg
-rwxr-xr-x  1 root root  1461 Jun 16 20:06 client.conf
-rwxr-xr-x  1 root root   858 Jun 16 20:06 http.conf
-rwxr-xr-x  1 root root 31172 Jun 16 20:06 mime.types
-rwxr-xr-x  1 root root  7910 Jun 17 09:40 storage.conf
-rwxr-xr-x  1 root root   105 Jun 16 20:06 storage_ids.conf
-rwxr-xr-x  1 root root  7202 May 20 10:45 tracker.conf

編輯配置文件 storage.conf 測試的時候,只需修改以下內容即可

root@storage1:/etc/fdfs$ vi storage.conf 
disabled=false                        #啓用配置文件
group_name=group1                     #組名,根據實際情況修改
port=23000                            #設置 storage 的端口號
base_path=/data/fastdfs/storage       #設置 storage 的日誌目錄(需預先創建)
store_path_count=1                    #存儲路徑個數,需要和 store_path 個數匹配
store_path0=/data/fastdfs/storage     #存儲路徑
tracker_server=10.10.10.81:22122     #tracker 服務器的 IP 地址和端口號
http.server_port=8888                #設置storage上啓動的http服務的端口號,如安裝的nginx的端口號

[備註]:store_path_count如果有多個,那麼需要設置多個存儲路徑,如:

store_path_count=2
store_path0=/data/fastdfs/storage
store_path1=/data/fastdfs/storage1

運行

執行如下命令:

root@storage1:/etc/fdfs# fdfs_storaged /etc/fdfs/storage.conf restart

查看進程是否運行:

root@dstorage1:/etc/fdfs# netstat -antp | grep storage
tcp  0  0 0.0.0.0:23000   0.0.0.0:*   LISTEN      23426/fdfs_storaged

觀察日誌 /data/fastdfs/storage/logs/storaged.log 看運行情況

mkdir data path: FB ...
mkdir data path: FC ...
mkdir data path: FD ...
mkdir data path: FE ...
mkdir data path: FF ...
data path: /data/fastdfs/storage/data, mkdir sub dir done.
[2016-06-16 19:51:04] INFO - file: storage_param_getter.c, line: 191, use_storage_id=0, id_type_in_filename=ip, storage_ip_changed_auto_adjust=1, store_path=0, reserved_storage_space=10.00%, use_trunk_file=0, slot_min_size=256, slot_max_size=16 MB, trunk_file_size=64 MB, trunk_create_file_advance=0, trunk_create_file_time_base=02:00, trunk_create_file_interval=86400, trunk_create_file_space_threshold=20 GB, trunk_init_check_occupying=0, trunk_init_reload_from_binlog=0, trunk_compress_binlog_min_interval=0, store_slave_file_use_link=0
[2016-06-16 19:51:04] INFO - file: storage_func.c, line: 254, tracker_client_ip: 10.10.10.81, my_server_id_str: 10.10.10.81, g_server_id_in_filename: -771094006
[2016-06-16 19:51:04] INFO - file: tracker_client_thread.c, line: 310, successfully connect to tracker server 10.10.10.80:22122, as a tracker client, my ip is 10.10.10.81
[2016-06-16 19:51:34] INFO - file: tracker_client_thread.c, line: 1235, tracker server 10.10.10.80:22122, set tracker leader: 10.10.10.80:22122

這裏看到,創建了二級目錄,成功連接到tracker。
可以使用 fdfs_monitor 來查看一下storage的狀態,看是否已經成功註冊到了tracker:

root@storage1:/# fdfs_monitor /etc/fdfs/storage.conf 
或者:
fdfs_monitor /etc/fdfs/client.conf(不過這個文件需要配置一下才行)
[2016-06-28 14:32:41] DEBUG - base_path=/data/fastdfs/storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

server_count=1, server_index=0

tracker server is 10.10.10.80:22122

group count: 1

Group 1:
group name = group1
disk total space = 7384 MB
disk free space = 4306 MB
trunk free space = 0 MB
storage server count = 1
active server count = 1
storage server port = 23000
storage HTTP port = 10000
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0

        Storage 1:
                id = 10.10.10.81
                ip_addr = 10.10.10.81  ACTIVE
                http domain = 
                version = 5.08
                join time = 2016-06-16 20:49:05
                up time = 2016-06-17 15:41:37
                total storage = 19211 MB
        。。。。。

看到這裏顯示“ACTIVE”,說明當前的storage節點已經註冊到了Tracker上。

設置開機啓動

同Tracker中的配置,把如下命令加到/etc/rc.local中:

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

到這裏storage的安裝配置也完成了。當第二臺節點也加進來後,可以通過日誌看到如下信息:

[2016-06-17 15:41:46] INFO - file: storage_sync.c, line: 2698, successfully connect to storage server 10.10.10.82:23000

【注】因爲同一組的節點會進行數據同步,所以一個組內的所有storage節點會進行通信,那麼這個地方就可以看出來,他們上線後會建立連接。
另外,從端口信息上也可以看出來:

root@storage1:/# netstat -antp | grep storage
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      23426/fdfs_storaged
tcp        0      0 10.10.10.81:23000      10.10.10.82:33842      ESTABLISHED 23426/fdfs_storaged
tcp        0      0 10.10.10.81:50824      10.10.10.82:23000      ESTABLISHED 23426/fdfs_storaged
tcp        0      0 10.10.10.81:58116      10.10.10.80:22122      ESTABLISHED 23426/fdfs_storaged

storage1會與storage2建立連接,同時會與tracker建立連接。

在storage上安裝配置nginx

以下操作在storage1上完成。

安裝nginx

上傳 fastdfs-nginx-module_v1.16.tar.gz nginx-1.10.0.tar.gz pcre-8.36.tar.gz zlib-1.2.8.tar.gz 到storage服務器上的/usr/local/src 目錄下

root@storage1:/usr/local/src/nginx-1.10.0# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
root@storage1:/usr/local/src/nginx-1.10.0# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/fastdfs-nginx-module-master/src --with-pcre=/usr/local/src/pcre-8.36 --with-zlib=/usr/local/src/zlib-1.2.8

編譯安裝:

make && make install 

配置nginx

修改nginx配置文件
① 修改nginx的監聽端口
將 server 段中的 listen 端口號改爲 8888:
② 在 server 段中添加fastdfs的配置:
這裏的group[1-3] 可以匹配 group1,group2,group3,當然group也可以是其他的名字,這裏正則表達式只是用來過濾請求路徑。
location ~ /group[1-3]/M00 {
root /data/fastdfs/storage/data;
ngx_fastdfs_module;
}
/data/fastdfs/storage/data 這個是指向真正存儲文件的地方,該目錄下有這些個二級目錄

root@dtv-210:/data/fastdfs/storage/data# ll
total 1048
drwxr-xr-x 259 root root 4096 Jun 27 15:42 ./
drwxr-xr-x   4 root root 4096 Jun 16 19:50 ../
drwxr-xr-x 258 root root 4096 Jun 16 19:50 00/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 01/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 02/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 03/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 04/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 05/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 06/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 07/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 08/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 09/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 0A/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 0B/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 0C/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 0D/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 0E/
drwxr-xr-x 258 root root 4096 Jun 16 19:50 0F/
。。。

③將 FastDFS 的 nginx 插件模塊的配置文件拷貝到 FastDFS 配置文件目錄:

root@storage1:/usr/local/src/fastdfs-nginx-module-master/src# cp mod_fastdfs.conf /etc/fdfs/

修改 fastdfs的nginx模塊的配置文件 mod_fastdfs.conf

base_path=/data/fastdfs/storage      #保存日誌目錄
tracker_server=10.10.10.80:22122     #tracker 服務器的 IP 地址以及端口號
storage_server_port=23000            #storage服務器的端口號
group_name=group1                    #當前服務器的group名
url_have_group_name = true           #文件url中是否有group 名
store_path_count=1                   #存儲路徑個數,需要和store_path 個數匹配
store_path0=/data/fastdfs/storage    #存儲路徑
group_count = 1                      #設置組的個數
#然後在末尾添加分組信息,目前只有一個分組,就只寫一個
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs/storage

④建立 M00 至存儲目錄的符號連接

root@storage1# ln -s /data/fastdfs/storage/data /data/fastdfs/storage/data/M00
root@storage1# ll /data/fastdfs/storage/data/M00
lrwxrwxrwx 1 root root 26 Jun 16 19:53 /data/fastdfs/storage/data/M00 -> /data/fastdfs/storage/data/

到這裏,nginx和FastDFS 插件模塊就配置完成了。
⑤啓動nginx
執行/usr/local/nginx/sbin/nginx
把上述語句加入/etc/rc.local即可開機自啓動。
可以通過netstat -unltp | grep nginx查看nginx是否起來,如果有錯誤,可以查看日誌:

root@storage1:/usr/local/nginx/logs# cat error.log 

也可以訪問http://10.10.10.80:8888查看nginx是否工作,看到如下界面說明nginx啓動OK。
nginx
其他storage節點依照上述步驟安裝與配置即可。

安裝nginx反向代理及緩存服務器

在機器10.10.10.79上安裝。

安裝nginx

將所需的軟件包上傳至服務器/usr/local/src下

root@server:/usr/local/src# tar zxvf nginx-1.10.0.tar.gz 
root@server:/usr/local/src# tar zxvf ngx_cache_purge-2.3.tar.gz 
root@server:/usr/local/src# tar zxvf pcre-8.36.tar.gz 
root@server:/usr/local/src# tar zxvf zlib-1.2.8.tar.gz 
root@server:/usr/local/src# cd nginx-1.10.0
root@server:/usr/local/src/nginx-1.10.0#./configure  --prefix=/usr/local/nginx  --add-module=/usr/local/src/ngx_cache_purge-2.3  --with-pcre=/usr/local/src/pcre-8.36/  --with-zlib=/usr/local/src/zlib-1.2.8
root@server:/usr/local/src/nginx-1.10.0#make && make install

nginx 以及 nginx cache purge 插件模塊安裝完畢。

配置nginx

①創建nginx的緩存目錄

root@server# mkdir -pv /var/cache/nginx/proxy_cache/tmp

②增加系統打開文件數的限制,手寫到rc.local

root@server# ulimit -SHn 102400
root@server# echo "ulimit -SHn 102400" >> /etc/rc.local

③編輯 vim /usr/local/nginx/conf/nginx.conf 文件修改以下內容:

user  nobody;
worker_processes  4;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  65535;
    use epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 300m;
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    #設置緩存存儲路徑、存儲方式、分配內存大小、磁盤最大空間、緩存期限
    proxy_cache_path  /var/cache/nginx/proxy_cache  levels=1:2
    keys_zone=http-cache:500m max_size=10g inactive=30d;
    proxy_temp_path /var/cache/nginx/proxy_cache/tmp;
    #設置 group1 的服務器
    upstream fdfs_group1 {
        server 10.10.10.80:8888 weight=1 max_fails=2 fail_timeout=30s;
        server 10.10.10.81:8888 weight=1 max_fails=2 fail_timeout=30s;
    }
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server {
        listen       80;
        server_name  localhost;
        access_log  logs/host.access.log  main;
    #設置 group1 的反向代理參數
    location /group1/M00 {
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_cache http-cache;
        proxy_cache_valid 200 304 12h;
        proxy_cache_key $uri$is_args$args;
        proxy_pass http://fdfs_group1;
        expires 30d;
    }
    #設置清除緩存的訪問權限
        location ~ /purge(/.*) {
        allow 127.0.0.1;
        allow 10.10.10.0/24;
        deny all;
        proxy_cache_purge http-cache $1$is_args$args;
    } 
    }
}

接下來,可以使用 /usr/local/nginx/sbin/nginx -t 來測試配置文件語法的正確性。

root@server:/usr/local/nginx# ./sbin/nginx -t
ngx_http_fastdfs_set pid=5895
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
root@server:/usr/local/nginx# 

④啓動nginx並加入到開機啓動

/usr/local/nginx/sbin/nginx

把/usr/local/nginx/sbin/nginx加入/etc/rc.local即可。
同樣可以查看端口看是否正常啓動:

netstat -unltp | grep nginx

測試上傳文件

以下操作在tracker服務器(10.10.10.80)上進行
①創建client 使用的日誌目錄

root@server# mkdir -pv /data/fastdfs/client

②修改客戶端的配置文件/etc/fdfs/client.conf

base_path=/data/fastdfs/client #日誌存放路徑
tracker_server=10.10.10.80:22122 #tracker 服務器 IP 地址和端口號
http.tracker_server_port=8888

③上傳一個圖片

root@server# fdfs_upload_file /etc/fdfs/client.conf 1.jpg
group1/M00/00/00/CgoKeFRQqaaASddNAxEgRsu1UiU345.jpg
root@server#

得到了如下的地址:
group1/M00/00/00/CgoKeFRQqaaASddNAxEgRsu1UiU345.jpg
還可以通過fdfs_file_info 來查看上傳文件的信息

root@server# fdfs_file_info /etc/fdfs/client.conf 

通過瀏覽器訪問:
④使用瀏覽器訪問一下該地址:
http://10.10.10.79/group1/M00/00/00/CgoKeFRQqaaASddNAxEgRsu1UiU345.jpg
即可成功訪問到圖片。
⑤查看nginx的緩存目錄,可以查看緩存的內容。

root@server# ll -R /var/cache/nginx/proxy_cache/ 

⑥清除緩存
如果清除該圖片的緩存,可以在文件 URL 之前加上 purge 即可,如下圖
http://10.10.10.79/purge/group1/M00/00/00/CgoKeFRQqaaASddNAxEgRsu1UiU345.jpg
清除之後,當storage中文件被刪除後,將無法再請求到文件。
⑦配置瀏覽器端查看是否命中緩存
使用Chrome瀏覽器的開發工具,查看響應的頭部信息來查看是否命中緩存,要啓用此功能,修改nginx的主配置文件如下:
在location /group1/M00 段增加這2個行

add_header  X-Via  $server_addr;
add_header  X-Cache-Status $upstream_cache_status;

最終效果

location /group1/M00 {
    proxy_next_upstream http_502 http_504 error timeout invalid_header;
    proxy_cache http-cache;
    proxy_cache_valid 200 304 12h;
    proxy_cache_key $uri$is_args$args;
    proxy_pass http://fdfs_group1;
    expires 30d;
    add_header  X-Via  $server_addr;
    add_header  X-Cache-Status $upstream_cache_status;
}

重啓nginx,清除緩存再次訪問即可查看響應頭命中信息。

附錄

需要再多寫一點, FastDFS這樣部署就已經可以正常運行了,但是有一個問題,就是FastDFS本身不支持對上傳的文件去重。也就是說,同一個文件,我們反覆上傳,那麼會在Storage中保存多份,這顯然是不合理的。解決辦法,需要的請參考我另一篇文章:
《FastDHT配合FastDFS進行文件上傳去重》
http://blog.csdn.net/ForeverSunshine/article/details/51226061

發佈了36 篇原創文章 · 獲贊 7 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章