Nginx+FastDFS

来源:互联网 发布:sql with用法 编辑:程序博客网 时间:2024/05/18 15:05

如果要想单独使用Nginx程序你直接进行nginx源代码上传即可,但是这样的源代码是无法在实际的开发之中使用的,如果要想使用nginx还需要准备出一些模块组件。

1.将nginx的源代码以及组件模块源代码进行上传,随后进行解压缩:

解压nginx-1.11.3.tar.gztar xzvf /srv/ftp/nginx-1.11.3.tar.gz -C /usr/local/src/
解压echo-nginx-module-0.59.tar.gztar xzvf /srv/ftp/echo-nginx-module-0.59.tar.gz -C /usr/local/src/
解压nginx-upstream-fair-a18b409.tar.gztar xzvf /srv/ftp/nginx-upstream-fair-a18b409.tar.gz -C /usr/local/src/
解压ngx_cache_purge-2.3.tar.gztar xzvf /srv/ftp/ngx_cache_purge-2.3.tar.gz -C /usr/local/src/

2.为了方便进行统一的管理,nginx最终编译后的保存目录设置在/usr/local目录之中:

mkdir -p /usr/local/nginx/{logs,conf,fastcgi_temp,sbin,client_body_temp,proxy_temp,uwsgi_temp,scgi_temp}

3.进入到nginx源代码的目录:cd /usr/local/src/nginx-1.11.3/

4.进行编译的配置

./configure --prefix=/usr/local/nginx/ \--sbin-path=/usr/local/nginx/sbin/  \--with-http_ssl_module \--conf-path=/usr/local/nginx/conf/nginx.conf \--pid-path=/usr/local/nginx/logs/nginx.pid \--error-log-path=/usr/local/nginx/logs/error.log \--http-log-path=/usr/local/nginx/logs/access.log \--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \--http-client-body-temp-path=/usr/local/nginx/client_body_temp \--http-proxy-temp-path=/usr/local/nginx/proxy_temp \--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \--http-scgi-temp-path=/usr/local/nginx/scgi_temp \--add-module=/usr/local/src/echo-nginx-module-0.59 \--add-module=/usr/local/src/gnosek-nginx-upstream-fair-a18b409 \--add-module=/usr/local/src/ngx_cache_purge-2.3

5.进行nginx源代码的编译与安装:make && make install

6.当编译完成之后就可以通过安装目录查看到执行程序:/usr/local/nginx/sbin/nginx

  1. 启动nginx服务:/usr/local/nginx/sbin/nginx

  2. 通过浏览器访问:http://192.168.125.167;

FastDFS安装与配置

首先FastDFS给出的开发包依然是源代码开发包,本次使用的开发包版本为FastDFS_v5.08.tar.gz。但是如果要想编译此源代码,则需要首先获得一些额外的开发包的支持:libfastcommon

配置libfastcommon组件包

1.要通过github上去下载libfastcommon组件包的源代码:
下载地址:https://github.com/mldn/libfastcommon.git;

2.将libfastcommon源代码下载到/usr/local/src目录之中;

  • 首先进入到此目录:cd /usr/local/src/
  • 使用git克隆源代码:git clone https://github.com/mldn/libfastcommon.git

3.进入到libfastcommon所在的目录之中:cd /usr/local/src/libfastcommon/

4.进行项目的编译与安装处理:

  • 在libfastcommon组件里面提供有make指令,使用本地的:./make.sh
  • 随后进行安装:./make.sh install
  • 安装完成之后会发现一些重要的信息:
mkdir -p /usr/lib64 mkdir -p /usr/lib install -m 755 libfastcommon.so /usr/lib64 install -m 755 libfastcommon.so /usr/lib

5.此时发现存在有一些重要的文件存储路径,如果要想正常进行编译处理,需要做一个软连接。

ln -sv /usr/include/fastcommon/ /usr/local/include/fastcommon ln -sv /usr/lib/libfastcommon.so /usr/local/lib/libfastcommon.so

