MongoDB nginx-gridfs安装并使用

nginx-gridfs是一个 Nginx 的扩展模块,用于支持直接访问 MongoDB 的 GridFS 文件系统上的文件并提供 HTTP 访问。

nginx-gridfs源码:https://github.com/barakav/gridfs-nginx-plugin

mongo-c-driver源码:https://github.com/eagleas/mongo-c-driver

1.下载nginx、nginx-gridfs安装

wget http://nginx.org/download/nginx-1.7.9.tar.gz

wget https://github.com/barakav/gridfs-nginx-plugin/archive/master.zip

tar -zxvf nginx-1.7.9.tar.gz 

unzip master 

rm -rf master 

mkdir nginx

mv gridfs-nginx-plugin-master gridfs-nginx-plugin

cd  gridfs-nginx-plugin

wget https://github.com/eagleas/mongo-c-driver/archive/master.zip

unzip master 

rm -rf master 

rm -rf mongo-c-driver

mv -f mongo-c-driver-master mongo-c-driver

安装nginx前需要安装依赖包:zlib,pcre,openssl

yum -y install pcre-devel openssl-devel zlib-devel

cd nginx-1.7.9

安装nginx

./configure --prefix=/home/mongo/nginx --with-openssl=/usr/include/openssl --with-http_stub_status_module --add-module=/home/mongo/gridfs-nginx-plugin

make

make install

在make编译的时候,若报如下错误:

cc1: warnings being treated as errors
/home/mongo/gridfs-nginx-plugin/mongo-c-driver/src/bson.c: In function ‘bson_ensure_space’:
/home/mongo/gridfs-nginx-plugin/mongo-c-driver/src/bson.c:632: warning: comparison between signed and unsigned
make[1]: *** [objs/addon/src/bson.o] Error 1
make[1]: Leaving directory `/home/mongo/nginx-1.7.9'
make: *** [build] Error 2

在objs/Makefile中将 -Werror 删除,对于warnings忽略

2.nginx-gridfs插件配置说明(conf/nginx.conf)

gridfs:nginx识别插件的关键字
edusns:db名
[root_collection]: 选择collection,如root_collection=blog, mongod就会去找blog.files与blog.chunks两个块,默认是fs
[field]: 查询字段,保证mongdb里有这个字段名,支持_id, filename, 可省略, 默认是_id
[type]: 解释field的数据类型,支持objectid, int, string, 可省略, 默认是int
[user]: 用户名, 可省略
[pass]: 密码, 可省略
mongo: mongodb url
参考实例:
location /photo/ {
  gridfs  gridfs
  root_collection=fs
  field=filename
  type=string;
  #user=eduadmin
  #pass=eduadmin$123;
  mongo  192.168.36.6123000;
}
注意,如果不指定 field,默认为 MongoDB 的自增ID,且type为int,基于副本集配置
location /photo/ {
    gridfs my_app field=filename type=string;
    mongo "foo"
          10.7.2.27:27017
          10.7.2.28:27017;
}
启动nginx:/home/mongo/nginx/bin/nginx
在浏览器里输入http://192.168.36.61/photo/20150315172324.png 能下载图片就说明成功了!

3.注意点

(1)在测试配置时要记住不要将nginx的文件过期缓存时间配置开启了,最好是在配置好服务器以后再做这个工作,否则很容易造成配置错误的假象。
(2)要保证系统启动过程中,MongoDB比nginx先启动,否则nginx-gridfs初始化的时候不能正确链接MOngoDB数据库。

(3)nginx-gridfs的不足:没有实现http的range support,也就是断点续传,分片下载的功能。

gridfs受限于mongodb本身的性能限制,在存储大量数据后写性能下降很快,读并发也不尽如人意。在吞吐峰极限值(6台集群2分片30-40M/s)情况下日志会出现大量。所以,他并不适合作为一个专用的大规模的分布式文件存储系统,但能够满足一般应用级别静态文件存储需求。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值