nginx无痛升级并增加nginx-gridfs模块支持

在不影响

Ubuntu系统版本:

 cat /etc/issue
Ubuntu 12.10 \n \l
cpu:x86_64

nginx版本:原先使用apt-get安装的,已卸载,版本貌似是1.1左右

即将安装nginx版本1.4.4

升级步骤:

1.首先准备好将要使用的工程,下载nginx相应版本源码

2.解压,进入主目录 vi nginx-1.4.4/objs/Makefile修改一个小错误,把第3行的-Werror错误去掉,不然会报一个unused错误。

3.查看现有版本的配置信息nginx -V,并记录下来,比如我的是:

sudo ./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-dav-ext-module  

这就是原先编译的配置参数信息了,但是升级的时候不能直接拿来用,因为后面的--add-module路径参数都不对,而且不同版本间的模块叫法等也许不一样了。

推荐一个网址 http://wiki.nginx.org/ModulesChs#.E6.A0.87.E5.87.86_HTTP_.E6.A8.A1.E5.9D.97

标准模块很多都是必须安装的,如果不想安装使用--without开头的配置。

还有一些可选的模块,看个人的需要添加了,还有邮件模块,另外还有第三方模块,比如nginx-gridfs就属于第三方的模块。

4.这步骤添加nginx-gridfs,如果不需要可以跳过此步骤。首先从github上下载nginx-gridfs工程,然后按照步骤git submodule init, git submodule update.

5.鉴于nginx已经把很多的模块作为标准模块了,因此,有些模块已经被默认安装了,我们配置的时候直接使用原先的配置即可。如果不加nginx-gridfs模块,去掉其参数即可。

sudo ./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module  --add-module=/opt/nginx-gridfs/

配置过程中有可能会出现如下一些错误:

错误提示“
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
”
apt-get install libxml2
apt-get install libxslt1-dev

错误提示“
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
”
apt-get install libgd2-xpm libgd2-xpm-dev

错误提示“
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
”
apt-get install libgeoip-dev
错误提示:”
checking for PCRE library in /usr/include/pcre/ ... not found
“
apt-get install libpcre3-dev libssl-dev

6.make

7.sudo apt-get remove nginx 把原先的nginx相关模块卸载掉。或者which nginx,然后找到位置备份之。

8.sudo make install

9. sudo make upgrade 更新

nginx -v查看当前的版本号是否是1.4.4, which nginx查看当前nginx的链接位置

nginx -V 查看参数配置是否正确

sudo ln -s /et/nginx/sbin/nginx  /usr/sbin/ 


-----------------------------------------------------------------------------------------------

昨晚太晚了,没搞完,今天接着弄!

现在接着配置nginx-grifs,实现nginx-gridfs功能

10. sudo /etc/nginx/nginx.conf 打开配置,添加如下配置:

        location /img/ {
            #gridfs  数据库名     
            gridfs   img  field=filename  type=string; 
            
            mongo 127.0.0.1:27017;  #local
       }

gridfs:nginx识别插件的关键字

pics: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

参考于网络  http://www.cnblogs.com/zhangmiao-chp/archive/2011/05/05/2038285.html

如果出现:

Starting nginx: nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:10
nginx: configuration file /etc/nginx/nginx.conf test failed

的错误,那有可能是你配置错了,location位于http{}中并且在某server{}中

11.上传文件验证:

sudo  mongofiles put --host localhost --port 27017 --db img --local ~/photo.jpg --type jpg

如果提示Error: need a filename错误,就在--local ~/photo.jpg   xxxname.jpg 添加一个名字即可 (http://hi.baidu.com/manbuzhiwu/item/9adbce5d45405811e6c4a59c)

然后浏览器访问 IP/img/xxxname.jpg

如果正确就可以看到图片了。


项目中使用nginx-gridfs,是为了直接访问静态文件,以提高访问效率。使用mongodb存储图片毕竟不是长久之计,随着项目的发展也有可能需要单独做一个图片服务器,甚至集群,不管怎样,不断的升级,不断的重构,不断的优化将伴随着项目的整个过程。不用想的太远,但是也要去预测短期的项目未来,及时做好准备,这样才能走好脚下的路。


推荐几篇关于图片服务的文章 :

l  Web Secret:图片服务(一)——构建一个基本的图片服务

l  Web Secret:图片服务(二)——扩展您的图片服务

l  Web Secret:图片服务(三)——为您的服务加上缓存

l  Web Secret:图片服务(四)——重新设计您的存储架构

l  Web Secret图片服务(五)——提高服务的性能



----------------------------------------------------------- 2013.12.25再次更新  -------------------------------------------------------------------

原来编译的过程中忽略了一个小细节,当时我编译不过,认为是新版本github上提交出现的问题,当时还给其中以为共享者发了邮件,但是没回我,所以从别的地方下载了一个老的版本。后来,成功之后,我以为是我操作不当导致我当时没编译过,所以直接把我的步骤换成了官网的步骤。但是现在我在把程序部署到服务器上的时候才发现,无法通过网址访问到图片,查了一下日志,只发现当访问的时候会出现如下log:

 [error] 11336#0: *1 Mongo connection dropped, could not reconnect, client: 192.168.1.69, server: img.xxx.com, request: "GET /static/img/test.jpg HTTP/1.1", host: "192.168.1.69"

Mongo连接掉线了,并且无法重连。我开始以为是本地ubuntu和服务器系统版本之间的问题,但是服务器升级有风险,遂没有实施。于是想起这个细节,再次在本地验证,果不其然。

附件上传一个我当时下载的经过测试可以正常使用的nginx-gridfs版本。

依然不清楚导致此问题的原因。

资源下载在这里:点我


把图片后缀jpg去掉解压即可。

你可能感兴趣的:(nginx)