3.6 配置前端代理Nginx
重新编译安装nginx,添加nginx-mogilefs-module-master模块
[root@nginx1 tar]# cd /home/software/src/nginx-1.4.7/
[root@nginx1 nginx-1.4.7]# ./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=/home/software/tar/nginx-mogilefs-module-master
[root@nginx1 nginx-1.4.7]# make && make install
配置Nginx,将静态文件和图片的访问都转发至后端MogileFS即可
[root@nginx1 nginx]# cat /etc/nginx/nginx.conf
worker_processes 2;
error_log /var/log/nginx/nginx.error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
fastcgi_cache_path /www/cache levels=1:2 keys_zone=fcgicache:10m inactive=5m;
upstream mogfs_cluster { # 定义后端mogilefs集群
server 192.168.0.45:7001;
server 192.168.0.46:7001;
}
server {
listen 4040;
server_name xxrenzhe.lnmmp.com;
access_log /var/log/nginx/nginx-img.access.log main;
root /www/lnmmp.com;
valid_referers none blocked xxrenzhe.lnmmp.com *.lnmmp.com;
if ($invalid_referer) {
rewrite ^/ http://xxrenzhe.lnmmp.com/404.html
}
location ~* ^(/images/.*)$ { # 图片访问直接转发至后端mogilefs集群
mogilefs_tracker mogfs_cluster;
mogilefs_domain images; # 指定images域
mogilefs_noverify on;
mogilefs_pass $1 { # 传输访问的全路径(/images/.*)
proxy_pass $mogilefs_path;
proxy_hide_header Content=Type;
proxy_buffering off;
}
}
}
server {
listen 80;
server_name xxrenzhe.lnmmp.com;
access_log /var/log/nginx/nginx-static.access.log main;
location / {
root /www/lnmmp.com;
index index.php index.html index.htm;
}
location ~* ^(/text/.*)$ { # 文本访问直接转发至后端mogilefs集群
mogilefs_tracker mogfs_cluster;
mogilefs_domain text; # 指定text域
mogilefs_noverify on;
mogilefs_pass $1 { # 传输访问的全路径(/text/.*)
proxy_pass $mogilefs_path;
proxy_hide_header Content=Type;
proxy_buffering off;
}
}
gzip on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/x-javascript text/xml application/xml;
gzip_disable msie6;
}
server {
listen 8080;
server_name xxrenzhe.lnmmp.com;
access_log /var/log/nginx/nginx-php.access.log main;
location / {
root /www/lnmmp.com;
index index.php index.html index.htm;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /www/lnmmp.com;
}
location ~ \.php$ {
root /www/lnmmp.com;
fastcgi_pass 127.0.0.1:9000;
fastcgi_cache fcgicache;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_key $request_method://$host$request_uri;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
启动nginx服务
[root@nginx1 nginx]# service nginx start
Starting nginx: [ OK ]
3.7 访问验证
上传文件测试分布式文件系统MogileFS的功能
[root@mysql mogdata]# mogupload --domain=images --key='/images/fish.jpg' --file="/images/fish.jpg" # 上传图片文件,选择images域
[root@mysql mogdata]# mogupload --domain=text --key='/text/index.html' --file="/text/index.html" # 上传文本文件,选择text域
[root@mysql mogdata]# moglistkeys --domain=images # 列出已添加的key
/images/fish.jpg
[root@mysql mogdata]# moglistkeys --domain=text
/text/index.html
[root@mysql mogdata]# mogfileinfo --domain=images --key='/images/fish.jpg' # 查看已添加文件的具体信息,包括实际可访问地址
- file: /images/fish.jpg
class: default
devcount: 2
domain: images
fid: 9
key: /images/fish.jpg
length: 3225
- http://192.168.0.45:7500/dev1/0/000/000/0000000009.fid
- http://192.168.0.46:7500/dev2/0/000/000/0000000009.fid
[root@mysql mogdata]# mogfileinfo --domain=text --key='/text/index.html'
- file: /text/index.html
class: default
devcount: 2
domain: text
fid: 8
key: /text/index.html
length: 15
- http://192.168.0.46:7500/dev2/0/000/000/0000000008.fid
- http://192.168.0.45:7500/dev1/0/000/000/0000000008.fid

说明:直接访问mogilefs提供的文件存储路径,访问正常;
结合Nginx,从前端访问测试


说明:通过前端Nginx访问图片和文本文件一切正常,实现了对前端访问的透明性;
3.8 后续扩展
结合LNMT的架构,再通过Haproxy进行前端调度,就可以使用MogileFS这个分布式存储系统完全替代NFS,实现对图片文件和文本文件的存储和读写;
鉴于MogileFS的无目录性,无法直接挂载使用,故向mogilefs集群中添加文件则需要程序通过API调用上传(upload)文件,虽然对于运维人员有点不方便,但也保证了一定的高效性;
如果想体验直接挂载使用分布式文件系统的乐趣,则可以尝试使用MooseFS,在此就不多说了!
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103094.htm
