FastDFS+Nginx实现http协议下载(2)

uu564237933 FastDFSFastDFS+Nginx实现http协议下载(2)已关闭评论23,0122字数 5311阅读17分42秒阅读模式

-- nginx结合 fastdfs-nginx-module插件去实现http协议下载 在所有storage节点和tracker节点都需要安装nginx

原理介绍

1. storage节点中的nginx 主要是结合fastdfs-nginx-module模块提供http的访问服务 同时解决group中storage服务器的同步延迟问题。
2. tracker节点中的nginx 主要是提供http访问的反向代理、负载均衡以及缓存服务

服务器角色

192.168.5.231    tracker + nginx
192.168.5.233    group1/stroage1 + nginx + fastdfs-nginx-module
192.168.5.234    group1/storage2 + nginx + fastdfs-nginx-module
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
本实验请在完成《FastDFS部署(1)》后进行

软件下载

nginx1.8:
wget http://nginx.org/download/nginx-1.8.1.tar.gz
fastdfs-nginx-module v1.15:
wget http://downloads.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Ffastdfs%2Ffiles%2FFastDFS%2520Nginx%2520Module%2520Source%2520Code%2F&ts=1477375666&use_mirror=nchc
ngx_cache_purge 2.3:
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz

storage上安装nginx

-- 在5.233 和 5.234上操作 -- 这里先在5.233上操作 然后把相关内容复制去5.234
-- storage节点中的nginx 主要是结合fastdfs-nginx-module模块提供http的访问服务 同时解决group中storage服务器的同步延迟问题。
-- 安装nginx依赖
yum install -y zlib-devel pcre-devel openssl-devel
-- 解决fastdfs-nginx-module模块的路径问题
tar -xzvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module/src
vim config                                        -- 修改模块的配置文件
修改以下内容
要修改的行: CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
修改成这样: CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
-- 编译安装nginx
 tar -xzvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx --with-pcre --add-module=../fastdfs-nginx-module/src             -
- 这里暂时只with这个实验必要的模块 生产环境中可以按需添加模块
make && make install
-- 复制fastdfs-nginx-module模块的配置文件到fastdfs的配置目录
cp /root/software/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
-- 修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
在将server段的listen端口改为8080
listen 8080;
在server段中添加以下内容
location ~ /group1/M00 {     -- 匹陪url 其中group1是该storage节点所属组的组名 对应storage配置文件中的 group_name,M00还不清楚
    root /home/fastdfs/data;      -- 根目录指向storage的存储目录 对应storage配置文件中的store_path0中的路径
    ngx_fastdfs_module;   -- 调用ngx_fasdfs_module模块
}
-- 这里只写了必须的配置 nginx的其他功能 请按需配置
-- 修改ngx_fastdfs_module模块的配置文件
vim /etc/fdfs/mod_fastdfs.conf
修改以下内容
base_path=/home/fastdfs                                        -- 保存日志的目录(跟storage.conf配置一样即可) 但实际上日志默认会输出到nginx的error.log 除非修改了log_filename参数
tracker_server=192.168.5.231:22122                        -- tracker_server的IP地址 有多个tracker_server就需要写多个tracker_server
storage_server_port=23000                                      -- storage服务器的端口号
group_name=group1                                                -- 当前storage节点所属的group名
url_have_group_name = true                                    -- 文件url中是否有group名(这个看刚刚上传文件后返回的url而定)
store_path_count=1                                                   -- 存储路径个数(跟storage.conf配置一样即可)
store_path0=/home/fastdfs                                       -- 存储路径(跟storage.conf配置一样即可)
group_count = 1                                                         -- 整个fastdfs有多少个组 这里暂时是1个
在最后添加以下内容
[group1]                                                                      -- 第1个组的详细信息
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/home/fastdfs
-- 建立M00至存储目录的软连接(不然实际路径跟返回的url路径对不上 但实际测试中 不做这步也能正常访问  估计是模块做了路径转换)
ln -s /home/fastdfs/data/ /home/fastdfs/data/M00
 
ll /home/fastdfs/data/M00
lrwxrwxrwx 1 root root 19 Oct 25 15:27 /home/fastdfs/data/M00 -> /home/fastdfs/data/
-- 这样的路径结构就对得上了
-- 复制fastdfs源码目录里与http功能相关的文件到fastdfs配置文件目录(不然nginx启动会报错)
cd /root/software/fastdfs-5.05/conf/
cp http.conf mime.types /etc/fdfs/
-- 启动nginx
/usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=6163
-- 这个 并不是表示模块以一个进程起来了 他只是输出了nginx其中一条子进程的pid
-- 查看启动情况
netstat -tnlp | grep 8080           -- 查看端口情况
 
