FastDFS安装与nginx反向代理配置

来源:互联网 发布:数据库中的存储过程 编辑:程序博客网 时间:2024/05/22 18:24

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-masterroot@tracker:/usr/local/src/libfastcommon-master# lsHISTORY  INSTALL  libfastcommon.spec  make.sh  php-fastcommon  README  srcroot@tracker:/usr/local/src/libfastcommon-master# ./make.sh root@tracker:/usr/local/src/libfastcommon-master# ./make.sh install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

root@tracker:/usr/local/src# tar zxvf FastDFS_v5.08.tar.gz root@tracker:/usr/local/src# cd FastDFSroot@tracker:/usr/local/src/FastDFS# ./make.shroot@tracker:/usr/local/src/FastDFS# ./make.sh install 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

到这里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*
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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

root@tracker:~$ ll /etc/fdfs/total 28drwxr-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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

配置

①进入到 /etc/fdfs 目录下,将tracker.conf.sample重命名为tracker.conf。

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

②创建数据文件和日志文件目录

root@tracker:/etc/fdfs# mkdir -pv /data/fastdfs/trackermkdir: 已创建目录 "/data"mkdir: 已创建目录 "/data/fastdfs"mkdir: 已创建目录 "/data/fastdfs/tracker"
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

③编辑 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 启动时所监听的端口
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

运行

直接使用 fdfs_trackerd 来启动tracker进程,然后使用netstat 查看端口是否起来。

root@tracker:/etc/fdfs# fdfs_trackerd /etc/fdfs/tracker.conf restartroot@tracker:/etc/fdfs# netstat -antp | grep trackerdtcp   0  0 0.0.0.0:22122   0.0.0.0:*   LISTEN   14520/fdfs_trackerd root@tracker:/etc/fdfs# 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

也可以查看日志看服务是否正常运行:

root@tracker:~# cat /data/fastdfs/tracker/logs/trackerd.log
  • 1
  • 1
