(一)安装nginx前的准备
安装nginx需要安装openssl和pcre,具体安装步骤请参考nginx安装的相关博文
(二)nginx和nginx-gridfs 联合编译安装
nginx-gridfs 源码下载(包含Mongo-C-Driver可用的下载链接):地址 http://files.cnblogs.com/de0319gh/nginx-gridfs.zip
nginx和nginx-gridfs 联合编译安装三个步骤:
(1)./configure --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl --with-http_stub_status_module --add-module=/home/cdh/Downloads/mdirolf-nginx-gridfs
(2)make (3)make install
(三)nginx-gridfs 配置
配置的语法说明:
gridfs db_name [root_collection] [field] [type] [user] [pass];
mongo mongodb url;
[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]:密码, 可省略
配置示例:
(1)指定db为my_app,其他均为默认,默认服务器为本地
location /gridfs/ { gridfs my_app; }
(2)
location /gridfs/ { gridfs my_app field=filename type=string; mongo 127.0.0.1:27017; }
(3)用于副本集
location /gridfs/ { gridfs my_app field=filename type=string; mongo "foo" 10.7.2.27:27017 10.7.2.28:27017; }
(4)完整配置
location /gridfs/ { gridfs my_app root_collection=pics field=_id type=int user=foo pass=bar; mongo 127.0.0.1:27017; }
(四)注意点
(1)在测试配置时要记住不要将nginx的文件过期缓存时间配置开启了,最好是在配置好服务器以后再做这个工作,否则很容易造成配置错误的假象。
(2)要保证系统启动过程中,MongoDB比nginx先启动,否则nginx-gridfs初始化的时候不能正确链接MOngoDB数据库。
了解CentOS守护进程启动顺序的可以参考 http://wenku.baidu.com/view/f13befcfa1c7aa00b52acb40.html
查看启动顺序 ls /etc/rc3.d
以上内容都时链接到/etc/init.d/目录里的相应守护进程启动脚本,S55nginx 意思时nginx的守护进程启动顺序为55,S85mongod 意思时mongod的启动顺序为85,问题就在mongod比nginx晚启动。
修改启动顺序,将mongod的启动顺序值改为比nginx小的数:
mv /etc/rc3.d/S85mongod /etc/rc3.d/S54mongod
mv /etc/rc5/d/S85mongod /etc/rc5.d/S54mongod
ok,现在reboot重启解决问题。
(五)nginx-gridfs错误解决
问题描述:
nginx -V检查毫无问题,但是访问所有gridfs内容均报503 Service Temporarily Unavailable,看nginx日志:Mongo connection dropped, could not reconnect。
解决方案:重新clone gridfs,然后不要git submodule init; git submodule update; 而是:
git clone https://github.com/eagleas/mongo-c-driver.git
换了一个别人的mongo-c-driver,重新编译安装nginx即可。
文章出处:https://github.com/mdirolf/nginx-gridfs/issues/52
引用:
To fix the isseu you need the latest master branch of the C driver (the GridFS module uses another branch, which seems to be faulty).
After cloning this repo, do not do 'git submodule init && git subodule update', but instead:
git clone https://github.com/eagleas/mongo-c-driver.git
Then compile nginx as usual.
There are one problem is that you must delete the -Werror option of gcc in [nginx-source/objs/MakeFile].
If you don't do that, the warning will be treated as errors when you do make.
the log of make below:
cc1: warnings being treated as errors
/home/Administrator/upload/nginx-gridfs//mongo-c-driver/src/numbers.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g --std=c99 -Isrc -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/ngx_modules.o \
关于nginx的卸载
nginx的卸载要看你是怎么装的nginx。RPM装的 用rpm -e卸载
yum装的用 yum remove卸载 ,源码装的直接删文件就OK.
另外一篇比较好的介绍mongo+nginx-gridfs安装的地址 http://blog.sina.com.cn/s/blog_40e8378d0100oxuy.html