这种情况下你才可以进行FastDFS的编译处理。

编译FastDFS源代码

1.将FastDFS的源代码的文件上传到Linux系统之中,随后将其解压缩到/usr/local/src目录之中;

tar xzvf /srv/ftp/FastDFS_v5.08.tar.gz -C /usr/local/src/

2.进入到FastDFS源代码目录:cd /usr/local/src/FastDFS/

3.进行项目的编译处理:./make.sh

进行安装:./make.sh install

4.如果要想确认当前的配置是否已经真正安装成功,最好的检测方法是查看/etc/fdfs目录,如果可以在目录中发现有如下的文件信息,则表示FastDFS安装成功:

  • tracker.conf.sample:tracker的配置文件;
  • storage.conf.sample:storage的配置文件;
  • client.conf.sample:客户端配置文件。

配置FastDFS

现在成功的实现了FastDFS编译处理,那么随后就需要对其进行配置,如果要按照标准配置来讲,现在肯定需要有一个tracker,随后还需要提供有storage,但是FastDFS如果要想使用,则必须与Nginx进行有效结合。本次使用的主机列表如下:

主机名称 IP地址 描述 fastdfs-tracker 192.168.125.165 FastDFS-Tracker、FastDFS-Client fastdfs-storage-a 192.168.125.166 FastDFS-Storage

1.【fastdfs-tracker】通过给定的模版做一个复制:

cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf

2.【fastdfs-tracker】进行模版的配置:

  • 所有的服务进程一定要有一个数据的保存目录,建立目录:mkdir -p /usr/data/fdfs/tracker
  • 修改tracker.conf的配置文件:vim /etc/fdfs/tracker.conf
base_path=/usr/data/fdfs/tracker

3.【fastdfs-storage】复制一份配置的模版文件:cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

  • storage负责存储,也一定需要有一个存储目录:mkdir -p /usr/data/fdfs/storage
  • 编辑storage.conf配置文件:vim /etc/fdfs/storage.conf

数据目录:base_path=/usr/data/fdfs/storage
备份目录:store_path0=/usr/data/fdfs/storage
设置tracker路径:tracker_server=192.168.68.180:22122

4.**【fastdfs-tracker】**FastDFS专门提供了测试的命令工具,需要进行一些配置:

  • 复制出一份配置文件:cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
  • 建立一个客户端的数据目录:mkdir -p /usr/data/fdfs/client
  • 编辑客户端配置文件:vim /etc/fdfs/client.conf

数据保存路径:base_path=/usr/data/fdfs/client
Tracker连接地址:tracker_server=192.168.68.180:22122

5.【fastdfs-tracker】启动tracker服务:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

6.【fastdfs-storage-a】启动storage服务:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

7.【fastdfs-tracker】启动客户端进行测试:

首先需要上传一个文件:/usr/bin/fdfs_test /etc/fdfs/client.conf upload /srv/ftp/a.jpg上传完成之后会返回如下的路径信息:

http://192.168.125.166/group1/M00/00/00/wKh9plk6t1-AYS1VAABT61m_G70347_big.jpg

8.【fastdfs-tracker】进行状态监控:/usr/bin/fdfs_monitor /etc/fdfs/client.conf

9.【fastdfs-tracker】取得文件信息:/usr/bin/fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKh9plk6t1-AYS1VAABT61m_G70347_big.jpg

root@fastdfs-tracker:/srv/ftp# /usr/bin/fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKh9plk6t1-AYS1VAABT61m_G70347.jpgsource storage id: 0source ip address: 192.168.125.166file create timestamp: 2017-06-09 22:57:35file size: 21483file crc32: 1505696701 (0x59BF1BBD)

10.【fastdfs-tracker】下载文件:/usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKh9plk6t1-AYS1VAABT61m_G70347_big.jpg

