拓扑图jpg

下面的操作是我们nginx做反代对tracker做负载均衡
    nginx-mogilefs-module模块下载网站:https://github.com/vkholodkov/nginx-mogilefs-module
http://wiki.nginx.org/3rdPartyModules
http://www.grid.net.ru/nginx/download/
        编译包:   
             nginx-1.6.2.tar.gz   
            nginx_mogilefs_module-1.0.4.tar.gz 

一、安装Nginx:

1、解决依赖关系

# yum groupinstall "Development Tools" "Server Platform Deveopment"
# yum install openssl-devel pcre-devel

2、安装

首先添加用户nginx,实现以之运行nginx服务进程:
# groupadd -r nginx
# useradd -r -g nginx nginx

需要编译安装nginx和nginx-mogilefs-module模块
        # tar xf nginx-1.6.2.tar.gz
        # tar xf nginx_mogilefs_module-1.0.2.tar.gz
        # cd nginx-1.6.2
接着开始编译和安装:
# ./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre \
  --with-debug \
  --add-module=../nginx_mogilefs_module-1.0.4(这里指定的路径由于我们的模块已经解压到了父目录下)
    检测内容中会显示刚添加的模块信息:如(adding module in ../nginx_mogilefs_module-1.0.4
                                            + ngx_http_mogilefs_module was configured)
# make && make install

说明:
1、Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即可。
2、如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。

3、为nginx提供SysV init脚本:

新建文件/etc/rc.d/init.d/nginx,内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

而后为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/nginx

添加至服务管理列表,并让其开机自动启动:
# chkconfig --add nginx
# chkconfig nginx on

而后就可以启动服务并测试了:
# service nginx start

二、配置nginx代理:官方文档:http://www.grid.net.ru/nginx/mogilefs.en.html
    1、配置文件的配置操作
        # vim /etc/nginx.conf
         location /files {        ---------------添加指定下载的目录
                mogilefs_tracker 172.16.3.3:7001; -----------------指定我们上面配置的tracker的地址和监听的接口
                mogilefs_domain files;

                mogilefs_pass {
                    proxy_pass $mogilefs_path;
                    proxy_hide_header Content-Type;
                    proxy_buffering off;
                }
            }
        # nginx -t    
        # service nginx restart

    2、测试下:(由于我们 上面实例操作中创建了一个domain为files并上传了一个文件key为fstab.html)
http://172.16.3.3/files/fstab.html(图)

测试能够查看到上传的文件的内容
    3、上传一个图片并做修改配置并再次测试
        # mogupload --trackers=172.16.3.3:7001 --domain=images --key='/test.jpg' --file='/usr/share/backgrounds/simple_waves.jpg'
        修改配置文件
        location /img {              这里指定的是访问URL的路径
            mogilefs_tracker 172.16.3.3:7001;
            mogilefs_domain images;        这里指定的是domian名称空间的名称

            mogilefs_pass {
                proxy_pass $mogilefs_path;
                proxy_hide_header Content-Type;
                proxy_buffering off;
            }
        }
        测试: http://172.16.3.3/img/test.jpg(图)

 测试图2
    4、我们这里将做nginx的反向代理:
        nginx配置文件:
         upstream trackers {       定义代理trackers组
            server 172.16.3.2:7001 weight=2  max_fails=2 fail_timeout=3;
            server 172.16.3.3:7001;
            server 172.16.3.10:7001;
            least_conn;                   指定调度方法
       }

        server {
            listen       80;
        location /files {                        指定第一个location
            mogilefs_tracker trackers;
            mogilefs_domain files;

            mogilefs_pass {
                proxy_pass $mogilefs_path;
                proxy_hide_header Content-Type;
                proxy_buffering off;
            }
        }
        location /img {                                指定第二个location
            mogilefs_tracker trackers;
            mogilefs_domain images;

            mogilefs_pass {
                proxy_pass $mogilefs_path;
                proxy_hide_header Content-Type;
                proxy_buffering off;
            }
        }
        测试: http://172.16.3.3/img/test.jpg

查看结果1

 
以上的操作都是下载查看文件;由于本身有漏洞,所有上传文件不能实现,但网上有实现的是另外编写了配置文件。但是可以删除文件我们以上的配置是没问题的。           
使用curl上传
文件。
# curl -X PUT -T '/etc/passwd' http://172.16.3.3/files/passwd.html  上传是不能实现
# curl -X DELETE  http://172.16.3.3/files/fstab.html          删除是可以实现的

# mogfileinfo --trackers=172.16.3.3:7001 --domain=files --key='/fstab.html' 再次查看是没有的。

我们实际用的时候,是用应用程序API接口上传的。

下面是补充下:
没试验过仅供参考
用curl模拟了delete的操作,好像没什么问题,tracker的mysql记录和两台stored的文件都删除了
curl –request DELETE http://27.17.28.134/upload/2222

但是put操作确发生了问题
curl –request PUT –data “/home/ftpuser/memcached-1.4.13.tar.gz” http://27.17.28.134/upload/memcached-1.4.13.tar.gz
tracker的mysql已经写入到tempfile的数据表中了,而且其中一台stored已经写入了文件,但是好像在写入第二台stored的时候出现了问题,一直到curl报错退出
curl: (52) Empty reply from server
追踪了一下nginx的debug日志:
。。。
补:
这个问题已经解决,需要一个小小的patch,打完patch之后重新编译nginx即可。
详情见这个 http://www.ruby-forum.com/topic/217606
diff --git a/ngx_http_mogilefs_module.c b/ngx_http_mogilefs_module.c
index e229f47..a4d249d 100644
--- a/ngx_http_mogilefs_module.c
+++ b/ngx_http_mogilefs_module.c
@@ -483,6 +483,9 @@ ngx_http_mogilefs_put_handler(ngx_http_request_t *r)
         case FETCH:
             spare_location = mgcf->create_close_spare_location;
             ctx->state = CREATE_CLOSE;
+#if defined nginx_version && nginx_version >= 8011
+            r->main->count++;
+#endif
             break;
         case CREATE_CLOSE:
             r->headers_out.content_length_n = 0;