[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
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

⑤设置开机自动启动

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

至此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一样。

安装libfastcommonroot@storage1:/usr/local/src# unzip libfastcommon-master.zip root@storage1:/usr/local/src# cd libfastcommon-masterroot@storage1:/usr/local/src/libfastcommon-master# lsHISTORY  INSTALL  libfastcommon.spec  make.sh  php-fastcommon  README  srcroot@storage1:/usr/local/src/libfastcommon-master# ./make.sh root@storage1:/usr/local/src/libfastcommon-master# ./make.sh install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
安装FastDFSFastDFS_v5.08.tar.gzroot@storage1:/usr/local/src# tar zxvf FastDFS_v5.08.tar.gz root@storage1:/usr/local/src# cd FastDFSroot@storage1:/usr/local/src/FastDFS# ./make.shroot@storage1:/usr/local/src/FastDFS# ./make.sh install 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

配置

进入到 /etc/fdfs 目录下,复制/usr/local/src/FastDFS/conf/* 下面的所有文件到当前目录下(/etc/fdfs):

root@storage1:/etc/fdfs$ lltotal 96drwxr-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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

编辑配置文件 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的端口号
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

[备注]: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
  • 1
  • 1

查看进程是否运行:

root@dstorage1:/etc/fdfs# netstat -antp | grep storagetcp  0  0 0.0.0.0:23000   0.0.0.0:*   LISTEN      23426/fdfs_storaged
  • 1
  • 2
  • 1
  • 2

观察日志 /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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

这里看到,创建了二级目录,成功连接到tracker。 
可以使用 fdfs_monitor 来查看一下storage的状态,看是否已经成功注册到了tracker:

root@storage1:/# fdfs_monitor /etc/fdfs/storage.conf 或者:fdfs_monitor /etc/fdfs/client.conf(不过这个文件需要配置一下才行)
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
[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: 0server_count=1, server_index=0tracker server is 10.10.10.80:22122group count: 1Group 1:group name = group1disk total space = 7384 MBdisk free space = 4306 MBtrunk free space = 0 MBstorage server count = 1active server count = 1storage server port = 23000storage HTTP port = 10000store path count = 1subdir count per path = 256current write server index = 0current 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        。。。。。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

看到这里显示“ACTIVE”,说明当前的storage节点已经注册到了Tracker上。

设置开机启动

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

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

到这里storage的安装配置也完成了。当第二台节点也加进来后,可以通过日志看到如下信息:

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

【注】因为同一组的节点会进行数据同步,所以一个组内的所有storage节点会进行通信,那么这个地方就可以看出来,他们上线后会建立连接。 
另外,从端口信息上也可以看出来:

root@storage1:/# netstat -antp | grep storagetcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      23426/fdfs_storagedtcp        0      0 10.10.10.81:23000      10.10.10.82:33842      ESTABLISHED 23426/fdfs_storagedtcp        0      0 10.10.10.81:50824      10.10.10.82:23000      ESTABLISHED 23426/fdfs_storagedtcp        0      0 10.10.10.81:58116      10.10.10.80:22122      ESTABLISHED 23426/fdfs_storaged
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

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# lsauto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  srcroot@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
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

编译安装:

make && make install 
  • 1
  • 1

配置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# lltotal 1048drwxr-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/。。。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

③将 FastDFS 的 nginx 插件模块的配置文件拷贝到 FastDFS 配置文件目录:

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

修改 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=group1storage_server_port=23000store_path_count=1store_path0=/data/fastdfs/storage
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

④建立 M00 至存储目录的符号连接

root@storage1# ln -s /data/fastdfs/storage/data /data/fastdfs/storage/data/M00root@storage1# ll /data/fastdfs/storage/data/M00lrwxrwxrwx 1 root root 26 Jun 16 19:53 /data/fastdfs/storage/data/M00 -> /data/fastdfs/storage/data/
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

到这里,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 
  • 1
  • 1

也可以访问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.0root@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.8root@server:/usr/local/src/nginx-1.10.0#make && make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

nginx 以及 nginx cache purge 插件模块安装完毕。

配置nginx

①创建nginx的缓存目录

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

②增加系统打开文件数的限制,手写到rc.local

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

③编辑 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;    }     }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

接下来,可以使用 /usr/local/nginx/sbin/nginx -t 来测试配置文件语法的正确性。

root@server:/usr/local/nginx# ./sbin/nginx -tngx_http_fastdfs_set pid=5895nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfulroot@server:/usr/local/nginx# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

④启动nginx并加入到开机启动

/usr/local/nginx/sbin/nginx
  • 1
  • 1

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

netstat -unltp | grep nginx
  • 1
  • 1

测试上传文件

以下操作在tracker服务器(10.10.10.80)上进行 
①创建client 使用的日志目录

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

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

base_path=/data/fastdfs/client #日志存放路径tracker_server=10.10.10.80:22122 #tracker 服务器 IP 地址和端口号http.tracker_server_port=8888
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

③上传一个图片

root@server# fdfs_upload_file /etc/fdfs/client.conf 1.jpggroup1/M00/00/00/CgoKeFRQqaaASddNAxEgRsu1UiU345.jpgroot@server#
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

得到了如下的地址: 
group1/M00/00/00/CgoKeFRQqaaASddNAxEgRsu1UiU345.jpg 
还可以通过fdfs_file_info 来查看上传文件的信息

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

通过浏览器访问: 
④使用浏览器访问一下该地址: 
http://10.10.10.79/group1/M00/00/00/CgoKeFRQqaaASddNAxEgRsu1UiU345.jpg 
即可成功访问到图片。 
⑤查看nginx的缓存目录,可以查看缓存的内容。

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

⑥清除缓存 
如果清除该图片的缓存,可以在文件 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;
  • 1
  • 2
  • 1
  • 2

最终效果

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;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

重启nginx,清除缓存再次访问即可查看响应头命中信息。

附录

需要再多写一点, FastDFS这样部署就已经可以正常运行了,但是有一个问题,就是FastDFS本身不支持对上传的文件去重。也就是说,同一个文件,我们反复上传,那么会在Storage中保存多份,这显然是不合理的。解决办法,需要的请参考我另一篇文章: 
《FastDHT配合FastDFS进行文件上传去重》 
http://blog.csdn.net/ForeverSunshine/article/details/51226061