11.【fastdfs-tracker】删除文件:
/usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKh9plk6t1-AYS1VAABT61m_G70347_big.jpg
它适合于做专门的图像文件的存储服务器。

在storage上配置nginx

如果要想显示FDFS中的文件内容,则一定需要通过WEB服务器完成,那么现在最简单的做法就是直接使用nginx进行处理。
1、【fastdfs-storage-a】**现在如果要想让fdfs可以在nginx上显示有一点非常麻烦,就是需要配置一系列的组件模块,将这些模块上传到Linux系统之中,而后进行解压缩:

解压缩nginx-1.11.3.tar.gztar xzvf /srv/ftp/nginx-1.11.3.tar.gz -C /usr/local/src/
解压缩echo-nginx-module-0.59.tar.gztar xzvf /srv/ftp/echo-nginx-module-0.59.tar.gz -C /usr/local/src/
解压缩ngx_cache_purge-2.3.tar.gztar xzvf /srv/ftp/ngx_cache_purge-2.3.tar.gz -C /usr/local/src/
解压缩nginx-upstream-fair-a18b409.tar.gztar xzvf /srv/ftp/nginx-upstream-fair-a18b409.tar.gz -C /usr/local/src/
解压缩fastdfs-nginx-module_v1.16.tar.gztar xzvf /srv/ftp/fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/src/
解压缩pcre-8.36.tar.gztar xzvf /srv/ftp/pcre-8.36.tar.gz -C /usr/local/src/
解压缩zlib-1.2.8.tar.gztar xvf /srv/ftp/zlib-1.2.8.tar.gz -C /usr/local/src/

2.【fastdfs-storage-a】建立nginx编译后的保存目录:

mkdir -p /usr/local/nginx/{logs,conf,fastcgi_temp,sbin,client_body_temp,proxy_temp,uwsgi_temp,scgi_temp}

3.【fastdfs-storage-a】进入到nginx源代码所在的目录之中:cd /usr/local/src/nginx-1.11.3/

4.【fastdfs-storage-a】进行编译处理:

./configure --prefix=/usr/local/nginx/ \--sbin-path=/usr/local/nginx/sbin/ \--with-http_ssl_module \--conf-path=/usr/local/nginx/conf/nginx.conf \--pid-path=/usr/local/nginx/logs/nginx.pid \--error-log-path=/usr/local/nginx/logs/error.log \--http-log-path=/usr/local/nginx/logs/access.log \--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \--http-client-body-temp-path=/usr/local/nginx/client_body_temp \--http-proxy-temp-path=/usr/local/nginx/proxy_temp \--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \--http-scgi-temp-path=/usr/local/nginx/scgi_temp \--add-module=/usr/local/src/echo-nginx-module-0.59 \--add-module=/usr/local/src/gnosek-nginx-upstream-fair-a18b409 \--add-module=/usr/local/src/ngx_cache_purge-2.3 \--add-module=/usr/local/src/fastdfs-nginx-module/src \--with-zlib=/usr/local/src/zlib-1.2.8 \--with-pcre=/usr/local/src/pcre-8.36

5.【fastdfs-storage-a】编译并安装nginx:make && make install

6.【fastdfs-storage-a】如果要想将nginx与fastdfs整合,则需要有一个配置文件。

  • 拷贝配置文件:cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
  • 编辑mod_fastdfs.conf配置文件:vim /etc/fdfs/mod_fastdfs.conf

配置存储的路径:base_path=/usr/data/fdfs/storage
设置tracker路径:tracker_server=192.168.125.166:22122
设置组名称:group_name=group1
URL路径上是否需要编写组名称:url_have_group_name=true
设置存储节点:store_path0=/usr/data/fdfs/storage

7.【fastdfs-storage-a】复制两个重要的与http访问有关的配置文件:

  • 复制http.conf配置文件:cp /usr/local/src/FastDFS/conf/http.conf /etc/fdfs/
  • 拷贝mime.types配置文件:cp /usr/local/src/FastDFS/conf/mime.types /etc/fdfs/