tail -f /usr/local/nginx/logs/error.log        -- 查看错误日志 没有错误则表示启动正常
-- 访问nginx 和 存储的图片
浏览器访问 192.168.5.233:8080                                -- 就会看到nginx的欢迎页
访问上传的文件
192.168.5.233:8080/group1/M00/00/00/wKgF6VgO69WAW6kgAAULPRl0gCs682.png
-- 把上传图片时返回的url拼进去 即可访问上传的图片 看到图片即表示nginx配置成功
-- 把nginx及相关文件同步给5.234
rsync -avzR /usr/local/nginx/ 192.168.5.234:/
rsync -avzR /etc/fdfs/http.conf /etc/fdfs/mime.types /etc/fdfs/mod_fastdfs.conf 192.168.5.234:/
-- 由于5.234和5.233属于同一组 所以拷贝后需要修改的东西不多 如果是跨组的拷贝 就要注意需要修改的配置内容了
在5.234上也启动nginx 和访问上传的图片

tracker上安装nginx

-- 在192.168.5.231上操作
-- tracker上的nginx主要是为了 反向代理给storage上的nginx 和 负载均衡 和 实现缓存
-- 安装nginx依赖
yum install -y zlib-devel pcre-devel openssl-devel
-- 编译安装nginx
tar -xzvf nginx-1.8.1.tar.gz
tar -xzvf 2.3.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx --with-pcre --add-module=../ngx_cache_purge-2.3/                 
-- 这里暂时只with这个实验必要的模块 生产环境中可以按需添加模块
make && make install
-- 修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
######### 配置缓存区域 ###########
在http区域添加以下内容
add_header X-Cache-Status $upstream_cache_status;                -- 添加缓存命中标识
proxy_cache_path /tmp levels=1:2 keys_zone=http-cache:500m max_size=10g inactive=30d;
proxy_temp_path /tmp/proxy_tmp;
-- 缓存不是本笔记重点 意思解释请查看/project/nginx/反向代理/反向代理 缓存
######### 设置upstream ##########
在http区域添加以下内容
    upstream fdfs_group1 {                        -- fastdfs里组1的upstream 如有多个组要定义多个
        server 192.168.5.233:8080;
        server 192.168.5.234:8080;
    }
######### 设置反向代理 和 缓存 ###########
在server区域修改以下内容
        listen       8080;
在server区域添加以下内容
        location /group1/M00{                    -- 匹配group1/M00的url  如果有group2 3 就要配置多个
                proxy_next_upstream http_502 http_504 error timeout invalid_header;        -- 后端这些错误的就切换到upstream里的下一个后端服务器 所以我感觉他解决同步延迟问题是通过这里来解决
                proxy_cache http-cache;
                proxy_cache_valid 200 304 12h;
                proxy_cache_key $uri$is_args$args;
                proxy_pass http://fdfs_group1;                         -- 反向代理给名为fdfs_group1的upstream
                #expires 30d;
        }
-- 缓存不是本笔记重点 意思解释请查看/project/nginx/反向代理/反向代理 缓存
 
########## 配置清除缓存 ############
在server区域添加以下内容
        location ~ /purge(/.*) {
                allow 127.0.0.1;
                allow 192.168.5.0/24;
                deny all;
                proxy_cache_purge http-cache $1$is_args$args;
        }
-- 运行nginx
/usr/local/nginx/sbin/nginx
-- 访问nginx 和 存储的图片
浏览器访问 192.168.5.231:8080                                -- 就会看到nginx的欢迎页
访问上传的文件
192.168.5.231:8080/group1/M00/00/00/wKgF6VgO69WAW6kgAAULPRl0gCs682.png
-- 把上传图片时返回的url拼进去 即可访问上传的图片 看到图片即表示nginx配置成功
-- 清理nginx缓存 -- 在nginx配置的allow的IP列表内操作
curl http://192.168.5.231:8080/purge/group1/M00/00/00/wKgF6VgO69WAW6kgAAULPRl0gCs682.png

 文章源自运维生存时间-https://www.ttlsa.com/fastdfs/fastdfs-nginx-module-download/

文章源自运维生存时间-https://www.ttlsa.com/fastdfs/fastdfs-nginx-module-download/文章源自运维生存时间-https://www.ttlsa.com/fastdfs/fastdfs-nginx-module-download/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
uu564237933
  • 本文由 投稿,于03/06/2017 01:00:09发表
  • 转载请务必保留本文链接:https://www.ttlsa.com/fastdfs/fastdfs-nginx-module-download/