基于nginx访问的mogilefs
Storge Node支持将存储的数据以http模式的接口输出,客户端跟Storge Node进行交互的时,通常需要基于MogileFS提供的API编程之后,通过客户端程序和storge通信。nginx上的nginx_mogilefs模块就是这样的一个程序,用nginx作为MogileFS的反向代理,用户只要把需要访问的资源以url的形式向nginx请求,即能够得到需要的资源。
nginx_mogilefs_module工作原理:
mogielfs为nginx的第三方模块,该模块能够自动工作在nginx上,解码用户请求的url,把url中的一部分作为domain,一部分作为key发送给Tracker,Tracker将检索到的对应fid返回给mogielfs模块,mogielfs利用这个fid与Storge Nod交互取得数据,并将取得的数据返回给客户端。nginx也能够接受用户的上传文件请求,把对应的url解析成domain和key,并且将文件根据Tracker的指示存储在某个store节点上。
继续上次的配置:
http://blog.chinaunix.net/uid-30212356-id-5734140.html
配置环境:
mysql+tracker:192.168.85.129
storage node1:192.168.85.130
storage node2:192.168.85.131
nginx:192.168.85.132
下载地址:
项目地址:
一.编译安装Nginx
1.安装编译Nginx所需要的某些包
[root@localhost ~]# yum install pcre-devel zlib-devel openssl-devel -y
Nginx的gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl库;
2.提供一个运行nginx进程的用户和组
[root@localhost ~]# groupadd -r nginx
[root@localhost ~]# useradd -g nginx -s /sbin/nologin -r nginx
3.下载Nginx安装包和nginx-mogilefs-module并编译安装
[root@localhost ~]# wget
[root@localhost ~]# wget
[root@localhost ~]# tar xf nginx-1.10.0.tar.gz
[root@localhost ~]# tar xf nginx_mogilefs_module-1.0.4.tar.gz
[root@localhost ~]# cd nginx-1.10.0
[root@localhost nginx-1.10.0]#
./configure \
--prefix=/etc/nginx \
--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_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 \
--add-module=/root/nginx_mogilefs_module-1.0.4
--with-http_dav_module
[root@localhost nginx-1.10.0]# make && make install
4.启动Nginx(控制脚本见附录)
创建Nginx控制脚本并添加进服务列表
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# service nginx start
Starting nginx: [ OK ]
或是如下启动
[root@localhost nginx]# pwd
/etc/nginx
[root@localhost nginx]# nginx -c ./nginx.conf
5.打开浏览器访问测试Nginx能否正常工作
二.配置MogileFS
1.显示mogilefs可用的host
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 host list
192.168.85.130 [1]: alive
IP: 192.168.85.130:7500
192.168.85.131 [2]: alive
IP: 192.168.85.131:7500
2.显示可用的device
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 device list
192.168.85.130 [1]: alive
used(G) free(G) total(G) weight(%)
dev1: alive 3.176 13.300 16.476 100
192.168.85.131 [2]: alive
used(G) free(G) total(G) weight(%)
dev2: alive 3.292 13.184 16.476 100
3.mogstored中创建domain
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 domain add images
[root@localhost ~]# mogadm --trackers=192.168.85.129:7001 domain list
domain class mindevcount replpolicy hashtype
-------------------- -------------------- ------------- ------------ -------
files default 2 MultipleHosts() NONE
images default 2 MultipleHosts() NONE
也可以在为domain创建class,这里就不创建了,使用default的吧!
4.上传图片
[root@localhost ~]# mogupload --trackers=192.168.85.129:7001 --domain=images --key="1.png" --file="/usr/share/backgrounds/default.png"
[root@localhost ~]# mogfileinfo --trackers=192.168.85.129:7001 --domain=images --key="1.png"
- file: /images/1.png
class: default
devcount: 2
domain: images
fid: 11
key:
1.png
length: 1470177
- http://192.168.85.130:7500/dev1/0/000/000/0000000011.fid
- http://192.168.85.131:7500/dev2/0/000/000/0000000011.fid
三.配置Nginx连接MogileFS
关于Nginx的详细配置参数可以参考:
或是参考从Github下载的模块包中doc文件中的mogilefs.en.html文件;
1.编辑Nginx配置文件,添加如下location
location /images/ {
mogilefs_tracker 192.168.85.129:7001;
mogilefs_domain images;
mogilefs_noverify on;
mogilefs_pass {
proxy_pass $mogilefs_path;
proxy_hide_header Content-Type;
proxy_buffering off;
}
}
测试文件语法无错后重启Nginx服务:
[root@localhost nginx]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reloading nginx: [ OK ]
2.测试
访问时输入http://192.168.85.132/images/1.png
四.补充
在第二步第4小步中,可以看到我们上传图片时的key为1.png,这时能进行访问,如果是--key="/images/1.png"这样,那么访问1.png时会出现404页面,因为此时tracker将http://IP/images/1.png中的/images/1.png作为key,而默认情况下,mogilefs仅会将http://IP/images/1.png中的1.png作为key而不是/images/1.png,那么此时访问时肯定就是404了,解决办法就是在location中定义URL时,将整个URL作为key;
1.编辑Nginx配置文件,添加一个location
location ~* ^(/files/.*)$ {
mogilefs_tracker 192.168.85.129:7001;
mogilefs_domain files;
mogilefs_noverify on;
mogilefs_pass $1 {
proxy_pass $mogilefs_path;
proxy_hide_header Content-Type;
proxy_buffering off;
}
}
2.重启Nginx服务
[root@localhost nginx]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reloading nginx: [ OK ]
3.上传文件
[root@localhost ~]# mogupload --trackers=192.168.85.129:7001 --domain=files --key='/files/fstab.html' --file='/etc/fstab'
[root@localhost ~]# mogfileinfo --trackers=192.168.85.129:7001 --domain=files --key="/files/fstab.html"
- file: /files/fstab.html
class: default
devcount: 2
domain: files
fid: 14
key:
/files/fstab.html
length: 805
- http://192.168.85.130:7500/dev1/0/000/000/0000000014.fid
- http://192.168.85.131:7500/dev2/0/000/000/0000000014.fid
4.测试
5.如果是有多台tracker还可以配置负载均衡,直接配置tracker为upstrame
编辑Nginx的配置文件,在server外添加upstream(默认为轮询也可以基于权重):
upstream trackers_cluster {
server 192.168.85.129:7001;
server 192.168.85.128:7001;
}
然后再相应的location中更改mogilefs_tracker为mogilefs_tracker upstream;即可
6.对于本地上传功能,好像不支持
192.168.85.132主机上:
[root@localhost ~]# curl -X PUT -T "/usr/share/backgrounds/default_1920x1440.png" http://192.168.85.132/images/3.png
-
<html>
-
<head><title>413 Request Entity Too Large</title></head>
-
<body bgcolor="white">
-
<center><h1>413 Request Entity Too Large</h1></center>
-
<hr><center>nginx/1.10.0</center>
-
</body>
-
</html>
[root@localhost ~]# curl -X PUT -T "/root/test.html"
-
<html>
-
<head><title>405 Not Allowed</title></head>
-
<body bgcolor="white">
-
<center><h1>405 Not Allowed</h1></center>
-
<hr><center>nginx/1.10.0</center>
-
</body>
-
</html>
后来在132主机上的Nginx上安装了http的dav模块,可以做到基于PUT方法上传文件,但是在我这个环境中它的工作流程是这样的:先在132主机上上传文件,然后复制该文件到tracker上,最后让tracker通过mogupload命令上传,这并没有什么意义,虽然可以将Nginx和tracker安装在同一主机上,达到上传文件在mogupload的地步,但是还是觉得功能不够好,我想的是Nginx上传文件后能直接自动完成mogupload这样,但是现在还不知道怎么做,头疼!现在唯一的问题就是如何上传文件并直接mogupload了;
7.Nginx取代mogstored上的perbal(参考网上教程)
需要再编译Nginx时加上--with-http_dav_module就行了。
然后编辑Nginx配置文件为(给出一个完整示例):
[root@localhost nginx]# cat nginx.conf
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen
7500;
charset utf-8;
location / {
autoindex on;
root /data/mogdata/;
dav_methods put delete mkcol copy move;
client_max_body_size 200m;
client_body_temp_path /data/temp;
create_full_put_path on;
dav_access user:rw group:rw all:r;
}
error_page 500 502 503 504 /50x.html;
location /50x.html {
root html;
}
}
}
编辑mogstored配置文件为:
[root@localhost mogdata]# cat /etc/mogilefs/mogstored.conf
maxconns=10000
#httplisten=192.168.85.130:7500
server=none
mgmtlisten=192.168.85.130:7501
docroot=/data/mogdata
然后上传一个文件测试:
[root@localhost ~]# mogupload --trackers=192.168.85.129:7001 --domain=files --key="
/files/shadow.html" --file="/root/shadow.html"
结果:
[root@localhost ~]# mogfileinfo --trackers=192.168.85.129:7001 --domain=files --key="/files/shadow.html"
- file: /files/shadow.html
class: default
devcount: 2
domain: files
fid: 15
key: /files/shadow.html
length: 841
- http://192.168.85.130:7500/dev1/0/000/000/0000000015.fid
- http://192.168.85.131:7500/dev2/0/000/000/0000000015.fid
以上就完成了Nginx取代perbal了;
附录:
Nginx的控制脚本:
根据which nginx 修改nginx="/usr/sbin/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/local/nginx/sbin/nginx"
#
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