nginx 代理 mongodb的 GridFS

参考:

      http://blog.snsgou.com/post-776.html

      http://blog.csdn.net/chen_jp/article/details/7934360      ---mongodb安装yum方式

 

      http://blog.csdn.net/kk185800961/article/details/45427367   ---yum安装3.0.2版本

首先下载相应的安装包:

Nginx1.7.8

http://nginx.org/download/nginx-1.7.8.tar.gz

 

pcre(支持Nginx地址重写)

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz

gzip(页面压缩)

http://zlib.net/zlib-1.2.8.tar.gz

openssl(https安全支持组件)

http://www.openssl.org/source/openssl-1.0.1h.tar.gz

(2)nginx-gridfs插件、MongoDB访问驱动 mongodb-mongo-c-driver

https://github.com/mdirolf/nginx-gridfs/archive/v0.8.tar.gz

https://github.com/mongodb/mongo-c-driver/archive/v0.3.1.tar.gz

#安装pcre库 进入你安装的目录:比如:/opt 

wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz -O pcre-8.35.tar.gz

tar -xzvf ./pcre-8.35.tar.gz

cd pcre-8.35

./configure

make && make install

 

#安装zlib库

wget -c http://zlib.net/zlib-1.2.8.tar.gz -O zlib-1.2.8.tar.gz

tar -xzvf ./zlib-1.2.8.tar.gz

cd ./zlib-1.2.8

./configure

make && make install

 

#解压ssl

wget -c http://www.openssl.org/source/openssl-1.0.1h.tar.gz -O openssl-1.0.1h.tar.gz

tar -xzvf ./openssl-1.0.1h.tar.gz

 

#解压nginx-gridfs

wget -c https://github.com/mdirolf/nginx-gridfs/archive/v0.8.tar.gz -O nginx-gridfs-0.8.tar.gz

tar -xzvf ./nginx-gridfs-0.8.tar.gz

 

  #解压mongo-c-driver

wget -c https://github.com/mongodb/mongo-c-driver/archive/v0.3.1.tar.gz -O mongo-c-driver-0.3.1.tar.gz

tar -xzvf ./mongo-c-driver-0.3.1.tar.gz

mv ./mongo-c-driver-0.3.1/* ./nginx-gridfs-0.8/mongo-c-driver

 

#安装nginx   /opt目录

wget -c http://nginx.org/download/nginx-1.7.8.tar.gz -O nginx-1.7.8.tar.gz

tar -xzvf ./nginx-1.7.8.tar.gz

cd ./nginx-1.7.8

mkdir /opt/nginx

./configure --prefix=/opt/nginx  --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/pcre-8.35 --with-zlib=/opt/zlib-1.2.8 --with-openssl=/opt/openssl-1.0.1h --add-module=/opt/nginx-gridfs-0.8

make && make install

 

此时当查看 

#./nginx -t 

显示:nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

          nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

     则表示安装Nginx成功!

 

yum安装mongodb   (非yum安装则可以参考:http://www.qttc.net/201303298.html

http://www.qttc.net/201304299.html

  # cd /etc/yum.repos.d

  创建一个以repo结尾的文件

  # touch 10gen.repo     

  # vim 10gen.repo

  # cat 10gen.repo

 编辑10gen.repo文件 ,添加如下内容:

[mongodb-10gen]  

name=10gen Repository

baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64

gpgcheck=0

enabled=1

保存退出;

# yum list|grep mongo 将出现类似如下内容



 

进入root目录;运行命令安装:

# yum install mongo-10gen mongo-10gen-server  ;则如下图:

 

 选择:y

 依次下载相关的包:

 

# chkconfig --list|grep mongo     #查看一下mongo服务

# chkconfig mongod on     #开机启动 

# chkconfig --list|grep mongo  

# service mongod start    #启动mongodb 

# service mongod status  

# mongo   #连接测试,安装成功。

# exit       # 退出mongodb 命令模式

# service mongod stop       #因为是yum安装故,直接用服务启动就可以,千万不要用kill -9 PID 方式操作,会损坏mongodb。 

以后有更新了,停掉mongodb,执行yum update mongo-10gen mongo-10gen-server 即可。

服务器配置: /etc/mongod.conf

mongfiles 帮助命令

#mongofiles --help

上传文件到指定的数据库的集合,带用户名密码

#mongofiles -h 127.0.0.1 -d changhongeb -u changhongeb -p changhongeb -c imgs put e850352.gif

通过mongo 命令连接mongodb数据库

#mongo 127.0.0.1:27017/changhongeb -u changhongeb -p changhongeb

配置nginx-gridfs

 

# vim /opt/nginx/nginx.conf

在 server 节点中添加 location 节点

 

location /pics/ {

    gridfs  edusns

    root_collection=sns_attach

    field=filename

    type=string

    user=eduadmin

    pass=eduadmin$123;

    mongo  172.8.8.153:27017;

}

注意,如果不指定 field,默认为 MongoDB 的自增ID,且type为int

参数介绍:

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

猜你喜欢

转载自yjph83.iteye.com/blog/2165964