8.【fastdfs-storage-a】为了可以让nginx找到所需要的图像内容,还需要设置一个软连接:

ln -s /usr/data/fdfs/storage/data/ /usr/data/fdfs/storage/M00

9.【fastdfs-storage-a】修改nginx配置文件:vim /usr/local/nginx/conf/nginx.conf

修改监听端口:listen 9999;
进行storage访问:

location ~/group[1-3]/M00 {     root /usr/data/fdfs/storage/ ;     ngx_fastdfs_module ; }

10.【fastdfs-storage-a】测试一下配置文件:/usr/local/nginx/sbin/nginx -t

  • 启动nginx服务:/usr/local/nginx/sbin/nginx
  • 打开浏览器测试:http://192.168.125.166:9999/group1/M00/00/00/wKh9plk6t1-AYS1VAABT61m_G70347_big.jpg

配置第二台storage主机

现在已经成功的实现了一个单节点配置,那么随后还需要准备出第二台节点,考虑到整体的编译的复杂度以及nginx的配置的复杂度,所以将之前的虚拟机做了一个复制。
1.【fastdfs-storage-b】修改storage.conf配置文件:vim /etc/fdfs/storage.conf
设置新主机为第二组:group_name=group2

2.【fastdfs-storage-b】修改mod_fastdfs配置文件:vim /etc/fdfs/mod_fastdfs.conf
设置新主机为第二组:group_name=group2

3.【fastdfs-storage-b】启动nginx服务:/usr/local/nginx/sbin/nginx

4.【fastdfs-storage-b】启动storage服务:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

5.【fastdfs-tracker】检测一下所有从节点:/usr/bin/fdfs_monitor /etc/fdfs/client.conf
只要你的storage主机配置好了tracker地址,那么一定可以自动找到;

在Tracker上配置Nginx

现在所有的storage节点都已经被tracker所管理,而每一个storage主机上都存在有一个自己的nginx,但是如果要想访问肯定要通过tracker访问比较合适,那么下面就需要在tracker主机上进行nginx的配置处理。

1.【fastdfs-tracker】将所需要的nginx开发包上传到系统之中:

解压缩nginx-1.11.3.tar.gztar xzvf /srv/ftp/nginx-1.11.3.tar.gz -C /usr/local/src/
解压缩echo-nginx-module-0.59.tar.gztar xzvf /srv/ftp/echo-nginx-module-0.59.tar.gz -C /usr/local/src/
解压缩ngx_cache_purge-2.3.tar.gztar xzvf /srv/ftp/ngx_cache_purge-2.3.tar.gz -C /usr/local/src/
解压缩nginx-upstream-fair-a18b409.tar.gztar xzvf /srv/ftp/nginx-upstream-fair-a18b409.tar.gz -C /usr/local/src/
解压缩pcre-8.36.tar.gztar xzvf /srv/ftp/pcre-8.36.tar.gz -C /usr/local/src/
解压缩zlib-1.2.8.tar.gztar xvf /srv/ftp/zlib-1.2.8.tar.gz -C /usr/local/src/

2.【fastdfs-tracker】建立nginx保存的目录:

mkdir -p /usr/local/nginx/{logs,conf,fastcgi_temp,sbin,client_body_temp,proxy_temp,uwsgi_temp,scgi_temp}

3.【fastdfs-tracker】进入到nginx源代码目录:cd /usr/local/src/nginx-1.11.3/

  • 进行编译配置:
