nginx 扩展 ngx_lua 的数据量连接池(1)

来源:互联网 发布:分布式数据库一致性 编辑:程序博客网 时间:2024/06/08 13:55

因为php 中应用mysql 并没有一个连接池的感念,所以看完 春哥在Tech-Club上关于『由Lua粘合的Nginx生态环境』的演讲实录后萌生了 使用的念头!

要让nginx有编写lua 的能力,我们这里还需要安装 要最新版的Nginx,LuaJIT,ngx_devel_kit,ngx_lua等安装文件。

这里推荐下载 2.0版本!因为第一次下载的1.18版本好像并不支持64系统,这让我纠结了~

先安装依赖包

yum install readline-devel pcre-devel openssl-devel

wget http://agentzh.org/misc/nginx/drizzle7-2011.07.21.tar.gz tar xzvf drizzle7-2011.07.21.tar.gzcd drizzle7-2011.07.21/./configure --without-servermake libdrizzle-1.0make install-libdrizzle-1.0ln -s /usr/local/lib/libdrizzle* /usr/lib64/

shell> wget http://luajit.org/download/LuaJIT-<VERSION>.tar.gzshell> tar zxvf LuaJIT-<VERSION>.tar.gzshell> cd LuaJIT-<VERSION>shell> makeshell> make install

因为安装在缺省路径,所以LuaJIT对应的lib,include均在/usr/local目录里。

shell> export LUAJIT_LIB=/usr/local/libshell> export LUAJIT_INC=/usr/local/include/luajit-<VERSION>

假如你已经安装了 nginx模块 那么就需要关闭nginx 模块进行从新编译加上 ngx_devel_kit,ngx_lua 模块。

首先 先备份 nginx 从新编译 nginx

ps 说明(因为公司需要自己还添加了cgi发射器ngix 扩展 所以你看到这文章的时候不要把这个模块安装上就是了! 因为我想做一次安装记录备份,这里有点不专业了)

/usr/local/nginx/sbin/nginx -s stopmv /usr/local/nginx/ /usr/local/nginx20111226cd /root/soft/lnmp0.7-full/nginx-0.8.54tar zxvf gnosek-nginx-upstream-fair-5f6a3b7.tar.gz./configure --user=www --group=www --prefix=/usr/local/nginx --add-module=../ngx_devel_kit-0.2.17 --add-module=../echo-nginx-module-0.41 --add-module=../xss-nginx-module-0.03rc9 --add-module=../ngx_coolkit-0.2rc1 --add-module=../set-misc-nginx-module-0.22rc8 --add-module=../form-input-nginx-module-0.07rc5 --add-module=../encrypted-session-nginx-module-0.02 --add-module=../ngx_lua-0.6.10 --add-module=../headers-more-nginx-module-0.18 --add-module=../srcache-nginx-module-0.16 --add-module=../array-var-nginx-module-0.03rc1 --add-module=../memc-nginx-module-0.13rc3 --add-module=../redis2-nginx-module-0.09 --add-module=../redis-nginx-module-0.3.6 --add-module=../auth-request-nginx-module-0.2 --add-module=../rds-json-nginx-module-0.12rc10 --add-module=../rds-csv-nginx-module-0.05rc2 --add-module=../drizzle-nginx-module-0.1.4 --with-http_ssl_module --with-http_stub_status_module --with-http_ssl_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6cp /usr/local/nginx20111226/conf/nginx.conf /usr/local/nginx/conf/nginx.confcp /usr/local/nginx/conf/fastcgi.conf /usr/local/nginx/conf/fcgi.confservice nginx start

试着启动一下Nginx看看,如果你运气不好的话,可能会遇到如下错误:

cannot open shared object file: No such file or directory

这是神马情况?可以用ldd命令来看看:

libluajit-<VERSION>.so => not found

此类问题通常使用ldconfig命令就能解决:

shell> echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.confshell> ldconfig
再试着启动Nginx看看,应该就OK了。

应用

我们先用一个简单的程序来暖暖场:把下面的代码加入到Nginx的配置文件nginx.conf,并重启Nginx,然后浏览,就能看到效果了。



location /lua {    set $test "hello, world.";    content_by_lua '        ngx.header.content_type = "text/plain";        ngx.say(ngx.var.test);    ';}

打开浏览器访问 http:{ip}/lua 

在浏览器显示 

hello, world. 已经成功安装! 这里要谢谢 http://huoding.com/2012/08/31/156 这博文! 大家也可以过去看看我这里还要强力吐槽 csdn 的编辑器实在让我受不了!!~ 以后想换个地方了!

原创粉丝点击