CentOS6.3搭建Nginx代理访问MongoDB GridFS图片资源


PHP可以直接读取MongoDB GridFS中的图片并显示到页面中,但对PHP的压力就大了。偶然机会,了解到Nginx可以代理访问,实现过程如下:

1、工具准备

安装一些必要的编译工具及库,这里是直接从“编译安装LNMP”系列教材中摘取的,有点冗余。

1
yum -y  install  make  apr* autoconf automake curl-devel  gcc  gcc -c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch gettext glib2-devel  bzip2 -devel libuuid-devel docbook-style-xsl libxslt-devel  gzip -devel

 

2、源码准备

(1)Nginx基础软件

Nginx1.4.4

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

pcre(支持Nginx地址重写)

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

gzip(页面压缩)

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

openssl(https安全支持组件)

http://www.openssl.org/source/openssl-1.0.1e.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

 

3、编译安装Shell脚本

install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
PATH= /bin : /sbin : /usr/bin : /usr/sbin : /usr/local/bin : /usr/local/sbin :~ /bin
export  PATH
 
#需要切换到root用户
if  [[ $EUID - ne  0 ]];  then
     echo  "This script must be run as root"
     exit  1
fi
 
ROOT_PATH= /usr/local/src/2
mkdir  $ROOT_PATH
rm  -rf $ROOT_PATH/*
 
##################################################
# 必要的工具
##################################################
 
yum -y  install  make  apr* autoconf automake curl-devel  gcc  gcc -c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch gettext glib2-devel  bzip2 -devel libuuid-devel docbook-style-xsl libxslt-devel  gzip -devel
 
##################################################
# 安装Nginx
##################################################
 
#安装pcre库
cd  $ROOT_PATH
wget -c  ftp : //ftp .csx.cam.ac.uk /pub/software/programming/pcre/pcre-8 .33. tar .gz -O pcre-8.33. tar .gz
tar  -xzvf . /pcre-8 .33. tar .gz
cd  . /pcre-8 .33
. /configure
make  &&  make  install
 
#安装zlib库
cd  $ROOT_PATH
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
cd  cd  $ROOT_PATH
wget -c http: //www .openssl.org /source/openssl-1 .0.1e. tar .gz -O openssl-1.0.1e. tar .gz
tar  -xzvf . /openssl-1 .0.1e. tar .gz
 
#解压nginx-gridfs
cd  cd  $ROOT_PATH
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
cd  $ROOT_PATH
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
cd  $ROOT_PATH
wget -c http: //nginx .org /download/nginx-1 .4.4. tar .gz -O nginx-1.4.4. tar .gz
tar  -xzvf . /nginx-1 .4.4. tar .gz
cd  . /nginx-1 .4.4
mkdir  /usr/local/nginx
. /configure  --prefix= /usr/local/nginx  --user=nginx --group=nginx --sbin-path= /usr/local/nginx/nginx  --conf-path= /usr/local/nginx/nginx .conf --pid-path= /usr/local/nginx/nginx .pid --with-http_ssl_module --with-pcre=$ROOT_PATH /pcre-8 .33 --with-zlib=$ROOT_PATH /zlib-1 .2.8 --with-openssl=$ROOT_PATH /openssl-1 .0.1e --add-module=$ROOT_PATH /nginx-gridfs-0 .8
make  &&  make  install
 
 
#添加nginx用户及用户组
groupadd www   #添加nginx用户组及用户
useradd  -g www -s  /usr/sbin/nologin  www   #不让nginx用户直接登录

 

4、配置nginx-gridfs

1
vim  /usr/local/nginx/conf/nginx .conf

在 server 节点中添加 location 节点

1
2
3
4
5
6
7
8
9
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

 

5、给MongoDB上传图片 

1
/usr/local/mongodb/bin/mongofiles  put --host 172.8.8.153 --port 27017 --db edusns -- local  ~ /photo .jpg -- type  jpg

在浏览器里输入http://localhost/pics/photo.jpg 能下载图片就说明成功了!

 

6、启动Nginx

1
/usr/local/nginx/nginx

 

存在的问题:浏览器端不能缓存,每次请求都要从服务器获取

文章来源:http://www.cnblogs.com/phpgo/p/5672468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值