./configure --prefix=/usr/local/nginx/ \--sbin-path=/usr/local/nginx/sbin/ \--with-http_ssl_module \--conf-path=/usr/local/nginx/conf/nginx.conf \--pid-path=/usr/local/nginx/logs/nginx.pid \--error-log-path=/usr/local/nginx/logs/error.log \--http-log-path=/usr/local/nginx/logs/access.log \--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \--http-client-body-temp-path=/usr/local/nginx/client_body_temp \--http-proxy-temp-path=/usr/local/nginx/proxy_temp \--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \--http-scgi-temp-path=/usr/local/nginx/scgi_temp \--add-module=/usr/local/src/echo-nginx-module-0.59 \--add-module=/usr/local/src/gnosek-nginx-upstream-fair-a18b409 \--add-module=/usr/local/src/ngx_cache_purge-2.3 \--with-zlib=/usr/local/src/zlib-1.2.8 \--with-pcre=/usr/local/src/pcre-8.36
  • 编译与安装:make && make install

4.**【fastdfs-tracker】**nginx本身带有缓存功能, 所以为了保证读取速度快,追加缓存配置所在目录:

mkdir -p /usr/data/nginx/{cache,tmp}

5.【fastdfs-tracker】配置nginx文件:vim /usr/local/nginx/conf/nginx.conf

#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  65536;    use epoll;}http {    include mime.types;     default_type application/octet-stream;     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 /usr/data/nginx/cache levels=1:2 keys_zone=http-cache:500m max_size=10g inactive=30d; proxy_temp_path /usr/data/nginx/tmp;    upstream fdfs_group1 {         server 192.168.125.166:9999 weight=1 max_fails=2 fail_timeout=30s;     }     upstream fdfs_group2 {         server 192.168.125.170:9999 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"';    #access_log logs/access.log main;    sendfile on;     #tcp_nopush on;     #keepalive_timeout 0;     keepalive_timeout 65;     #gzip on;    server {         listen 80;         server_name localhost;         #charset koi8-r;         #access_log logs/host.access.log main;         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 /group2/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_group2;             expires 30d;         }         location ~ /purge(/.*) {             allow 127.0.0.1;             allow 192.168.125.0/24;             deny all;             proxy_cache_purge http-cache $1$is_args$args;         }        location / {             root html;             index index.html index.htm;         }        #error_page 404 /404.html;         # redirect server error pages to the static page /50x.html         # error_page 500 502 503 504 /50x.html;        location = /50x.html {             root html;         }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80         #         #location ~ \.php$ {         #   proxy_pass http://127.0.0.1;         #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000         #         #location ~ \.php$ {         #   root html;         #   fastcgi_pass 127.0.0.1:9000;         #   fastcgi_index index.php;         #   fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;         # include fastcgi_params;         #}         # deny access to .htaccess files, if Apache's document root         # concurs with nginx's one         #         #location ~ /\.ht {         #   deny all;         #}     }    # another virtual host using mix of IP-, name-, and port-based configuration     #     #server {     #   listen 8000;     #   listen somename:8080;     #   server_name somename alias another.alias;     #   location / {     #       root html;     #       index index.html index.htm;     #   }     #}    # HTTPS server     # #server {     #   listen 443 ssl;     #   server_name localhost;     #   ssl_certificate cert.pem;     #   ssl_certificate_key cert.key;     #   ssl_session_cache shared:SSL:1m;     #   ssl_session_timeout 5m;    #   ssl_ciphers HIGH:!aNULL:!MD5;     #   ssl_prefer_server_ciphers on;     #   location / {     #       root html;     #       index index.html index.htm;     #   }     #} }

6.【fastdfs-tracker】检测nginx配置文件:`/usr/local/nginx/sbin/nginx -t

http://192.168.125.170/group2/M00/00/00/wKh9plk6t1-AYS1VAABT61m_G70347_big.jpg

使用Java操作FastDFS

Java操作FastDFS只需要一个开发包即可,但是这个开发包很忐忑,这个FastDFS开发包不知道为什么总是在变。

1.建立一个新的Maven项目,随后修改pom.xml配置文件:

<dependency>     <groupId>com.github.kischang</groupId>    <artifactId>fastdfs-client</artifactId>    <version>0.1</version></dependency>

2.建立一个资源文件:fastdfs_client.conf:

# 配置上你tracker的连接地址tracker_server=192.168.125.165:22122

3.编写文件上传:

package cn.mldn.fastdfs;import java.io.File;import java.util.Arrays;import org.csource.common.NameValuePair;import org.csource.fastdfs.ClientGlobal;import org.csource.fastdfs.StorageClient;import org.csource.fastdfs.StorageServer;import org.csource.fastdfs.TrackerClient;import org.csource.fastdfs.TrackerServer;import org.springframework.core.io.ClassPathResource;public class FastDFSUploadFile {    public static void main(String[] args) throws Exception {        File imgFile = new File("D:" + File.separator + "my.jpg"); // 要上传的文件        // 如果要想进行上传则一定需要获取到文件的扩展名称        String fileExtName = imgFile.getName()                .substring(imgFile.getName().lastIndexOf(".") + 1);        // 通过ClassPath路径获取要使用的配置文件        ClassPathResource classPathResource = new ClassPathResource(                "fastdfs_client.conf");        // 进行客户端访问的整体配置,需要知道配置文件的完整路径        ClientGlobal.init(classPathResource.getClassLoader()                .getResource("fastdfs_client.conf").getPath());        // FastDFS的核心操作在于tracker处理上,所以此时需要定义Tracker客户端        TrackerClient trackerClient = new TrackerClient();        // 定义TrackerServer的配置信息        TrackerServer trackerServer = trackerClient.getConnection();        // 在整个FastDFS之中真正负责干活的就是Storage        StorageServer storageServer = null;        StorageClient storageClient = new StorageClient(trackerServer,                storageServer);        // 定义上传文件的元数据        NameValuePair[] metaList = new NameValuePair[3];        metaList[0] = new NameValuePair("fileName", imgFile.getName());        metaList[1] = new NameValuePair("fileExtName", fileExtName);        metaList[2] = new NameValuePair("fileLength",                String.valueOf(imgFile.length()));        // 如果要上传则使用trackerClient对象完成        String[] upload_file = storageClient.upload_file(imgFile.getPath(), fileExtName, metaList) ;        System.out.println(Arrays.toString(upload_file));        trackerServer.close();    }}

现在上传完成后会给出两个内容:组名称、文件路径名称,如果现在只想获得完整名称,则更换一个类:

StorageClient1 storageClient = new StorageClient1(trackerServer,storageServer);String upload_file = storageClient.upload_file1(imgFile.getPath(), fileExtName, metaList) ;

4.删除文件:

package cn.mldn.fastdfs;import java.io.File;import org.csource.fastdfs.ClientGlobal;import org.csource.fastdfs.StorageClient;import org.csource.fastdfs.StorageServer;import org.csource.fastdfs.TrackerClient;import org.csource.fastdfs.TrackerServer;import org.springframework.core.io.ClassPathResource;public class FastDFSDeleteFile {    public static void main(String[] args) throws Exception {        File imgFile = new File("D:" + File.separator + "my.jpg"); // 要上传的文件        // 如果要想进行上传则一定需要获取到文件的扩展名称        String fileExtName = imgFile.getName()                .substring(imgFile.getName().lastIndexOf(".") + 1);        // 通过ClassPath路径获取要使用的配置文件        ClassPathResource classPathResource = new ClassPathResource(                "fastdfs_client.conf");        // 进行客户端访问的整体配置,需要知道配置文件的完整路径        ClientGlobal.init(classPathResource.getClassLoader()                .getResource("fastdfs_client.conf").getPath());        // FastDFS的核心操作在于tracker处理上,所以此时需要定义Tracker客户端        TrackerClient trackerClient = new TrackerClient();        // 定义TrackerServer的配置信息        TrackerServer trackerServer = trackerClient.getConnection();        // 在整个FastDFS之中真正负责干活的就是Storage        StorageServer storageServer = null;        StorageClient storageClient = new StorageClient(trackerServer,                storageServer);        int delete_file = storageClient.delete_file("group2", "M00/00/00/wKh9plk6t1-AYS1VAABT61m_G70347_big.jpg") ;        System.out.println(delete_file);        trackerServer.close();    }}

删除成功返回的内容是0。

FastDFS安全配置

访问列表

FastDFS图片服务器的安全主要通过两个层面来解决:iptables做一个过滤列表,在tracker里面可以设置黑白名单。

1.【fastdfs-tracker】打开tracker配置文件:vim /etc/fdfs/tracker.conf

allow_hosts=192.168.125.[165,164-168]

重新启动tracker服务:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
测试图片上传:

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /srv/ftp/b.jpg

上传成功:

http://192.168.125.166/group1/M00/00/00/wKh9plk8CeWATsUTAABT61m_G70589_big.jpg

以上的配置没有问题,但是很多的时候有可能有各种原因导致程序出现无法使用,最稳妥的做法还是使用iptables控制。

盗链防范

现在图片服务器虽然搭建完成了,但是整个的图片服务器会有一个问题存在,用户只需要知道你的访问地址,就可以进行图片的展示,也就是说有可能会有大量的人员直接利用你自己服务器的资源来为自己服务,这样的设计明显不复合于实际的要求,所以现在需要开启盗链防范。

1.【fastdfs-storage-*】修改http.conf配置文件:vim /etc/fdfs/http.conf

  • 将盗链显示错误图片上传,并且将盗链图片保存到/etc/fdfs目录下:mv /srv/ftp/noauth.gif /etc/fdfs/
  • 为了让图片可以使用,还需要设置一个权限:chmod 777 /etc/fdfs/noauth.gif

开启token验证:http.anti_steal.check_token=true
设置连接的密钥:http.anti_steal.secret_key=FastDFS1234567890
出现盗链后的显示图片:http.anti_steal.token_check_fail=/etc/fdfs/noauth.gif

重新启动storage服务:/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

2.【fastdfs-storage-*】随后要修改程序,因为token只有程序可以生成,修改一下fastdfs_client.conf文件:

// 配置上你tracker的连接地址 tracker_server=192.168.125.165:22122 //表示现在要进行token检测 http.anti_steal_token=true //进行认证的密码处理 http.secret_key=FastDFS1234567890

3.【fastdfs-storage-*】重新启动nginx服务:/usr/local/nginx/sbin/nginx -s reload

4.编写一个生成token信息的操作;

package cn.mldn.fastdfs;import org.csource.fastdfs.ClientGlobal;import org.csource.fastdfs.ProtoCommon;import org.csource.fastdfs.TrackerClient;import org.csource.fastdfs.TrackerServer;import org.springframework.core.io.ClassPathResource;public class FastDFSToken {    public static void main(String[] args) throws Exception {        // 通过ClassPath路径获取要使用的配置文件        ClassPathResource classPathResource = new ClassPathResource(                "fastdfs_client.conf");        // 进行客户端访问的整体配置,需要知道配置文件的完整路径        ClientGlobal.init(classPathResource.getClassLoader()                .getResource("fastdfs_client.conf").getPath());        // FastDFS的核心操作在于tracker处理上,所以此时需要定义Tracker客户端        TrackerClient trackerClient = new TrackerClient();        // 定义TrackerServer的配置信息        TrackerServer trackerServer = trackerClient.getConnection();        String fileId = "M00/00/00/wKh9plk8CeWATsUTAABT61m_G70589_big.jpg" ;        int ts = (int) (System.currentTimeMillis() / 1000) ;// 时间参考        StringBuffer fileUrl = new StringBuffer() ;        fileUrl.append("http://") ;        fileUrl.append(trackerServer.getInetSocketAddress().getHostString()) ;        fileUrl.append("/group2/").append(fileId) ;        fileUrl.append("?token=").append(ProtoCommon.getToken(fileId, ts, ClientGlobal.g_secret_key)) ;        fileUrl.append("&ts=").append(ts) ;        System.out.println(fileUrl);        trackerServer.close();    }}
原创粉丝点击